Reputation: 13
Please advice how do I insert the data to two table in database using php. Below is the code, currently I only able to insert the data for $insert_cust but unable to insert the values from for loop.
<?php
//including the database connection file
include_once("config_db.php");
//getting data from the fields
if(isset($_POST['Submit']))
{
$cust_name=$_POST['cust_name'];
$cust_add=$_POST['cust_add'];
//insert data to database tb_cust
$insert_cust=mysql_query("INSERT INTO tb_cust(cust_id,cust_name,cust_add) VALUES(NULL,'$cust_name','$cust_add')");
mysql_query($insert_cust);
$lastid=mysql_insert_id();
//insert data to database tb_ord_dtl
for($i=0; $i < count($_POST['tm_id']); $i++) {
$tm_id = addslashes($_POST['tm_id'][$i]);
$order_qty = addslashes($_POST['order_qty'][$i]);
$order_unit_prc = addslashes($_POST['order_unit_prc'][$i]);
//insert data to database tb_odr_dtl
echo "INSERT INTO tb_odr_dtl(order_id,cust_id,tm_id,order_qty,order_unit_prc) VALUES(NULL,$lastid,'$tm_id','$order_qty','$order_unit_prc')";
}
}
?>
Upvotes: 0
Views: 686
Reputation: 4637
In forloop
you need not to use echo
there
for($i=0; $i < count($_POST['tm_id']); $i++) {
$tm_id = addslashes($_POST['tm_id'][$i]);
$order_qty = addslashes($_POST['order_qty'][$i]);
$order_unit_prc = addslashes($_POST['order_unit_prc'][$i]);
//insert query here
$Insert_query=mysql_query("INSERT INTO tb_cust(cust_id,cust_name,cust_add) VALUES(NULL,'$cust_name','$cust_add')");
}
Upvotes: 0
Reputation: 14467
$insert_cust=mysql_query("INSERT INTO tb_cust(cust_id,cust_name,cust_add) VALUES(NULL,'$cust_name','$cust_add')");
mysql_query($insert_cust);
Why did you run mysql_query twice? As you try to run mysql_query
again which is nonsense, you can't get a valid insert id I think.
At the end you use echo
to insert?
Upvotes: 3