gpt4 book ai didi

php - MySQLI:in_array 不能正常工作

转载 作者:行者123 更新时间:2023-11-30 22:38:53 26 4
gpt4 key购买 nike

我目前正在创建一个演示注册系统(我会注意到这不是作业或家庭作业),如果用户已经购买了商品(并且项目)之前,它反射(reflect)在数据库中

    //current URL of the Page. cart_update.php redirects back to this URL
$current_url = base64_encode($url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);

//Query to get item information
$results = $mysqli->query("SELECT * FROM events ORDER BY eventid");// WHERE sort = '$sort'
$remaining = $mysqli->query("SELECT * FROM orders");
$obj_r = $remaining->fetch_object(); // obj_r means objects remaining

//Query to disable already purchaced items
$QgetSignups = $mysqli->query("SELECT s.eventid, o.status FROM signups s JOIN orders o ON s.orderid = o.orderid WHERE s.userid = $userid AND o.status = 1");
$signedup = array();

while($row = mysqli_fetch_assoc($QgetSignups))
//while($row = $QgetSignups->fetch_array(MYSQLI_NUM))
{
array_push($signedup,$row["eventid"]);
}

if ($results) {

//fetch results set as object and output HTML
while($obj = $results->fetch_object())
{
echo '<div class="product">';
echo '<form method="post" action="cart_update.php">';
echo '<div class="product-thumb"><img src="images/'.$obj->image.'"></div>';
echo '<div class="product-content"><h4>'.$obj->name.'</h4>';
echo '<div class="product-desc">'.$obj->description.'</div>';
echo '<div class="product-info">';
echo 'Price: '.$currency.$obj->price.' | ';
echo 'Items Left: '.$obj->max.' | ';
echo '<input type="hidden" style="width:20px;height:15px;" type="text" name="product_qty" value="1" size="3" />'; // OLD CODE TO DISPLAY QTY BOX: Qty: <input style="width:20px;height:15px;" type="text" name="product_qty" value="1" size="3" />';

if($signupAmount[$row['eventid']] >= $row['max'])
{
echo '<button class="add_to_cart">Add To Cart</button>';
}

if(in_array($row['eventid'],$signedup))
{
print_r($signedup);
//echo '<button DISABLED class="add_to_cart">Add To Cart</button>';
}
echo '</div></div>';
echo '<input type="hidden" name="product_code" value="'.$obj->name.'" />';
echo '<input type="hidden" name="type" value="add" />';
echo '<input type="hidden" name="return_url" value="'.$current_url.'" />';
echo '</form>';
echo '</div>';
}

}


?>

脚本当前尝试检查购买的商品是否与“eventid”(代表商品编号的数字)匹配。截至目前,我正试图通过将项目编号插入数组,然后使用 in_array 对其进行检查来实现这一点,但是如果我使用方法 while($row = mysqli_fetch_assoc ($QgetSignups))(它提取数组编号,但 in_array 没有正确检查它是否存在)并且如果我使用 //while($row = $QgetSignups->fetch_array(MYSQLI_NUM )),array_push 不收集值,但 in_array 方法尝试显示数组(尽管它是空的)。

最佳答案

您没有在存在以下代码的当前 while 循环中迭代 $row。

if (in_array($row['eventid'], $signedup)) {
print_r($signedup);
//echo '<button DISABLED class="add_to_cart">Add To Cart</button>';
}

我不确定你的数据库模式是如何设置的,但也许应该是这样的:

if (in_array($obj->eventid, $signedup)) {
print_r($signedup);
//echo '<button DISABLED class="add_to_cart">Add To Cart</button>';
}

关于php - MySQLI:in_array 不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31643082/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com