无法删除而不会出现“未定义的偏移”错误

时间:2018-12-05 11:49:51

标签: php mysqli

我正在为我的任务做一个非常简单的项目,这与购物车有关。我通过将SESSION与数组和索引一起使用来创建购物车页面,以跟踪用户的订单。问题是,每当我从购物车列表中删除一餐时,都会出现错误,例如“ Notice:Undefined offset:1”,“ Notice:Undefined offset:2”等。我在这里附加代码:

这是我的购物车页面。该错误表明它与我已评论的行有关:

<form method="POST" action="save_cart.php">
<table class="table">
<tbody>

<thead>
      <th></th>
      <th>Name</th>
      <th>Price</th>
      <th>Quantity</th>
      <th>Subtotal</th>
</thead>


  <?php
        //initialize total
        $total = 0;
        if(!empty($_SESSION['cart'])){

        //create array of initail qty which is 1
        $index = 0;
        if(!isset($_SESSION['qty_array'])){
          $_SESSION['qty_array'] = array_fill(0, count($_SESSION['cart']), 1);
        }
        $sql = "SELECT * FROM meal WHERE meal_id IN (".implode(',',$_SESSION['cart']).")";
        $query = $conn->query($sql);
          while($row = $query->fetch_assoc()){
            ?>

          <tr>

              <td class="removebtn" style="text-align: center;">
                <a href="delete_item.php?id=<?php echo $row['meal_id']; ?>&index=<?php echo $index; ?>" >Remove</a>
              </td>
              <td><?php echo $row['meal_name']; ?></td>
              <td>RM <?php echo number_format($row['meal_price'], 2); ?></td>
              <input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
              <td class="qtyinput" ><input type="number" min="1" max="5" class="form-control" value="<?php echo $_SESSION['qty_array'][$index]; ?>" name="qty_<?php echo $index; ?>"></td>

              //THE ERROR IS HERE

              <td>RM <?php echo number_format($_SESSION['qty_array'][$index]*$row['meal_price'], 2); ?></td>
              <?php $total += $_SESSION['qty_array'][$index]*$row['meal_price']; ?>
            </tr>
            <?php
            $index ++;
          }
        }
        else{
          ?>
          <tr>
            <td colspan="5" class="text-center"  style="text-align: center;">No Meal in Cart</td>
          </tr>
          <?php
        }

      ?>
      <tr>
        <td colspan="4" align="left"><b>Total</b></td>
        <td><b>RM <?php echo number_format($total, 2); ?></b></td>
      </tr>
</tr>

  

这是我的删除页面:

session_start();

//remove the id from our cart array
$key = array_search($_GET['meal_id'], $_SESSION['cart']);   
unset($_SESSION['cart'][$key]);

unset($_SESSION['qty_array'][$_GET['index']]);
//rearrange array after unset
$_SESSION['qty_array'] = array_values($_SESSION['qty_array']);

$_SESSION['message'] = "<script>alert('Meal is deleted from your cart');</script>";
header('location: cart.php');

我真的需要你的帮助。预先感谢!

1 个答案:

答案 0 :(得分:1)

它是一个索引错误,因为数组中不存在索引,请用此代码替换while循环内的html php代码。

<tr>

<td class="removebtn" style="text-align: center;">
<a href="delete_item.php?id=<?php echo $row['meal_id']; ?>&index=<?php echo $index; ?>" >Remove</a>
</td>
<td><?php echo $row['meal_name']; ?></td>
<td>RM <?php echo number_format($row['meal_price'], 2); ?></td>
<input type="hidden" name="indexes[]" value="<?php echo $index; ?>">
<td class="qtyinput" ><input type="number" min="1" max="5" class="form-control" value="<?php echo (isset($_SESSION['qty_array'][$index]) ? $_SESSION['qty_array'][$index] : 0); ?>" name="qty_<?php echo $index; ?>"></td>

//THE ERROR IS HERE

<td>RM <?php 
$qty = (isset($_SESSION['qty_array'][$index]) ? $_SESSION['qty_array'][$index] : 0);
echo number_format($qty *$row['meal_price'], 2); ?></td>
<?php $total += $qty*$row['meal_price']; ?>
</tr>