2016-04-01 18 views
0

Alışveriş sepetimle çok işim bitti. Sepeti güncelleyebiliyorum, ancak öğenin yanındaki sil düğmesini tıkladığımda boş bir sayfaya yönlendiriliyor.Alışveriş sepetinde silme düğmesi çalışmıyor

<?php 
    session_start(); 
     include ("db_connection.php");//include database connection 
     if (isset($_SESSION['cart'])) { 
     if(isset($_GET['ebook_id']) && !isset($_POST['update'])){ 
      $statement=$_SESSION['cart']; 
      $find=false; 
      $number=0; 
     for($i=0;$i<count($statement);$i++){ 
     if($statement[$i]['ebook_id']==$_GET['ebook_id']){ 
      $find=true; 
      $number=$i; 
     } 
     } 
     if($find==true){ 
     $statement[$number]['quantity']=$statement[$number]['quantity']+1; 
     $_SESSION['cart']=$statement; 
     }else{ 
     $ebook_title=""; 
     $price=0; 
     $ebook_image=""; 
     $query=mysqli_query($con,"SELECT * FROM ebooks WHERE ebook_id=".$_GET['ebook_id']); 
     while ($row=mysqli_fetch_array($query,MYSQLI_ASSOC)) { 
      $ebook_title=$row['ebook_title']; 
      $price=$row['price']; 
      $ebook_image=$row['ebook_image']; 
     } 
     $newData=array('ebook_id'=>$_GET['ebook_id'], 
       'ebook_title'=>$ebook_title, 
       'price'=>$price, 
       'ebook_image'=>$ebook_image, 
       'quantity'=>1); 

     array_push($statement, $newData); 
     $_SESSION['cart']=$statement; 

     } 
     } 
    }else{ 
     if(isset($_GET['ebook_id'])){ 
     $ebook_title=""; 
     $ebook_image=""; 
     $price=0; 
     $query=mysqli_query($con,"SELECT * FROM ebooks WHERE ebook_id=".$_GET['ebook_id']); 
     while ($row=mysqli_fetch_array($query,MYSQLI_ASSOC)) { 
     extract ($row); 
     $ebook_title=$row['ebook_title']; 
     $ebook_image=$row['ebook_image']; 
     $price=$row['price']; 
     } 
     $statement[]= array('ebook_id' => $_GET['ebook_id'],'ebook_title' => $ebook_title,'ebook_image' => $ebook_image,'price' => $price,'quantity' => 1); 

    $_SESSION['cart']=$statement; 
    } 
    } 
    ?> 

Bu benim sepeti biçimidir:: Bu benim cart.php tepesi Nihayet

<form method="POST"> 
          <table class="table"> 
           <thead> 
            <tr> 
             <th colspan="2">E-book</th> 
             <th>Price</th> 
             <th>Quantity</th> 
             <th>Subtotal</th> 
             <th>Remove</th> 
            </tr> 
           </thead> 
           <tbody> 

        <tr> 
         <?php 
         if (isset($_POST['update'])){ 
         $arrquantity=$_POST['quantity']; 
         //check validate quantity 
         $valid = 1; 
         for ($i=0; $i < count($arrquantity); $i++) { 
           if (!is_numeric($arrquantity[$i]) || $arrquantity[$i] < 1) { 
            $valid=0; 
            break; 
           } 

         } 
         if ($valid == 1){ 
         $cart = unserialize (serialize($_SESSION['cart'])); 
         for ($i=0;$i<count($cart);$i++) { 
           $cart[$i]['quantity']=$arrquantity[$i]; 
         } 
         }else{ 
         $error= "Quantity is invalid"; 
         } 
         $_SESSION['cart']=$cart; 
         } 
         //delete ebook in cart 


         $total=0; 
         if (isset($_SESSION['cart'])) { 
          $data=$_SESSION['cart']; 
          $total=0; 
          for ($i=0;$i<count($data);$i++) { 
          ?> 
          <?php echo isset($error) ? $error :'';?> 
          <div class="ebook"> 
          <td><a href=""><img src="<?php echo $data[$i]['ebook_image'];?>"></a></td> 
          <td><?php echo $data[$i]['ebook_title'];?></td> 
          <td>£<?php echo $data[$i]['price'];?></td> 
          <td><input type="text" value="<?php echo $data[$i]['quantity'];?>" data-price="<?php echo $data[$i]['price'];?>" data-id="<?php echo $data[$i]['ebook_id'];?>" class="quantity" name="quantity"></td> 
          <td class="subtotal">£<?php echo $data[$i]['price']*$data[$i]['quantity'];?></td> 
          <td><a href="cart.php?ebook_id=<?php echo $data[$i]['ebook_id'];?>&action =delete" onclick="return confirm('Are you sure?')" class="delete" data-id="<?php echo $data[$i]['ebook_id'];?>"><i class="fa fa-trash-o"></i></a> 
          </div> 
        </tr> 

         <?php 
         $total=($data[$i]['price']*$data[$i]['quantity'])+$total;  
          } 
         }else{ 
          echo "<p class='text-muted'>Your shopping cart is empty</p>"; 
         } 
         ?> 
         </tbody> 
           <tfoot> 
            <tr> 
             <th colspan="5">Total Price</th> 
             <th colspan="2" id="total">£<?php echo $total; ?></th> 
            </tr> 
           </tfoot> 
          </table> 

         </div> 
         <!-- /.table-responsive --> 

         <div class="box-footer"> 
          <div class="pull-left"> 
           <a href="category.php" class="btn btn-default"><i class="fa fa-chevron-left"></i>Continue shopping</a> 
          </div> 
          <div class="pull-right"> 
           <button type="submit" class="btn btn-default" name="update"><i class="fa fa-refresh"></i>Update</button> 
           <button type="submit" class="btn btn-primary">Checkout<i class="fa fa-chevron-right"></i> 
           </button> 
          </div> 
         </div> 

        </form> 

, bu bir öğeyi silmek için jquery geçerli:

$(".delete").click(function(e){ 
    e.preventDefault(); 
    var id=$(this).attr('data-id'); 
    $(this).parentsUntil('.ebook').remove(); 
    $.post('./js/delete.php',{ 
     Id:ebook_id 
    },function(a){ 
     location.href="cart.php?"; 

    }); 

Ve Öğeyi sepeti oturumundan kaldırmak için delete.php sayfası:

 <?php 
     session_start(); 
     $statement=$_SESSION['cart']; 
     for($i=0;$i<count($statement);$i++){ 
      if($statement[$i]['ebook_id']!=$_POST['ebook_id']){ 
      $newData[]=array(
      'ebook_id'=>$statement[$i]['ebook_id'], 
      'ebook_title'=>$statement[$i]['ebook_title'], 
      'price'=>$statement[$i]['price'], 
      'ebook_image'=>$statement[$i]['ebook_image'], 
      'quantity'=>$statement[$i]['quantity'] 
      ); 
     } 
     } 
    if(isset($newData)){ 
     $_SESSION['cart']=$newData; 
    }else{ 
     unset($_SESSION['cart']); 
     echo '0'; 
    } 
    ?> 

E-ticaret web sitelerinde yeniyim. Herhangi bir yardım için minnettar olurum. Silme sayfada Teşekkür

+0

olmalıdır 'js' yangını mu? "Ebook_id" JS'de tanımlanmış gibi görünmüyor. Bu ayrıca SQL enjeksiyonlarına açıktır. Hazırlanmış ifadeleri ve SQL'den ayrı kullanıcı verilerini kullanın. – chris85

cevap

0

<?php 

    if(isset($_POST['ebook_id'])){ 
    $query=mysqli_query($con,"DELETE FROM ebooks WHERE ebook_id=".$_POST['ebook_id']); 
    if($query==true) { 
     return true; 
     }else { 
     return false; 
     } 
    } 

Kişisel JS kodu

  $(".delete").click(function(e){ 
       e.preventDefault(); 
       var id=$(this).attr('data-id'); 
       $.ajax({ 
         type:"POST",  
         url: "delete.php", 
         data: {ebook_id: id}, 
         }).done(function(data) {        
         $("#row_"+id+"").hide();        
         }); 
       }); 

Formunuz

<form method="POST"> 
<table class="table"> 
    <thead> 
    <tr> 
     <th colspan="2">E-book</th> 
     <th>Price</th> 
     <th>Quantity</th> 
     <th>Subtotal</th> 
     <th>Remove</th> 
    </tr> 
    </thead> 
    <tbody> 

     <?php 
     if (isset($_POST['update'])){ 
      $arrquantity=$_POST['quantity']; 
      //check validate quantity 
      $valid = 1; 
      for ($i=0; $i < count($arrquantity); $i++) { 
       if (!is_numeric($arrquantity[$i]) || $arrquantity[$i] < 1) { 
        $valid=0; 
        break; 
       } 

      } 
      if ($valid == 1){ 
       $cart = unserialize (serialize($_SESSION['cart'])); 
       for ($i=0;$i<count($cart);$i++) { 
        $cart[$i]['quantity']=$arrquantity[$i]; 
       } 
      }else{ 
       $error= "Quantity is invalid"; 
      } 
      $_SESSION['cart']=$cart; 
     } 
     //delete ebook in cart 


     $total=0; 
     if (isset($_SESSION['cart'])) { 
     $data=$_SESSION['cart']; 
     $total=0; 
     for ($i=0;$i<count($data);$i++) { 
     ?> 
     <?php echo isset($error) ? $error :'';?> 
     //i don't know why you had open div here, so i replaced the div with tr and tr should have been inside the loop 
     <tr class="ebook" id="row_<?php echo $data[$i]['ebook_id'];?>"> 
      <td><a href=""><img src="<?php echo $data[$i]['ebook_image'];?>"></a></td> 
      <td><?php echo $data[$i]['ebook_title'];?></td> 
      <td>£<?php echo $data[$i]['price'];?></td> 
      <td><input type="text" value="<?php echo $data[$i]['quantity'];?>" data-price="<?php echo $data[$i]['price'];?>" data-id="<?php echo $data[$i]['ebook_id'];?>" class="quantity" name="quantity"></td> 
      <td class="subtotal">£<?php echo $data[$i]['price']*$data[$i]['quantity'];?></td> 
      <td><a href="cart.php?ebook_id=<?php echo $data[$i]['ebook_id'];?>&action =delete" onclick="return confirm('Are you sure?')" class="delete" data-id="<?php echo $data[$i]['ebook_id'];?>"><i class="fa fa-trash-o"></i></a> 
     </tr> 
    </tr> 

    <?php 
    $total=($data[$i]['price']*$data[$i]['quantity'])+$total; 
    } 
    }else{ 
     echo "<p class='text-muted'>Your shopping cart is empty</p>"; 
    } 
    ?> 
    </tbody> 
    <tfoot> 
    <tr> 
     <th colspan="5">Total Price</th> 
     <th colspan="2" id="total">£<?php echo $total; ?></th> 
    </tr> 
    </tfoot> 
</table> 

</div> 
<!-- /.table-responsive --> 

<div class="box-footer"> 
    <div class="pull-left"> 
     <a href="category.php" class="btn btn-default"><i class="fa fa-chevron-left"></i>Continue shopping</a> 
    </div> 
    <div class="pull-right"> 
     <button type="submit" class="btn btn-default" name="update"><i class="fa fa-refresh"></i>Update</button> 
     <button type="submit" class="btn btn-primary">Checkout<i class="fa fa-chevron-right"></i> 
     </button> 
    </div> 
</div> 

+0

Yanıt verdiğiniz için teşekkürler, ancak yine de aynı. Sil düğmesine tıkladığımda, onu sepetten silmek yerine yeni bir öğe ekler. Kodumun dağınık olduğunu biliyorum. Bunun için üzgünüm. – raul

İlgili konular