·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> php网站开发 >> 用PHP添加购物商品

用PHP添加购物商品

作者:佚名      php网站开发编辑:admin      更新时间:2022-07-23
用php添加购物商品
 1 <?php 2 session_start();  3 header ( "Content-type: text/html; charset=UTF-8" );                         //设置文件编码格式 4 require("system/system.inc.php");                          //包含配置文件 5 /** 6     *  1表示添加成功 7     *  2表示用户没有登录 8     *  3表示商品已添加过 9     *  4表示添加时出现错误10     *  5表示没有商品添加11 */12 $reback = '0';13 if(empty($_SESSION['member'])){ //判断用户有没有登录14     $reback = '2';              //没有就返回215 }else{16     $key = $_GET['key'];         //判断用户有没有添加商品17     if($key == ''){              //判断用户有没有添加商品,如果为空就表示没有商品喽18         $reback = '5';           //返回值19     }else{    20         $boo = false;             //定义商品有没有被添加21         $sqls = "select id,shopping from tb_user where name = '".$_SESSION['member']."'"; 22         $shopcont = $admindb->ExecSQL($sqls,$conn);23         if(!empty($shopcont[0]['shopping'])){  //shopping为三维数组,用empty判断商品是否为空24             $arr = explode('@',$shopcont[0]['shopping']);//@分割数组也就是每个商品的值25             foreach($arr as $value){ //foreach取出每个数组的值26                 $arrtmp = explode(',',$value);            //用explode分割得出商品的俱体信息27                 if($key == $arrtmp[0]){                 //如果添加的商品等于已添加的商品28                     $reback = '3';                      //那么返回值就表示已添加29                     $boo = true;                         30                     break;31                 }32             }33             if($boo == false){              //方法一添加商品34                 $shopcont[0]['shopping'] .= '@'.$key.',1'; 35                 $update = "update tb_user set shopping='".$shopcont[0]['shopping']."' where name = '".$_SESSION['member']."'";36                 $shop = $admindb->ExecSQL($update,$conn);37                 if($shop){38                     $reback = 1;39                 }else{40                     $reback = '4';41                 }42             }43         }else{44             $arrtmp = $key.",1";          //方法二添加商品45             $updates = "update tb_user set shopping='".$arrtmp."' where name = '".$_SESSION['member']."'";46             $result = $admindb->ExecSQL($updates,$conn);47             if($result){48                 $reback = 1;49             }else{50                 $reback = '4';51             }52         }53     }54 }55 echo $reback;56 ?>