[英]How to insert data in multiple tables using a prepared statement in PHP?
I have two tables which are billing_info, shipping_info
. 我有两个表,分别是billing_info, shipping_info
。 billing_id is Primary key and billing_id is a foreign key in shipping_info. billing_id是主键,而billing_id是shipping_info中的外键。
I have billing_info address form. 我有billing_info地址表格。 The user will fill the billing info, After filling info there is a checkbox which is called as shipping address is different
. 用户将填写账单信息,填写完信息后,将有一个复选框,该复选框称为“ shipping address is different
。 If any user checked that checkbox then it will ask for the shipping information. 如果有任何用户选中了该复选框,则它将询问运输信息。 After filling the shipping information according to the data billing info will store in the billing_info table and shipping_info will store in the shipping info table. 根据数据填写运输信息后,开票信息将存储在billing_info表中,而shipping_info将存储在运输信息表中。
If the user is not checked the checkbox than only billing info will store in the billing info table. 如果未选中用户复选框,则仅账单信息将存储在账单信息表中。
<input type="checkbox" name="shipping_status" value="1">
I am able to insert the data in the database only for billing_info if the user does not check the checkbox but not able to insert the data after checked the checkbox. 如果用户未选中该复选框,但在选中该复选框后无法插入数据,则只能将数据插入billing_info的数据库中。 There might be some issue with the query. 查询可能存在问题。 I tried query is 我试过查询是
1) START TRANSACTION;
INSERT INTO table1 (column name)VALUES (values);
INSERT INTO table2 (column name) VALUES (values);
COMMIT;
2) BEGIN;
INSERT INTO table1 (column name)VALUES (values);
INSERT INTO table2 (column name) VALUES (values);
COMMIT;
but still not able to insert the data. 但仍无法插入数据。 Would you help in out in this? 您会帮忙吗?
is this right way to store int user information? 这是存储int用户信息的正确方法吗?
if (isset($_POST['submit'])){
$b_firstname=$conn->real_escape_string(trim($_POST['b_firstname']));
$b_lastname=$conn->real_escape_string(trim($_POST['b_lastname']));
$b_address =$conn->real_escape_string(trim($_POST['b_address']));
$b_country=$conn->real_escape_string(trim($_POST['b_country']));
$b_state=$conn->real_escape_string(trim($_POST['b_state']));
$b_city=$conn->real_escape_string(trim($_POST['b_city']));
$b_pincode=$conn->real_escape_string(trim($_POST['b_pincode']));
$b_mobileno=$conn->real_escape_string(trim($_POST['b_mobileno']));
//$login_user_id=$conn->real_escape_string(trim($_POST['login_user_id']));
//echo $shipping_status=$conn->real_escape_string(trim($_POST['shipping_status']));
/*Shipping address*/
$s_firstname=$conn->real_escape_string(trim($_POST['s_firstname']));
$s_lastname=$conn->real_escape_string(trim($_POST['s_lastname']));
$s_address =$conn->real_escape_string(trim($_POST['s_address']));
$s_country=$conn->real_escape_string(trim($_POST['s_country']));
$s_state=$conn->real_escape_string(trim($_POST['s_state']));
$s_city=$conn->real_escape_string(trim($_POST['s_city']));
$s_pincode=$conn->real_escape_string(trim($_POST['s_pincode']));
$s_mobileno=$conn->real_escape_string(trim($_POST['s_mobileno']));
if (empty($conn->real_escape_string(trim($_POST['shipping_status'])))) {
$shipping_status=0;
}
else{
$shipping_status=1;
}
if ($shipping_status==0) {
echo $sql="INSERT INTO billing_info (b_firstname, b_lastname, b_address, b_country, b_state, b_city, b_pincode, b_mobileno,login_user_id,shipping_status, b_date_of_added) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
// print_r($sql);
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssssiiiis", $b_firstname, $b_lastname, $b_address, $b_country, $b_state, $b_city, $b_pincode, $b_mobileno,$login_user_id=1,$shipping_status, $date_of_added);
}
else{
// prepare and bind
echo $sql="INSERT INTO billing_info (b_firstname, b_lastname, b_address, b_country, b_state, b_city, b_pincode, b_mobileno,login_user_id,shipping_status, b_date_of_added) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);INSERT INTO shipping_info (s_firstname, s_lastname, s_address, s_country, s_state, s_city, s_pincode, s_mobileno,b_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssssiiiisssssssiii", $b_firstname, $b_lastname, $b_address, $b_country, $b_state, $b_city, $b_pincode, $b_mobileno,$login_user_id=1,$shipping_status, $date_of_added,$s_firstname, $s_lastname, $s_address, $s_country, $s_state, $s_city, $s_pincode, $s_mobileno,LAST_INSERT_ID());
}
$stmt->execute();
$stmt->close();
}
//redirect code
//header('Location: contactus.php');
$conn->close();
}
By default, every time you want to insert billing info so Insert query for billing info should not be in if statement. 默认情况下,每次您要插入帐单信息时,因此对帐单信息的插入查询不应位于if语句中。
$sql="INSERT INTO billing_info (b_firstname, b_lastname, b_address, b_country, b_state, b_city, b_pincode, b_mobileno,login_user_id,shipping_status, b_date_of_added) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
// print_r($sql);
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssssiiiis", $b_firstname, $b_lastname, $b_address, $b_country, $b_state, $b_city, $b_pincode, $b_mobileno,$login_user_id=1,$shipping_status, $date_of_added);
$stmt->execute();
$stmt->close();
For Shipping status is selected 选择运输状态
if ($shipping_status==1) {
$sql="INSERT INTO shipping_info (b_firstname, b_lastname, b_address, b_country, b_state, b_city, b_pincode, b_mobileno,login_user_id,shipping_status, b_date_of_added) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
// print_r($sql);
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssssiiiis", $b_firstname, $b_lastname, $b_address, $b_country, $b_state, $b_city, $b_pincode, $b_mobileno,$login_user_id=1,$shipping_status, $date_of_added);
$stmt->execute();
$stmt->close();
}
if (empty($conn->real_escape_string(trim($_POST['shipping_status'])))) {
$shipping_status=0;
$sql="INSERT INTO billing_info (b_firstname, b_lastname, b_address, b_country, b_state, b_city, b_pincode, b_mobileno,login_user_id,shipping_status, b_date_of_added) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?)";
// print_r($sql);
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssssiiiis", $b_firstname, $b_lastname, $b_address, $b_country, $b_state, $b_city, $b_pincode, $b_mobileno,$login_user_id=1,$shipping_status, $date_of_added);
}
else{
$shipping_status=1;
$sql="INSERT INTO billing_info (b_firstname, b_lastname, b_address, b_country, b_state, b_city, b_pincode, b_mobileno,login_user_id,shipping_status, b_date_of_added) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?);INSERT INTO shipping_info (s_firstname, s_lastname, s_address, s_country, s_state, s_city, s_pincode, s_mobileno,b_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssssssiiiisssssssiii", $b_firstname, $b_lastname, $b_address, $b_country, $b_state, $b_city, $b_pincode, $b_mobileno,$login_user_id=1,$shipping_status, $date_of_added,$s_firstname, $s_lastname, $s_address, $s_country, $s_state, $s_city, $s_pincode, $s_mobileno,LAST_INSERT_ID());
}
After bind_params
you have to insert this 在bind_params
之后,您必须插入
$firstname = "John";
$lastname = "Doe";
$email = "john@example.com";
$stmt->execute();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.