我一直在努力解决这个问题。当客户下订单时,有没有办法让“user_id”从我的“用户”表格到我的“订单”表中?
使用第二个SQL语句时,在“INSERT INTO”语句之后,系统仍然说订单已成功。但是,当我检查我的数据库时,甚至没有插入订单。
如果不使用第二个SQL语句,将正确插入订单,除了“user_id”之外的每个字段都放在表中。
我也试过“INSERT INTO table2(column1,column2,column3,...)SELECT column1,column2,column3,... FROM table1 WHERE condition;”声明和那也行不通。
如果只编写了一个SQL命令,它会起作用吗?如果是这样,我不知道该怎么做。
非常感谢任何帮助。
<?php
if (isset($_POST['submit'])) {
include_once 'dbh.inc.php';
$type = mysqli_real_escape_string($conn, $_POST['type']);
$square_ft = mysqli_real_escape_string($conn, $_POST['square_ft']);
$materials = mysqli_real_escape_string($conn, $_POST['materials']);
$time= mysqli_real_escape_string($conn, $_POST['time']);
$date= mysqli_real_escape_string($conn, $_POST['date']);
$price= mysqli_real_escape_string($conn, $_POST['price']);
if (empty($price) || empty($time) || empty($date) || empty($type) || empty($square_ft) || empty($materials)) {
header("Location: ../placeorder.php?order=empty");
exit();
} else {
//Insert orders into database
$sql = "INSERT INTO orders (price, time, date, type, square_ft, materials,) VALUES('$price', '$time', '$date', '$type', '$square_ft','$materials');";
$sql = "INSERT INTO orders (user_id) FROM users;";
mysqli_query($conn, $sql);
header("Location: ../orderform.php?order=success");
exit();
}
}
?>
<?php
include_once 'footer.php';
?>
答案 0 :(得分:0)
要从用户获取密钥,您需要在用户字段上执行SELECT
,然后将用户密钥插入订单。它会是这样的:
$sql1 = "SELECT user_id FROM users_table WHERE username='someusername'";
$userid = mysqli_fetch_assoc(mysqli_query($conn, $sql1));
$sql2 = "INSERT INTO orders (price, time, date, type, square_ft, materials, user_id) VALUES('$price', '$time', '$date', '$type', '$square_ft','$materials', '$userid');";
mysqli_query($conn, $sql1);