数据验证后,用于填充“客户”表中一行的sql查询将如下所示

mysqli_query($connect, "INSERT INTO Customers (CustFirst, CustLast, CustAddress)
VALUES ('$CustFirst', '$CustLast', '$CustAddress')")


现在可以正常工作了,但是“客户”表有4列,CustID是PK和AI。我是否必须按照以下方式进行新查询

mysqli_query($connect, "SELECT CustID from Customers
WHERE CustFirst='$CustFirst
AND CustLast='$CustLast'
AND CustAddress='$CustAddress')


只是为了检索插入表中的唯一行的CustID?还是有捷径?我想做一些事情

echo "The Customer " . $CustFirst . " " . $CustLast . " was added<br>";
echo "with " . $CustID . " being the number associated with the account.";


谢谢!

最佳答案

$CustID = mysqli_insert_id(connection);


您可以在执行插入查询后立即编写上述代码。这将返回插入数据库中的最新主键ID。

10-01 11:16