我如何在customer_information表中检索主键的值,以便可以将该值输入INSERT语句以将数据存储到我的customer_payment_information表中?
我想到了调用主键的值并将其存储在变量中,这样我就可以将变量输入到INSERT语句中,以存储我的customer_payment_information表的数据。
很遗憾,我的数据库中没有任何存储。
这是我的代码:
$sql = "SELECT customer_id FROM customer_information ORDER BY customer_id DESC LIMIT 1";
if ($conn->query($sql)){
$compile = $conn->prepare("INSERT INTO customer_payment_information (customer_id, fullName, creditcardNumber, expiry, ccv) VALUES (?, ?, ?, ?, ?)");
$compile->bind_param("issss", $sql, $ccname, $ccnumber, $expdate, $ccvnumber);
$compile->execute();
$compile->close();
$conn->close();
} else {
$errorMsg = "Connection failed: " . $conn->connect_error;
}
表1的主键具有auto_increment,因此是SQL查询
“从customer_information中选择customer_id来自customer_id DESC LIMIT 1”
您需要执行您的inital select语句并获得结果。这样的事情应该为您做。
$sql = "SELECT customer_id FROM customer_information ORDER BY customer_id DESC LIMIT 1";
$customerQuery = $conn->query($sql);
$customerResult = $customerQuery->fetch_assoc();
$customerId = $customerResult['customer_id'];
$compile
= $conn->prepare("INSERT INTO customer_payment_information (customer_id, fullName, creditcardNumber, expiry, ccv) VALUES (?, ?, ?, ?, ?)");
$compile->bind_param("issss", $customerId, $ccname, $ccnumber, $expdate, $ccvnumber);
$compile->execute();
$compile->close();
$conn->close();
您可能会在插入记录后考虑使用SELECT LAST_INSERT_ID()
之类的东西来获取其ID以备将来使用。