0

当我让它工作时我很高兴,但它会提交两次订单,除非我注释掉最后两行。有没有更好的方法来编写它,这样我就不会重复?

$sql = "INSERT INTO orders (weight, shipper, shipacct) VALUES ('$weight', '$shipper', '$shipacct')";
$conn->query($sql);
$recordid = $conn->insert_id;

我这样做是因为我试图使用记录 ID 作为订单 ID。我在购买收据上将此订单 ID 回显给客户。

更新代码:

$sql = "INSERT INTO orders (weight, shipper, shipacct) VALUES ('$weight', '$shipper', '$shipacct')";
$recordid = mysql_insert_id();

没有重复,但不返回记录 ID。

4

1 回答 1

0

警告

This extension is deprecated as of PHP 5.5.0, and will be removed in the future.
Instead, the MySQLi or PDO_MySQL extension should be used.
See also MySQL: choosing an API guide and related FAQ for more information.
Alternatives to this function include:
mysqli_insert_id()
PDO::lastInsertId()

尝试以下操作:

$sql = "INSERT INTO orders (weight, shipper, shipacct) VALUES ('$weight', '$shipper', '$shipacct')";
$conn->query($sql);
$recordid = mysql_insert_id();

注意:
因为 mysql_insert_id() 作用于最后执行的查询,所以确保在生成值的查询之后立即调用 mysql_insert_id()。

希望会有所帮助......

于 2014-12-08T18:44:50.827 回答