我只是一个初学者,我正在做一个项目(购物车)。用户可以在会话中将产品添加到购物车和产品存储的id中。当我使用这些it来反映DB的价格时,它就不起作用了。我正在使用PHP和MYSQL。这是我的密码
if(count($_SESSION['cart_items'])>0){
// getting the product ids
$nos = "";
foreach($_SESSION['cart_items'] as $no=>$value){
$nos = $nos . $no . ",";
}
// removing the last comma
$nos = rtrim($nos, ',');
//echo $nos; (will display like this INT VALUES 1,2,3,4)
$nos=mysql_real_escape_string($nos);
$site4->DBlogin();
$qry = "SELECT * FROM vendorproducts WHERE product_no IN('.implode(',',$nos).')";
$result = mysql_query($qry);
$row = mysql_fetch_assoc($result);
echo $row['price'];
}发布于 2015-07-22 17:15:26
PHP不能递归嵌入:
$qry = "SELECT * FROM vendorproducts WHERE product_no IN('.implode(',',$nos).')";
^---start of string end of string ---^由于您已经在字符串中,所以.implode(...)只是纯文本,而不是可执行代码。
这意味着您的查询是非法/无效的SQL,如果您进行了基本/最小错误检查,就会被告知如下:
$result = mysql_query($qry) or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^发布于 2015-07-22 17:52:17
我已经解决了这个问题,并感谢马克的建议。我犯了两个错误:-内爆输入字段不是数组。正如马克说的那样,这个查询是不可执行的。
所作的更改:
$nos = rtrim($nos, ',');
**$narray = array($nos);**
$fgmembersite4->DBlogin();
$qry = **'SELECT * FROM vendorproducts WHERE product_no IN ('.implode(',',$narray).')'**;
$result = mysql_query($qry) or die(mysql_error());
**while (**$row = mysql_fetch_assoc($result)){
echo $row['price'];}https://stackoverflow.com/questions/31569756
复制相似问题