以下代码返回错误,我不明白为什么。有人可以对我的情况有所了解。感谢。
我的订单表包含名为id,userID,amount,date的列。 var_dump都返回正确的值。
var_dump($_SESSION['u_id']);
var_dump($_SESSION['$s']);
$newOrder = $conn->prepare("INSERT INTO order (userID, amount) Values ('{$_SESSION['u_id']}','{$_SESSION['$s']}')");
$newOrder->execute();
完成错误消息
致命错误:未捕获错误:调用成员函数execute() boolean in /storage/ssd5/805/4077805/public_html/checkout.php:10 Stack 跟踪:#0 {main}被抛入 /storage/ssd5/805/4077805/public_html/checkout.php在线
答案 0 :(得分:0)
函数$conn->prepare()
未返回数据库对象,或者在调用时失败。检查您的SQL,您可以回显它并在db-console中运行以进行检查。
答案 1 :(得分:-1)
mysqli_stmt :: prepare在成功时返回TRUE,在失败时返回FALSE。 所以我猜测它在尝试准备查询时出错了。 我不认为你以正确的方式传递参数。我想你的意思是:
$conn->prepare("INSERT INTO order (userID, amount) Values (".$_SESSION['u_id'].",".$_SESSION['$s']".)")
但它不会保护你免受sql注入,所以我建议你看看http://php.net/manual/en/mysqli-stmt.bind-param.php以正确的方式做到这一点
此外,您应该在连接上运行execute,如下所示:
$conn->execute();