0

i want to insert session cart into database mysql

here is my code

foreach($_SESSION['cart'] as $productid => $qty){
    $query3 = "INSERT INTO dtransaction(trans_id, pro_id, quantity) VALUES ('$new', '$productid', '$qty')";
    $query4 = mysqli_query($con, $query3);
    print_r($query3."<br>");
}

the result of print_r

INSERT INTO dtransaction(trans_id, pro_id, quantity) VALUES ('TR033', '21', '1')
INSERT INTO dtransaction(trans_id, pro_id, quantity) VALUES ('TR033', '16', '2')
INSERT INTO dtransaction(trans_id, pro_id, quantity) VALUES ('TR033', '13', '8')
INSERT INTO dtransaction(trans_id, pro_id, quantity) VALUES ('TR033', '12', '3')

im using auto_increament (int) for detail_id but in database, inserted like this

detail_id   trans_id    pro_id  quantity
197         TR034       21      1
201         TR035       21      1

detail_id jump 4, pro_id and quantity only insert the first data

what i want is like this

detail_id   trans_id    pro_id  quantity
198         TR034       21      1
199         TR034       16      2
200         TR034       13      8
201         TR034       12      3

how to do that? sorry for my bad english

  • Is detail_id a nullable field? You aren't checking to see if the insert query failed, so all we can do is make guesses about it. Since you didn't insert a value into detail_id, I assume it isn't nullable and the query failed. But, because you didn't check the query for errors, that is just a guess. – kainaw Jun 01 '16 at 19:23
  • @kainaw im using auto_increament in detail_id – Ryan Anugrah Jun 01 '16 at 19:28
  • OK. So, what's the query error? – kainaw Jun 01 '16 at 19:32
  • @kainaw the query is running, but only insert 1 row(1 product), i have 4 products in my shopping cart, i need to insert all 4 products into 4 rows in my database – Ryan Anugrah Jun 01 '16 at 19:34
  • OK. So what's the query error? Until you add to your code a check for the query error and print the error so you can see the error, you will keep assuming there was no query error and keep working on the wrong problem. – kainaw Jun 01 '16 at 19:37
  • @kainaw ooh i got it, i make my trans_id as unique in my database, so it's say duplicate entry. solved now thanks – Ryan Anugrah Jun 01 '16 at 19:44

0 Answers0