[英]how to append id's separated by comma?
I am building an E-commerce website and for my cart.php
page I want to collect all the ids of the items which is added to cart and then send these ids through URL to my success.php
so that here I can retrieve these ids and update my database status to confirmed for particular user. 我正在建立一个电子商务网站,对于我的
cart.php
页面,我想收集添加到购物车中的商品的所有ID,然后通过URL将这些ID发送到我的success.php
以便在这里可以检索这些ID。并更新我的数据库状态以针对特定用户进行确认。 I tried using array for collecting ids but then i dont know how to write sql query to update table using array, i also tried implode()
function but then i am getting error in query. 我尝试使用数组来收集ID,但后来我不知道如何编写SQL查询来使用数组更新表,我也尝试了
implode()
函数,但随后却在查询中出错。 I need code for collecting ids, sending these ids through URL, retrieving the ids, and query to update. 我需要用于收集ID,通过URL发送这些ID,检索ID以及查询更新的代码。 My Code:
我的代码:
<?php
$result = mysqli_query($con, $query);
$num_rows = mysqli_num_rows($result);
$ids = array();
while ($num_rows > 0) {
$row = mysqli_fetch_array($result);
$ids[] = $row['item_id'];
$num_rows--;
}
$id = implode(', ', $ids);?>
<td><a href="success.php?id=<?php echo $id; ?>" class="btn btn-
primary">Confirm Order</a></td>
Code to send the IDs 发送ID的代码
<?php
$result = mysqli_query($con, $query);
$num_rows = mysqli_num_rows($result);
$ids=array();
while ($num_rows > 0) {
$row = mysqli_fetch_array($result);
$ids[]=$row['item_id'];
$num_rows--;
}
$id= implode(',', $ids);?>
<td><a href="success.php?id=<?php echo $id; ?>" class="btn btn-
primary">Confirm Order</a></td>
Code to update the DB using the IDs, based on the snippet you shared in the comments 根据您在注释中共享的片段,使用ID使用ID更新数据库的代码
<?php
$item_id=$_GET['id'];
$user_id=$_SESSION['id'];
$query="UPDATE users_items SET status='Confirmed' WHERE item_id='$item_id' AND user_id IN (".$item_id.")";
// Debug-Query Formation: //echo $query;
$result= mysqli_query($con, $query);
?>
Hope this helps 希望这可以帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.