>变量被添加到从另一个页面传递的会话数组中
详细信息页面在此处
<?php
session_start();
require_once 'core/init.php';
include 'includes/header.php';
include 'includes/navigation.php';
// cart code
if(empty($_SESSION['cart'])) {
$_SESSION['cart'] = array();
}
array_push($_SESSION['cart'], $_GET['id']);
购物车页面在这里
这是一些示例产品的输出
array(5) { [0]=> string(4) "3911" [1]=> string(4) "5005" [2]=> NULL [3]=> string(4) "3393" [4]=> string(3) "185" }
和当前 SQL 查询
SELECT * FROM `wheels` WHERE `recid` LIKE (3911, 5005, , 3393, 185)
所以我知道数组正确获取值(Bar 为 null)
session_start();
require_once 'core/init.php';
include 'includes/header.php';
var_dump($_SESSION['cart']);
$whereIn = implode(', ', $_SESSION['cart']);
// fetching products for cart
$cartQuery = "SELECT * FROM `wheels` WHERE `recid` = ($whereIn)";
$cartResult = mysqli_query($db, $cartQuery);
echo $cartQuery;
尝试调用 Sql 结果时
<?php while($product = mysqli_fetch_assoc($cartResult)) : ?>
我收到错误
Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in (blah blah directory) on line 121
从数组中筛选空值的最佳做法是什么? 例如,数组没有传递空值(当他们单击购物篮按钮时发生)。
有人可以解释为什么 while 循环无法获得结果,我假设这是因为 mysql 查询没有向结果变量返回结果?如果是这样,应该使用什么语法将 $_SESSION 传递到查询中以获得多个结果?
对不起,这么多问题。
您不能运行这样的查询:
SELECT * FROM `wheels` WHERE `recid` LIKE (3911, 5005, , 3393, 185)
您应该删除空节点并使用 IN 而不是类似:
SELECT * FROM `wheels` WHERE `recid` IN(3911, 5005, 3393, 185)
删除空元素:
$newArray =array();
foreach($array as $key => $value)
{
if($value != null)
$newArray[] = $value;
}
print_r($newArray);
在你的 SQL 查询"SELECT * FROM wheels WHERE recid = ($whereIn)"
中,你使用了 =
而不是 LIKE
。请检查一下。还要检查一次$cartQuery
你回声。