Reputation: 3
projectcostingfile table I have checked my sql query several times and make sure that the names are correct. Still not inserting.
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
$sqlinsert="INSERT INTO projectcostingfile (material) VALUES ('".$selected."')";
mysqli_query($conn, $sqlinsert);
}
}
}
The variable $selected is echoed correctly. There is no error when I run the code too. Any help will be appreciated!
Thanks for your help. The problem lies with my table structure like what some of you mentioned. I set the column fields to NOT NULL. Therefore inserting into only 1 column would not have any INSERT record shown in the database.
Upvotes: 0
Views: 83
Reputation: 479
Most of the solutions have been given above. But i would like to tell you that even a faced such issue were my code, Mysql connnection, query , php codes
Where right but in myphpadmin
inside my database i have accidentally added a 'black Space' near sql table or column name because to which my sql query was not giving me the error but also not inserting the code.i recommend you to try this.
If doesn't work than my bad.
Upvotes: 0
Reputation: 363
Definitely there is nothing wrong with your PHP code, but it does not comply DB schema. Check error.log file on your PHP server and it will show you lot of DB errors, concerning NULL values.
Upvotes: 0
Reputation: 350
I think you cannot insert only one field
material
in your given table. From the picture shown in your question, all fields in your table should not be null
. So i think we cannot added rows in that table by filling only one field. You have to insert all the fields or change the null status to yes
of all other field except material
.
I think this might work if you don't have any other options.
Upvotes: 0
Reputation: 889
Please try this:
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
$check_values = implode(",",$_POST['check_list']);
echo $check_values;
$sqlinsert="INSERT INTO projectcostingfile (material) VALUES ('".$check_values."')";
mysqli_query($conn, $sqlinsert);
}
}
Hope, this will help you.
Upvotes: 0
Reputation: 33
if(isset($_POST['submit'])){//to run PHP script on submit
if(!empty($_POST['check_list'])){
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $selected){
echo $selected."</br>";
$sqlinsert="INSERT INTO projectcostingfile (material) VALUES ('".$selected."')";
mysqli_query($conn, $sqlinsert);
}
}
}
above code is your correct you just put following code such as post array
<input type="text" name="check_list[]">
Upvotes: 0
Reputation: 138
Use
$sqlinsert="INSERT INTO projectcostingfile SET material = '".$selected."'";
Also use 'or die(mysqli_error());' after mysqli_query to identify the error..
mysqli_query($conn, $sqlinsert) or die(mysqli_error($conn));
Upvotes: 3
Reputation: 2532
Try to Replace Below Syntax :
$sqlinsert="INSERT INTO projectcostingfile (`material`) VALUES ('".$selected."')";
Upvotes: 0