0

I have this SQL:

$sql = "INSERT INTO orders (ID, Order_ID, Status, FName, LName, Email, 
Phone)VALUES ($UID, $orderID, 'Pending', '$fname', '$lname', '$email', 
'$phone');
INSERT INTO orders_inventory (Order_invID, Item_ID, Order_ID, Quantity) 
VALUES 
(NULL, $item_ID, $orderID, 1);";

This is how I connect it:

if(mysqli_query($db, $sql)){
   echo "three";
}

I did an echo on the $sql and this is what I got:

INSERT INTO orders (ID, Order_ID, Status, FName, LName, Email, Phone) 
VALUES (92, 625015841, 'Pending', '1', '1', '1@1', '1'); 
INSERT INTO orders_inventory (Order_invID, Item_ID, Order_ID, Quantity) 
VALUES (NULL, 1, 625015841, 1);

The SQL works when I paste it into the database manually, but the database crashes when I use the website PHP. The $DB is to connect to the database and it works because I tested it, and I have also been using it throughout the whole website.

I then did an error check using mysqli_error(db) and I get this error:

"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO orders_inventory (Order_invID, Item_ID, Order_ID, Quantity) VALUES (' at line 2"

Help would be greatly appreciated as I'm very stuck and don't know how to get around this or fix this problem

9
  • sorry the database doesnt crash when i use the website php, the php code just doesnt reach the echo "three" Commented Aug 31, 2018 at 2:42
  • try and execute the two statements one by one Commented Aug 31, 2018 at 2:44
  • Try removing the semicolon at the end. The semicolon is not part of SQL and some database drivers don't like it. Commented Aug 31, 2018 at 2:45
  • 2
    Wait... those are TWO statements! Run them separately. Commented Aug 31, 2018 at 2:47
  • 1
    Command line and GUI tools usually support multi-statements, whille the PHP API does not (per default). Commented Aug 31, 2018 at 2:53

1 Answer 1

3

You're attempting to run two queries at once, which mysqli_query will not do. However you can use mysqli_multi_query instead:

if(mysqli_multi_query($db, $sql)){
    echo "three";
}
Sign up to request clarification or add additional context in comments.

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.