[英]Problem: Call to a member function fetch_assoc()
im trying to figure out what's wrong with this code.我想弄清楚这段代码有什么问题。 im getting the Call to a member function fetch_assoc() error but I can't figure out why
我正在调用成员函数 fetch_assoc() 错误,但我不知道为什么
for the record - im trying to build an eCommerce site for a project in college and with the following code im trying to reduce the item stock when a purchase is made.为了记录 - 我正在尝试为大学项目建立一个电子商务网站,并使用以下代码尝试在购买时减少物品库存。
Just to mention - I executed the SELECT query in PhpMyAdmin and it worked顺便提一下 - 我在 PhpMyAdmin 中执行了 SELECT 查询并且它起作用了
<?php
require 'config.php';
session_start();
$sql = "SELECT * FROM cart,product WHERE product.product_code = cart.cart_product_code";
$result = mysqli_query($conn, $sql);
if ($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$stock=$row['product_stock']-$row['quantity'];
$id_stock=$row['cart_product_code'];
$sql= "UPDATE product SET product_stock='$stock' WHERE product_code='$id_stock'";
$result = mysqli_query($conn, $sql);
$query="TRUNCATE TABLE Cart";
$clear= mysqli_query($conn, $query) or die("Invalid query");
}
}
You are overriding the $result object inside the while loop by querying an update query.您通过查询更新查询来覆盖 while 循环内的 $result 对象。 You could execute the query without setting any variable :
您可以在不设置任何变量的情况下执行查询:
$sql= "UPDATE product SET product_stock='$stock' WHERE product_code='$id_stock'";
mysqli_query($conn, $sql);
Also, I strongly suggest you to use prepared statement to avoid any form of SQL injection .另外,我强烈建议您使用准备好的语句来避免任何形式的SQL 注入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.