2015-07-21 131 views
1

對不起,我越來越有更新的問題,在PHP中,一個購物車。我一直在尋找一個解決方案,這2天,但無濟於事。有人可以爲我提供一些建議嗎?關於購物車更新PHP語言

<?php 
if(isset($_POST["act"])) 
{ 
    if(isset($_POST["updspc"])) 
    { 
     $id_form=$_POST["id_form"]; 
     $countid=count($id_form); 
     $sl=$_POST["p_qty"]; 
     $total=$_POST["total"]; 
     if(isset($_SESSION["user"])) 
     { 
      for($i=0;$i<$countid;$i++) 
      {   
       if($sl[$i]<=0) 
        $del=mysql_query("delete from pia_spcart where id='$id_form[$i]' and p_stt1='addspcart1'"); 
       else{  
       $tt[$i]=$total[$i]*$sl[$i]; 
       $sql2="update pia_spcart set p_qty=$sl[$i] where user='$user' AND id='$id_form[$i]' and p_stt1='addspcart1'"; 
       $sql2.=';'; 
       echo "sql2: $sql2<hr>"; 
       $kq2=mysql_query($sql2); 
       } 

      }   
     }  
    } 

    if(isset($_POST["delspc"])) 
    { 
     $delete=$_POST["delete"];   
     $deletecount=count($_POST["delete"]); 
     if($deletecount==0) 
      echo "<script>alert('Please choose products to delete');</script>"; 
     else{ 
      for ($j=0;$j<$deletecount;$j++) 
      { 
       $SQL_delspc = "DELETE FROM pia_spcart WHERE user='$user' and id='$delete[$j]' and p_stt1='addspcart1'"; 
       $kq_delspc=mysql_query($SQL_delspc); 
       $n+=mysql_affected_rows(); 
      }     
     }  
    } 

    if(isset($_POST["order"])) 
    { 
     $id_form=$_POST["id_form"]; 
     $countid=count($id_form); 
     $now=date("Y-m-d H:i:s"); 
     $p_qty=$_POST["p_qty"]; 
//  $total=$_POST["total"]; 
     for($k=0;$k<$countid;$k++) 
     {   
      //$id_f=implode($_POST["id_form"], "','"); 
      $sql_kt="select * from pia_spcart where id='$id_form[$k]' and user='$user' and p_stt1='order'"; 
     // echo "$sql_kt<hr>"; 
      $kq_kt=mysql_query($sql_kt);    
      if(mysql_num_rows($kq_kt)==0) 
      {  
       $sql_order="update pia_spcart set p_stt1='order',p_orddate='$now' where id='$id_form[$k]' and user='$user' and p_stt1='addspcart1'"; 
       $kq_dathang=mysql_query($sql_order);    
       echo "<script>window.location='index.php?pia=listcart';</script>"; 
      } 
      else 
      { 
       while($r_kt=mysql_fetch_array($kq_kt)) 
       { 
        $sl_kt=$r_kt["p_qty"]; 
       // echo "$sl_kt<hr>"; 
        $sql_del="delete from pia_spcart where user='$user' and id='$id_form[$k]' and p_stt1='addspcart1'";     
        $kq_del=mysql_query($sql_del); 
        $sql_order="update pia_spcart set p_orddate='$now',p_qty=$sl_kt+$p_qty[$k] where id='$id_form[$k]' and user='$user' and p_stt1='order'"; 
        $sql_order.=';'; 
        $kq_dathang=mysql_query($sql_order); 
        echo "<script>window.location='index.php?pia=listcart';</script>"; 

       } 
      } 
     }     //echo "sql: $sql_order"; 
    } 

} 
?> 
<table width="560" border="0" cellspacing="0" cellpadding="0" style="border:1px solid #333"> 
<form method="post" name="form"> 
    <tr> 
    <td colspan="6" class="tieude" align="center">YOUR CART</td> 
    </tr> 
    <tr bgcolor="#ad2200" align="center" height="30" style="font-weight:bold"> 
    <td width="50" style="border-right:1px solid #666"><font color="#FFFFFF">No</font></td> 
    <td width="110" style="border-right:1px solid #666"><font color="#FFFFFF">Product</font></td> 
    <td width="60" style="border-right:1px solid #666"><font color="#FFFFFF">Quantity</font></td> 
    <td width="95" style="border-right:1px solid #666"><font color="#FFFFFF">Price</font></td> 
    <td width="95" style="border-right:1px solid #666"><font color="#FFFFFF">SubTotal</font></td> 
    <td width="50" ><font color="#FFFFFF">Delete</font></td>     
    </tr> 
    <?php 
    $user=$_SESSION["user"];  
    $sql="select pia_spcart.*,pia_products.* from pia_spcart,pia_products where pia_spcart.id=pia_products.id AND pia_spcart.user='$user' AND pia_spcart.p_stt1='addspcart1'"; 
    $kq=mysql_query($sql); 
    $i=0; 
    $tien=0; 
    if(mysql_num_rows($kq)==0) 
     echo "<tr><td colspan=6 height=30 align=center>No product in your cart!</td></tr>"; 
    else{ 
    while($r=mysql_fetch_array($kq)) 
    { 
     $id=$r["id"]; 
     $tensp=$r["p_prodname"]; 
     if(isset($_SESSION["p_qty"])) $p_qty=$_SESSION["p_qty"]; 
     else $p_qty=$r["p_qty"]; 
     $price=$r["p_price"]; $price2=number_format($price,0,'','.'); 
     if($price==0) $s="(contact)"; else $s=$price2." SGD"; 
     $total=$price*$p_qty; $total2=number_format($total,0,'','.'); 
     if($total==0) $t="(contact)"; else $t=$total2." SGD"; 
     $totaltien=$totaltien+$total;$totaltien2=number_format($totaltien,0,'','.');   
     if($totaltien==0) $tt="(contact)"; else $tt=$totaltien2." SGD"; 
     $i++; 
?>   
     <tr align="center" height="30" > 
      <td width="50" style="border-right:1px solid #666; border-bottom:1px solid #666"><?php echo $i; ?></td> 
      <td width="110" style="border-right:1px solid #666; border-bottom:1px solid #666"><?php echo $tensp; ?></td> 
      <td width="60" style="border-right:1px solid #666; border-bottom:1px solid #666"> 
      <input type="text" name="p_qty[]" value="<?php echo $p_qty ?>" style="width:30px" /> 
      <input type="hidden" name="id_form[]" value="<?php echo "$id"; ?>" /> 
      <input type="hidden" name="total[]" value="<?php echo "$total"; ?>" /> 

      </td> 
      <td align="right" width="95" style="border-right:1px solid #666; border-bottom:1px solid #666; padding-right:3px"><?php echo $s; ?> </td> 
      <td align="right" width="95" style="border-right:1px solid #666; border-bottom:1px solid #666; padding-right:3px"><?php echo $t; ?> </td> 
      <td width="50" style=" border-bottom:1px solid #666" > 
      <input type="checkbox" name="delete[]" value="<?php echo "$id"; ?>"/> 
      </td>    
     </tr>   
<?php   
    } 
    } 
    if(mysql_num_rows($kq)==0) 
     echo ""; 
    else 
     echo "<tr> 
    <td height=30 colspan=6 align=right style=\"padding-right:5px; padding-bottom:5px; color:#Fff\">Subtotal need to pay: $tt </td></tr> 
    <tr> 
    <td colspan=\"6\" style=\" border-bottom:1px solid #666\" bgcolor=\"#fff\" align=\"center\" height=\"35\"> 
    <input type=\"button\" name=\"continueshopping\" value=\"Continue Shopping\" class=\"button3\" onmouseover=\"style.background='url(images/button-150-2-o.png)'\" onmouseout=\"style.background='url(images/button-150-o.png)'\" onclick=\"document.form.action='index.php'; document.form.submit();\" /> 

    <input type=\"submit\" name=\"updspc\" value=\"Update\" class=\"button\" onmouseover=\"style.background='url(images/button-2-o.gif)'\" onmouseout=\"style.background='url(images/button-o.gif)'\" onclick=\"document.form.submit();\" /> 

    <input type=\"submit\" name=\"delspc\" value=\"Delete Cart\" class=\"button2\" onmouseover=\"style.background='url(images/button-110-2-o.png)'\" onmouseout=\"style.background='url(images/button-110-o.png)'\" onclick=\"document.form.submit();\" /> 

    <input type=\"submit\" name=\"order\" value=\"Order\" class=\"button2\" onmouseover=\"style.background='url(images/button-110-2-o.png)'\" onmouseout=\"style.background='url(images/button-110-o.png)'\" onclick=\"document.form.submit();\"/> 
    </td> 
    </tr>"; 
    ?> 
    <input type="hidden" name="act" /> 
    </form> 
</table> 
<div style='font-size:11px; line-height:20px; color:#FF0000; width:560px'></div> 

很難找到一個錯誤,我插echo "sql2: $sql2<hr>";,我看到網站上通知如下: sql2: update pia_spcart set p_qty=6 where user='' AND id='2' and p_stt1='addspcart1';

我認出了問題用戶但我的DB是罰款。

其實,我可以插入一些到我的數據庫,但不能改變產品信息或刪除它。如「更改個人信息功能」和「購物車」。

+2

停止使用棄用'mysql_ *'的功能,使用庫MySQLi/PDO代替。而HTML是一團糟。在繼續之前解決HTML問題。 – Raptor

+0

作爲@Raptor說了'mysql_ *'已經從PHP的最新版本中刪除。該'mysqli_ *'功能是相似的,但更安全。另外'PDO'工作在一個不同的模式,以及許多認爲這是更好。 – TRiG

回答

1

你有沒有定義 「$用戶」?我沒有看到一個:

$user=$_SESSION["user"]; 

它應該是某個地方的整個腳本之前,如:

if (isset($_SESSION["user"])) { 
    $user=$_SESSION["user"]; 
} else { 
    /// something... 
} 

!最重要的是,您似乎沒有:

session_start(); // first line, then the others... 

在腳本的開頭。因此會話變量沒有被定義!

+0

太感謝你了!我做到了成功:) –