0

I have a form with data I will like to insert in 2 different tables (order and order_etails). Here is what I did. But it is inserting in only 1 form.

<?php
include '../db/connect.php';

$sql="INSERT INTO order_etails (part_id, quantity, price, status_id,order_id)
  VALUES
        ('$_POST[part_id]','$_POST[quantity]','$_POST[price]','$_POST[status_id]','$_POST[order_id]     '),
('$_POST[part_id2]','$_POST[quantity2]','$_POST[price2]','$_POST[status_id2]','$_POST[order    _id2]'),
('$_POST[part_id3]','$_POST[quantity3]','$_POST[price3]','$_POST[status_id3]','$_POST[order    _id3]')";

$sql1="INSERT INTO order (platform)
VALUE
('$_POST[platform]')";

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
 }
echo "record(s) added";

mysqli_close($con);

?>

I have also tried this:

<?php
include '../db/connect.php';


$sql="INSERT INTO order_details (part_id, quantity, price, status_id,order_id)
VALUES
('$_POST[part_id]','$_POST[quantity]','$_POST[price]','$_POST[status_id]','$_POST[order_id]'),
('$_POST[part_id2]','$_POST[quantity2]','$_POST[price2]','$_POST[status_id2]','$_POST[order_id2]'),
   ('$_POST[part_id3]','$_POST[quantity3]','$_POST[price3]','$_POST[status_id3]','$_POST[order_id3]');

INSERT INTO order (platform)
VALUE
('$_POST[platform]')";


mysql_query($sql1);

if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "record(s) added";

mysqli_close($con);

?>
2
  • You're executing $sql and not $sql1 Commented Mar 12, 2014 at 23:16
  • NEVER put $_POST data directly in a query. You must use proper escaping to avoid serious SQL injection bugs. It's not clear why you're mixing mysql_query and mysqli as mysqli has the bind_param method that neatly avoids all of this mess. Commented Mar 12, 2014 at 23:19

3 Answers 3

1

Try VALUES instead of VALUE in your second query.

Also, you don't seem to actually execute both queries in either of your examples. You should have something like:

if (!mysqli_query($con,$sql))
{
    die('Error: ' . mysqli_error($con));
}
if (!mysqli_query($con,$sql1))
{
    die('Error: ' . mysqli_error($con));
}
echo "record(s) added";

You should also consider wrapping the two executions in a transaction, so that if the 2nd insert fails the first will also be rolled back.

Sign up to request clarification or add additional context in comments.

Comments

0

First of all you need using mysql multi query.

$sql="INSERT INTO order_details (part_id, quantity, price, status_id,order_id)
  VALUES
  ('$_POST[part_id]','$_POST[quantity]','$_POST[price]','$_POST[status_id]','$_POST[order_id]'),
  ('$_POST[part_id2]','$_POST[quantity2]','$_POST[price2]','$_POST[status_id2]','$_POST[order_id2]'),
  ('$_POST[part_id3]','$_POST[quantity3]','$_POST[price3]','$_POST[status_id3]','$_POST[order_id3]');
  $sql.= INSERT INTO order (platform)
  VALUES
  ('$_POST[platform]')";


 if (mysqli_multi_query($link, $sql)) {
  //do action 
 }

Comments

0

Thank you for both of you. It worked and here is how I did it:

<?php
  include '../db/connect.php';
  $sql="INSERT INTO order_details (part_id, quantity, price, status_id,order_id)
  VALUES

 ('$_POST[part_id]','$_POST[quantity]','$_POST[price]','$_POST[status_id]','$_POST[order_id]'),
 ('$_POST[part_id2]','$_POST[quantity2]','$_POST[price2]','$_POST[status_id2]','$_POST[order_id2]'),
 ('$_POST[part_id3]','$_POST[quantity3]','$_POST[price3]','$_POST[status_id3]','$_POST[order_id3]')";

 $sql1="INSERT INTO order (platform)
 VALUES
 ('$_POST[platform]')";

 if (!mysqli_query($con,$sql))
 {
  die('Error: ' . mysqli_error($con));
 }
 if (!mysqli_query($con,$sql1))
 {
  die('Error: ' . mysqli_error($con));
 }
 echo "record(s) added";  
?>

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.