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);
?>
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.
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
}
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";
?>