dongweihuan8832 2015-07-21 07:29
浏览 31
已采纳

关于购物车更新的PHP语言

Sorry, I'm getting a problem with updating, in PHP, a shopping cart. I have been looking for a solution to this for 2 days but to no avail. Can someone provide some advice for me?

<?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>

Too hard to find a error, I inserted echo "sql2: $sql2<hr>"; and I see the following notice on the website: sql2: update pia_spcart set p_qty=6 where user='' AND id='2' and p_stt1='addspcart1';

I recognized a problem with user but my DB is fine.

Actually, I could insert something into my DB but could not change product information or delete it. Such as "Change personal information function" and "Shopping Cart".

  • 写回答

1条回答 默认 最新

  • douyueju2912 2015-07-21 08:12
    关注

    Did you define "$user"? I don't see a:

    $user=$_SESSION["user"];
    

    It should be somewhere before the entire script, like:

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

    ! And most important, you don't seem to have a:

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

    at the beginning of the script. Thus the session variables are not defined!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么