我正在尝试在tbl_ordetails中插入数据然后更新tbl_cart中的数据,但是当我尝试插入数据时,结果总是“失败” ,我不知道是什么问题。请帮帮我
<?php
include ("connection.php");
if(isset($_POST['btnSubmit']))
{
$fullname=$_POST['fullname'];
$address=$_POST['address'];
$phone_number=$_POST['phone_number'];
$city=$_POST['city'];
$customer=$_POST['customer'];
$query = "INSERT INTO tbl_orderdetails (fullname, address, phone_number, city, customer) VALUES ('$fullname, $address, $phone_number, $city, $customer')";
$query1 = "UPDATE tbl_cart SET status ='Ordered' WHERE customer=['$customer']";
$result = mysqli_query($conn, $query);
if(($conn->query($query1) === TRUE) && ($result->num_rows > 0)){
echo "success";
exit;
}
else{
echo "failed";
exit;
}
}
?>
提前致谢
答案 0 :(得分:1)
您在...中缺少引号?
$query = "INSERT INTO tbl_orderdetails (fullname, address, phone_number, city, customer)
VALUES ('$fullname, $address, $phone_number, $city, $customer')";
它正在获取所有值并构建1值。应该是
$query = "INSERT INTO tbl_orderdetails (fullname, address, phone_number, city, customer)
VALUES ('$fullname', '$address', '$phone_number', '$city', '$customer')";
您的更新应为(减[])
$query1 = "UPDATE tbl_cart SET status ='Ordered' WHERE customer='$customer'";
更好的方法是使用预处理语句并绑定变量。
<强>更新强>
$result = $conn->query($query);
if ( $result === false ) {
echo "error:".$conn->error;
exit;
}
if($conn->query($query1) === TRUE){
echo "success";
exit;
}
else{
echo "failed:".$conn->error;
exit;
}
答案 1 :(得分:0)
我认为你的问题在于这行代码 if(($ conn-&gt; query($ query1)=== TRUE)&amp;&amp;($ result-&gt; num_rows&gt; 0)){
您实现了行计数<?php
include ("connection.php");
if(isset($_POST['btnSubmit']))
{
$fullname=$_POST['fullname'];
$address=$_POST['address'];
$phone_number=$_POST['phone_number'];
$city=$_POST['city'];
$customer=$_POST['customer'];
$query = "INSERT INTO tbl_orderdetails (fullname, address, phone_number, city, customer) VALUES ('$fullname, $address, $phone_number, $city, $customer')";
$query1 = "UPDATE tbl_cart SET status ='Ordered' WHERE customer=['$customer']";
$result = mysqli_query($conn, $query);
if($result){
echo "success";
exit;
}
else{
echo "failed";
exit;
}
}
?>
,但我没有看到您在哪里使用 sql select query statement ,那么您在哪里获得行计数。它是否应该在插入数据时或从数据库中选择数据时使用。
我不是mysqli粉丝而是PDO
你可以尝试这样的事情
{{1}}
我可以使用PDO更轻松地重写代码
同样,您的脚本未经过清理或验证,您很容易受到注入,并且可能是缓冲区溢出攻击