Tuesday 4 September 2018

Why the second query does not work?

I use ajax to call a php file to do the sql query. When the user press a button, the php file does some queries including one select and two insert. It works fine when the user press the button for the first time. In the second time, only the select query and the first insert query works, the second insert query does not insert anything. I have checked the query, it is all fine. It seems by some reason the second insert query is not executed.

$itemquery = "insert into `Items` values (...)";
if($affected_rows = $db->exec($itemquery) ){
    ...
    $pickupquery = "insert into `Pickup` values (...)";
    if ($affected_rows2 = $db->exec($pickupquery)) {
        echo "success";
    }
    else echo "No pickup is inserted";
}

For the first time I press the button, both query works fine (outputs "success"). Items are inserted into tables. But the second time I press it, only the $itemquery works, the $pickupquery does not insert anything (outputs "No pickup is inserted"). The $pickupquery itself has no problems, but I don't know why it just not work after the first press.
Any ideas?

Your SQL syntax is incorrect. It should be
INSERT INTO `Items` (COL_NAMES) VALUES (a, b,...)

for example
INSERT INTO `Items` (`name`, `price`) VALUES ('Beans', 0.40);

0 comments:

Post a Comment