如何通过url success.php?id =立即发送所有商品ID

时间:2017-07-05 10:14:42

标签: php html mysql mysqli

当我在购物车中添加一些商品时,我无法将数据库中的状态从“添加到购物车”更改为“已确认”

<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<?php
require 'includes/common.php';

if (!isset($_SESSION['email']))
{
    header('location: index.php');
}
?>
<html>
    <head>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
    <!--jQuery library--> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!--Latest compiled a>
        <meta charset="UTF-8">nd minified JavaScript--> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link href="style.css" rel="stylesheet" type="text/css"/>
        <title>Lifestyle Store | Products</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
    </head>
    <body >
          <?php
        include 'includes/header.php';
        ?>
        <?php
        $user_id=$_SESSION['id'];
        //changed from above user_id to $_getc ['user_id']
        $items_query="SELECT items.id,items.name,items.price from items INNER JOIN user_items INNER JOIN users ON items.id=user_items.item_id AND user_items.user_id=users.id WHERE user_items.user_id= $user_id AND user_items.status='Added To Cart'";
        
        // chaged from select * from user_items inner join items on user_items.item_id=items.id where items.id=$user_id";
        
        $items_query_result= mysqli_query($con, $items_query) or die(mysqli_error($con));
        
        
        
        ?>
        
       
       
        
       <?php  if (mysqli_num_rows($items_query_result)==0){ ?> 
       <?php echo 'Add something in cart';  }  else  { ?>
        <div class="container">
             <div class="row decor_bg">
                <div class="col-md-6 col-md-offset-3">
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                
                             
                             
                                <th>Item Number</th>
                                <th>Item Name</th>
                                <th>Price</th>
                                <th>Total</th>
                                
                            </tr>
                        </thead>
                         
                        <tbody>
                            <?php 
                           
                                $total=0;
                                    while ($row= mysqli_fetch_array($items_query_result)) {  
                                        $total+=$row['price'];
                                        
                                          
                                                                                             ?>
                        
                            <tr>
                                
                                    <td><?php echo $row['id']  ?> </td> <td >  <?php echo $row['name'];   ?> <a href="cart-remove.php?id=<?php echo $row['id']; ?>" class='remove_item_link ' style="color:#e53939">Remove </a> </td>   <td> Rs. <?php   echo $row['price']; } ?>  </td><td> Rs. <?php echo $total;?>  <a href="success.php?id=" class='btn btn-primary'>Confirm Order</a></td>
                            </tr>
                        
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
                             <?php }?>
       
             <?php 
        include 'includes/footer.php';
        ?>
        
    </body>
</html>

  1. 这是购物车的代码
  2. 用户在购物车中添加一些
  3. **然后当用户进入购物车时,购物车会显示所有带有ID,名称和价格的物品**
  4. 最后总价
  5. **我已经定义了一个while循环来总结价格并显示id名称等**
  6. 现在我想将项目ID发送到success.php,将数据库中的状态从“添加到购物车”更改为“已确认”

2 个答案:

答案 0 :(得分:0)

[已更新] 您可以将ID存储在一个变量中。

 <?php         
     $total=0;
     $ids = '';
     while ($row= mysqli_fetch_array($items_query_result)) {  
         $total+=$row['price'];
         if(strlen($ids)== 0){
            $ids = $row['id'];
         }else{
           $ids = ','.$row['id'];
         }
 ?>       
  <tr>

      <td><?php echo $row['id']  ?> </td>
      <td >  <?php echo $row['name'];   ?>
             <a href="cart-remove.php?id=<?php echo $row['id']; ?>" class='remove_item_link ' style="color:#e53939">
                 Remove 
             </a> 
      </td> 
      <td> Rs. <?php   echo $row['price']; } ?>  </td>
      <td> Rs. <?php echo $total;?>  
          <a href="success.php?id=<?php echo $ids;?>" class='btn btn-primary'>
            Confirm Order
          </a>
      </td>

然后在您的success.php中,您可以像这样展开$ids

$ids = explode(",",$_GET['id'])); //with explode you can get splits the ids

for($i=0; $i<count($ids); $i++){
   $final_id = $ids[$i];
  //then here you can do the update sql query then the row id is the `final_id` in every loop you will update the database.
}
希望它会有所帮助!

答案 1 :(得分:0)

<tbody>
<?php 
    $total=0;
    while ($row= mysqli_fetch_array($items_query_result)) {  
        $total+=$row['price'];  
        echo "<tr>";
        echo "<td>";
            echo $row['id'];
        echo "</td>";
        echo "<td>";
            echo $row['name'];
        echo '<a href="cart-remove.php?id='.$row['id'].'" class='remove_item_link ' style="color:#e53939">Remove </a>'; 
        echo "</td>";
        echo "<td>";
            echo "Rs ".$row['price'];
        echo "</td>";
        echo "<td>";
            echo "Rs ".$total;
            echo '<a href="success.php?id=" class='btn btn-primary'>Confirm Order</a>';
        echo "</td>";
        echo"</tr>";
     } ?>  
</tbody>

试试这个它会起作用,我已经纠正了你的迭代结束。通过它。