0

I want in my file execute 3 query at once, my code looks like this:

$stmtinsertcustomers = $conn->prepare("INSERT INTO customers (id, name, phone)
VALUES ('', ?, ?)");
echo $conn->error;
$stmtinsertcustomers->bind_param("ss", $name, $phone);
$stmtinsertcustomers->execute();
$last_id = mysqli_insert_id($conn);
$stmtinsertcustomers->close();


$stmtinsertorder = $conn->prepare("INSERT INTO orders (customerid, orderid, orderdate) VALUES (?,?,?)");
echo $conn->error;
$stmtinsertorder->bind_param("sss", $last_id, $orderid, $date);
$stmtinsertorder->execute();
$stmtinsertorder->close();

$stmtinsertorderitems = $conn->prepare("INSERT INTO orderitems (orderid, productid) VALUES (?,?)");
echo $conn->error;
$stmtinsertorderitems->bind_param("ss", $orderid, $productid);
$stmtinsertorderitems->execute();
$stmtinsertorderitems->close();

But it's unfortunately not working, what is the problem? Thank you for helping!

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
viktor1230
  • 41
  • 1
  • 7
  • 1
    Firstly it's worth knowing which API you are using, PDO and mysqli have different methods and you access them in slightly different ways. You say you are using PDO, but call `mysqli_insert_id()` and `bind_param()`. – Nigel Ren Mar 09 '21 at 09:17
  • I dont use any API, yes you're right, I need to use $last_id = $stmtinsertcustomers->lastInsertId(); if I understand correctly – viktor1230 Mar 09 '21 at 09:22
  • 1
    PDO and mysqli are both API's for using a database. You can't mix the calls between the 2 different API's and so you need to first ensure you know which you are using. – Nigel Ren Mar 09 '21 at 09:24
  • I want to use PDO rather because of SQL injuries, but thanks good to know. But why theese multiple inserts not working? – viktor1230 Mar 09 '21 at 09:26
  • you can prevent `sql injuries` (lol) with `mysqli` just as with `PDO`... – Professor Abronsius Mar 09 '21 at 09:27
  • OMG sorry injections – viktor1230 Mar 09 '21 at 09:31
  • At what point do the above queries fail? Does the first `insert` correctly return the `insert_id` value? Do any records get inserted? – Professor Abronsius Mar 09 '21 at 09:33
  • Also - it might be worth looking into [triggers](https://dev.mysql.com/doc/refman/8.0/en/trigger-syntax.html) - you could easily replace the last statement with a trigger – Professor Abronsius Mar 09 '21 at 09:35
  • Here's how you can check for the [actual mysqli error](https://stackoverflow.com/questions/22662488/mysqli-fetch-assoc-expects-parameter-call-to-a-member-function-bind-param/22662582#22662582) and here's how to do the same for [PDO](https://stackoverflow.com/questions/32648371/my-pdo-statement-doesnt-work/32648423#32648423). – El_Vanja Mar 09 '21 at 09:35
  • The first one is not inserting nothing but last 2 one working perfectly – viktor1230 Mar 09 '21 at 09:35
  • Side note: you can skip the id in the insert statement if it's auto incremental. – El_Vanja Mar 09 '21 at 09:36
  • It's impossible. There is always either a successful insert or an error. The `''` is improper value for a numeric field that may cause an error – Your Common Sense Mar 09 '21 at 09:36

0 Answers0