I have a form like this
<form id="formID" method="post" action="" enctype="multipart/form-data">
<input type="checkbox" name="attribute[]" value="Disk space"/>Disk space<br />
<input type="checkbox" name="attribute[]" value="Color"/>Color<br />
<input type="checkbox" name="attribute[]" value="Processor"/>Processor<br />
<input type="submit" name="save" id="save" value="save"/>
</form>
In mysql I have a database. The database structure is like this.
CREATE TABLE IF NOT EXISTS `ia_attributes` (
`attribute_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`attribute_name` text NOT NULL,
PRIMARY KEY (`attribute_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=186 ;
INSERT INTO `ia_attributes` (`attribute_id`, `attribute_name`) VALUES
(170, 'Color'),
(179, 'Disk space'),
(185, 'Processor');
As in the upper form you can see I have checkbox and I wanted to insert all the checkboxes at a time I made my code like this
<?php
$host = 'localhost';
$username = 'root';
$password = 'root';
$dbname = 'database';
$con=mysqli_connect($host,$username,$password);
mysqli_select_db($con,$dbname) or die ("no database");
<?php
if(isset($_POST["save"]) && isset($_POST["type"])){
$types = $_POST["type"];
if(sizeof($types) > 0 ){
foreach($types as $type){
$qry= '';
$result = mysqli_query($con,"SELECT COUNT(*) FROM `ia_attributes` where `attribute_name`='".$type."'");
if($result->num_rows == 1){
echo "<script>
alert('There are no fields to generate a report');
</script>";
}
else {
$qry = "INSERT INTO `ia_attributes`(`attribute_id`, `attribute_name`) VALUES ('', '".$type."')";
$sql_query = mysqli_query($con, $qry);
if($sql_query) {
echo "success";
}
else {
echo "error";
}
}
}
}
}
?>
Here whatever I am trying to save its showing only the alertbox There are no fields to generate a report. The condition which has been given is not working. So can someone kindly tell me how to fix this issue?
Do the select query with condition as attribute_name = selected color in your database. Then check number of results it returns. If it is greater than 0 then it already exist in your database, then display alert that it already exist.
if(isset($_POST["save"]) && isset($_POST["attribute"])){
$attributes = $_POST["attribute"];
if(sizeof($attributes) > 0 ){
foreach($attributes as $attribute){
$qry= '';
$result = mysqli->query($con,"SELECT attribute_id FROM ia_attributes where attribute_name=$attribute"));
/* determine number of rows result set */
$row_cnt = $result->num_rows;
if($row_cnt > 0){
echo "<script>
alert('There are no fields to generate a report');
</script>";
}
else{
$qry = "INSERT INTO `ia_attributes`(`attribute_id`, `attribute_name`) VALUES ('', '".$attribute."')";
$sql_query = mysqli_query($con, $qry);
if($sql_query) {
echo "success";
}
else {
echo "error";
}
}
}
}
}
You can use UNIQUE Index for attribute_name. If you add unique index to attribute_name and try to insert a value that has been already added, mysqli_query returns false.
$qry = "INSERT INTO `ia_attributes`(`attribute_id`, `attribute_name`) VALUES ('', '".$attribute."')";
$sql_query = mysqli_query($con, $qry);
if($sql_query) {
echo "success";
} else {
echo "'$attribute' has been already added";
}
Here is something you could do:
<?php
function inArray($needle, $haystack) {
if (in_array($needle, $haystack)) {
return true;
}
foreach ($haystack as $stack) {
if (is_array($stack) && in_array($needle, $stack)) {
return true;
}
}
return false;
}
$host = 'localhost';
$user = 'root';
$password = '-Blue78-';
$database = 'test';
try {
$mysqli = new mysqli($host, $user, $password, $database);
if ($mysqli->connect_errno) {
throw new Exception('Database connection failed: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST["attribute"])) {
$attributes = $_POST["attribute"];
if (count($attributes)) {
foreach ($attributes as $attribute) {
// check if record allready exists
$result = $mysqli->query('SELECT attribute_name FROM ia_attributes');
$existing = $result->fetch_all();
if (inArray($attribute, $existing)) {
echo 'Atribute allready exists';
} else {
$query = "INSERT INTO ia_attributes (attribute_name) VALUES ('" . $mysqli->real_escape_string($attribute) . "')";
$insert = $mysqli->query($query);
echo ($result) ? 'Success' : 'Failed';
}
}
}
}
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>