0

I have the following checkbox items and I want to insert them in the database.

<form action="test.php" method="post">
<input type="checkbox" name="check_list[]" value="rice:10">
<input type="checkbox" name="check_list[]" value="milk:8">
<input type="checkbox" name="check_list[]" value="orange:4">
<input type="submit" />
</form>

The following is the php code:

if(!empty($_POST['check_list'])) {
    foreach($_POST['check_list'] as $check) { 
        $mealA = explode(":",$check);
        $meal = $mealA[0];
        $sql1 = "INSERT INTO Order (User, Meal, Total) VALUES ('$name' , '$meal', '$total')";
        if (mysqli_query($mysqli,$sql1)) {
            echo "You order has been recieved";
        } else {
            echo "Error: " . $sql1;
        }
    }
}

Note that the value of each checkbox means item name:price.

The problem is that I got error "Error: " . $sql1;

5
  • Where is $mysqli value coming from ? Commented Apr 28, 2015 at 18:31
  • Also, try adding mysqli_error to your code: php.net/manual/en/mysqli.error.php Commented Apr 28, 2015 at 18:31
  • 2
    Change "Error: " . $sql1 to "Error:" . mysqli_error($mysqli) so you can have a better understanding of why it fails. Commented Apr 28, 2015 at 18:33
  • echo the three values $name, $meal, and $total to see what exactly is contained in them. If they look fine try running the failing sql command manually in mysql to see if it still gives an error. Commented Apr 28, 2015 at 18:38
  • I changed what @kidA suggested and got this error Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Order (User, Meal, Total) VALUES ('bero' , 'margherita', )' at line 1Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Order (User, Meal, Total) VALUES ('bero' , 'sicilian', )' at line 1 Commented Apr 28, 2015 at 19:21

2 Answers 2

2

Your query fails because you have not set a value for $total. So either you have to remove the Total field from the query resulting in

$sql1 = "INSERT INTO Order (User, Meal) VALUES ('$name' , '$meal')";

which will give a NULL value to the Total column for each successful new insert

OR

based on the code you provided I 'm guessing you wanted to do something similar to this?

$mealA = explode(":",$check);
$meal = $mealA[0];
$total = $mealA[1]; // setting the value of $total otherwise query is going to fail
$sql1 = "INSERT INTO Order (User, Meal, Total) VALUES ('$name' , '$meal', '$total')";
Sign up to request clarification or add additional context in comments.

Comments

0

The problem was the the insert statement:

INSERT INTO Order (User, Meal, Total) VALUES ('$name' , '$meal', '$total')

I changed the table name from Order to Orders and I removed Total since it is null and there is no value stored in it.

Thanks for your suggestions.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.