我使用的phpsteam经常用着用着就闪退,所以做起来挺麻烦的。里面的代码有抄袭借鉴网上的代码,就是那个php做购物网站点击量最高的那个。
但是我很多代码也是自己写的不和其相同。
PHP是一门选修课,大学生上课大家都懂,代码有些方面不规范请见谅。
个人分析与总结:用户充值页面做的不完善,应该用户提交充值然后由管理员收到通知后同意后才能充值成功,没有管理员的管理系统模块,缺少管理员添加用户和添加商品的页面和具体实现。
源码我会在今晚做一些我自知不足的地方后以百度网盘的形式放到这篇博客上。
我已经把管理员用户的管理页面完成了,但是充值模块依旧没做,考试太多。
百度网盘:可扫码
链接:https://pan.baidu.com/s/1FDul18i6F00V-iDGeZNjew
提取码:epyc
与数据库连接的php文件:
conn.php:
<?php $conn = mysqli_connect("localhost", "root", "123456", "shop") or die("连接数据库服务器失败!".mysqli_error()); //连接MySQL服务器,选择数据库 ?>
登录页面:当登录admin 账号时到管理员页面
login.php:
<?php /** * Created by PhpStorm. * User: dada * Date: 2019/6/3 * Time: 21:15 */ ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>登录</title> </head> <body> <form action="logincl.php" method="post"> <table border="1" align="center"> <h1 align="center">登录</h1> <tr> <td>账号:</td> <td><input type="text" name="name"id="name"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" id="password"></td> </tr> <tr> <td></td> <td align="center"><input type="submit" value="提交" align="center"><input type="reset" value="重置"></td> </tr> </table> </form> </body> </html>
登录处理:
logincl.php:
<?php
/**
* Created by PhpStorm.
* User: dada
* Date: 2019/6/3
* Time: 21:18
*/
session_start();
include_once("conn.php");
if (!($_POST['name'] and $_POST['password'] )){
echo "<script>alert('输入不允许为空');history.go(-1);</script>";
}else
{
if($_POST['name']=='admin'and $_POST['password']=='123456' )
{
echo "<script>alert('登录成功');location='guanli/adminmian.php';</script>";
}
else {
$sql = "select password,account from login WHERE username = '{$_POST['name']}'";
$result = mysqli_query($conn, $sql);
if ($result) {
$a = mysqli_fetch_object($result);
$account = $a->account;
$password = $a->password;
if ($_POST['password'] == $password) {
$_SESSION["name"] = $_POST['name'];
$_SESSION["account"] = $account;
echo "<script>alert('登录成功');location='main.php';</script>";
} else {
echo "<script>alert('密码错误'+$account);history.go(-1);</script>";
}
} else {
echo "<script>alert('无此账号');history.go(-1);</script>";
}
}
}
?>
主页面:
mian.php:
<?php /** * Created by PhpStorm. * User: dada * Date: 2019/6/3 * Time: 21:20 */ ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>购物选择</title> <script> function add(){ if(!window.confirm('是否要加入购物车??')) return false; } </script> </head> <body> <?php session_start(); $account=$_SESSION["account"]; echo"<h1 align='center'>".'您的当前余额:'.$account."</h1>" ?> <table align="center" border="1"> <tr> <td colspan="5" bgcolor="#a9a9a9" align="center">购物信息表</td> </tr> <tr> <td>名称</td> <td>价格</td> <td>产地</td> <td>库存</td> <td>操作</td> </tr> <?php $gwc=$_SESSION["gwc"]; // echo $gwc[0][0]; //echo'555'; //echo $gwc[1][0]; include_once("conn.php");// 包含数据库连接页 $result=mysqli_query($conn,"select * from bgbiao ");// 执行查询操作并返回结果集 //$gwc=mysqli_fetch_all($result); //echo $gwc[0][1]; while($myrow=mysqli_fetch_object($result)){// 循环输出数据 ?> <tr> <td align="center"><span class="STYLE2"><?php echo $myrow->name; ?></span></td> <td align="left"><span class="STYLE2"><?php echo $myrow->price; ?></span></td> <td align="center"><span class="STYLE2"><?php echo $myrow->address; ?></span></td> <td align="center"><span class="STYLE2"><?php echo $myrow->yu; ?></span></td> <?php echo "<td class='m_td'><a href='maincl.php?id={$myrow->id}' onclick='add()'>加入购物车</a></td>"; echo "</tr>"; ?> </tr> <?php } ?> <tr> <td colspan="3"><a href="gwc.php">查看购物车</a></td> <td colspan="2"><a href="user.php">查看账户</a></td> </tr> </table> </body> </html>
主页面处理:
maincl.php:
主要操作就是加入购物车的实现
<?php /** * Created by PhpStorm. * User: dada * Date: 2019/6/4 * Time: 11:57 */ session_start(); // $id = $_GET["id"]; if(empty($_SESSION["gwc"])) { //如果点击的购物车是空的(第一次添加) //如果购物车里是空的,造二维数组, $arr = array( array($id,1) //一维数组,取ids,第一次点击增加一个 ); $_SESSION["gwc"]=$arr; //扔到session里面 } else //这里不是第一次点击 { //先判断购物车里是否已经有了该商品,用$id $arr = $_SESSION["gwc"]; //把购物车的状态取出来 $chuxian = false; //定义一个变量;用来表示是否出现,默认是未出现 foreach ($arr as $v) { //便利他 //如果这里面有这件商品 if ($v[0] == $id) //如果取过来的$v[0](商品的代号)等于$ids那么就证明购物车中已经有了这一件商品 { $chuxian = true; //如果出现,直接把chuxian改成true } } if($chuxian) { //购物车中有此商品 for($i=0;$i<count($arr);$i++) { if($arr[$i][0] == $id) { //把点到的商品编号加1 $arr[$i][1] += 1; } } $_SESSION["gwc"] = $arr; } else { //这里就只剩下:购物车里有东西,但是并没有这件商品 $asg = array($id,1); //设一个小数组 $arr[] = $asg; $_SESSION["gwc"]=$arr; } } header("location:main.php") ?>
购物车页面实现:
gwc.php:
<?php /** * Created by PhpStorm. * User: dada * Date: 2019/6/3 * Time: 21:20 */ $b=0; ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>购物车</title> </head> <body> <table align="center" border="1"> <tr> <td colspan="5" bgcolor="#a9a9a9" align="center">购物信息表</td> </tr> <tr> <td>名称</td> <td>价格</td> <td>数量</td> <td>操作</td> </tr> <?php session_start(); if(!empty($_SESSION["gwc"])) { $arr = array(); $arr = $_SESSION["gwc"]; foreach ($arr as $v) { include_once("conn.php"); $sql = "select * from bgbiao WHERE id = '{$v[0]}'"; $result=mysqli_query($conn,$sql);// 执行查询操作并返回结果集 $att =mysqli_fetch_all($result); foreach ($att as $a) { $b = $b + $a[2] * $v[1]; } } //造数组 foreach ($arr as $v) { include_once("conn.php");// 包含数据库连接页 $sql = "select * from bgbiao WHERE id = '{$v[0]}'"; $result = mysqli_query($conn, $sql);// 执行查询操作并返回结果集 $att = mysqli_fetch_all($result); foreach ($att as $a) { echo "<tr> <td>{$a[1]}</td> <td>{$a[2]}</td> <td>{$v[1]}</td> <td><a href='delete.php?id={$a[0]}'>删除</a> </td> </tr> "; } } } ?> <tr> <td colspan="2" >总价钱: <?php echo $b; $_SESSION["qian"]=$b; ?> </td> <td colspan="3" > 您的余额: <?php $account=$_SESSION["account"]; echo $account; ?> </td> </tr> <tr> <td colspan="5" align="center"><a href="tijiao.php">提交订单</a></td> </tr> </table> </body> </html>
购物车删除:
delete.php:
<?php /** * Created by PhpStorm. * User: dada * Date: 2019/6/3 * Time: 20:48 */ session_start(); $id = $_GET["id"]; $arr = $_SESSION["gwc"]; //var_dump($arr); //取索引2(数量) foreach ($arr as $key=>$v) { if($v[0]==$id) { if($v[1]>1){ //要删除的数据 $arr[$key][1]-=1; } else{ //数量为1的情况下,移除该数组 unset($arr[$key]); } } } $_SESSION["gwc"] = $arr; //记得扔到session里面 header("location:gwc.php"); //删除完跳转回去
购物车提交与数据库数据交互操作:
tijiao.php:
<?php /** * Created by PhpStorm. * User: dada * Date: 2019/6/4 * Time: 13:09 */ session_start(); include ("conn.php"); //判断用余额是否满足 $name = $_SESSION["name"]; //获取到用户名 $aa=$_SESSION["qian"];//总价格 $account=$_SESSION["account"]; //判断余额是否满足 $ann=array(); if(!empty($_SESSION["gwc"])) { $ann=$_SESSION["gwc"]; } $zhonglei = count($ann); if($account>=$aa) { //钱够,判断库存 foreach($ann as $v) { $sql = "select name,yu from bgbiao WHERE id='{$v[0]}'"; //代号$v[0] $result=mysqli_query($conn,$sql);// 执行查询操作并返回结果集 $akc =mysqli_fetch_all($result); $akc[0][1];//库存 //比较是否满足库存 // echo $akc[0][0]; // echo $akc[0][1]; // echo $v[1]; if($akc[0][1]<$v[1]) { echo "{$akc[0][0]}库存不足"; //退出 header("location:gwc.php"); exit; } } //提交订单: //i. 从用户账户中扣除本次购买的总价格 //ii. 从商品库存中扣除本次每种商品的购买数量 //iii. 向订单表和订单内容表中加入本次购买的商品信息 //扣除账户余额 $sql1 = "update login set account = account-{$aa} WHERE username = '{$name}'"; $result=mysqli_query($conn,$sql1);// 执行查询操作并返回结果集 //扣除库存 foreach($ann as $v) { $sql2= "update bgbiao set yu = yu-{$v[1]} WHERE id='{$v[0]}'"; //代号$v[0] $result=mysqli_query($conn,$sql2);// 执行查询操作并返回结果集 } $_SESSION["gwc"]=null; $_SESSION["account"]=$account-$aa; header("location:main.php"); } else { echo "钱不够"; header("location:gwc.php"); exit; }
查看账户页面:
user.php:
<?php /** * Created by PhpStorm. * User: dada * Date: 2019/6/3 * Time: 21:39 */ ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>个人信息浏览</title> </head> <body> <table align="center" border="1"> <tr> <td colspan="2" bgcolor="#a9a9a9" align="center">个人信息表</td> </tr> <tr> <td>姓名</td> <td>余额</td> </tr> <?php session_start(); // $_SESSION["name"]='dadada'; $name =$_SESSION["name"]; //echo $_SESSION["name"]; include_once("conn.php");// 包含数据库连接页 $result=mysqli_query($conn,"select id,username,account from login where username='{$name}'");// 执行查询操作并返回结果集 if (!$result) { printf("Error: %s ", mysqli_error($conn)); exit(); } while($myrow=mysqli_fetch_object($result)){// 循环输出数据 ?> <tr> <td align="center"><span class="STYLE2"><?php echo $myrow->username; ?></span></td> <td align="left"><span class="STYLE2"><?php echo $myrow->account; ?></span></td> </tr> <tr> <?php echo "<td class='m_td'><a href=useradd.php>充值</a></td><td><a href=userupdatepassword.php>改密码</a></td>"; echo "</tr>"; ?> <?php } ?> </table> </body> </html>
用户更改密码操作实现:
userupdatepassword.php:
<?php /** * Created by PhpStorm. * User: dada * Date: 2019/6/4 * Time: 9:18 */ session_start(); $name =$_SESSION["name"]; ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>改密码</title> </head> <body> <form action="userupdatepasswordcl.php" method="post"> <table border="1" align="center"> <tr> <td colspan="2" align="center" bgcolor="#a9a9a9">改密码</td> </tr> <tr> <td>姓名:</td> <td><?php echo $name ;?></td> </tr> <tr> <td>新密码:</td> <td><input type="password" name="password1" ></td> </tr> <tr> <td>再次输入新密码:</td> <td><input type="password" name="password2" ></td> </tr> <tr> <td align="center"><input type="submit" value="改密码" align="center"></td><td align="center"><input type="reset" value="重置"></td> </tr> </table> </form> </body> </html>
改密码处理操作:
userupdatepasswordcl.php:
<?php /** * Created by PhpStorm. * User: dada * Date: 2019/6/4 * Time: 9:22 */ session_start(); $name =$_SESSION["name"]; include_once("conn.php"); if (!($_POST['password1'] )||!($_POST['password2'] )){ echo "<script>alert('输入不允许为空');history.go(-1);</script>"; } else { $password1=$_POST['password1']; $password2=$_POST['password2']; if($password1==$password2) { $sqlstr2 = "update login set password ='{$password1}' where username='{$name}'"; $result2 = mysqli_query($conn, $sqlstr2); if ($result2) { echo "<script>alert('改密成功');location='login.php';</script>"; } else { echo "<script>alert('改密失败');history.go(-1);</script>"; } } else { echo "<script>alert('俩次密码不一致');history.go(-1);</script>"; } } ?>
用户充值页面:
useradd.php:
<?php /** * Created by PhpStorm. * User: dada * Date: 2019/6/4 * Time: 8:46 */ session_start(); $name =$_SESSION["name"]; //echo $_SESSION["name"]; //include_once("conn.php"); //$result=mysqli_query($conn,"select account from login where id='{$id}'");// 执行查询操作并返回结果集 ?> <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>充值</title> </head> <body> <form action="useraddcl.php" method="post"> <table border="1" align="center"> <tr> <td colspan="2" align="center" bgcolor="#a9a9a9">充值</td> </tr> <tr> <td>姓名:</td> <td><?php echo $name ;?></td> </tr> <tr> <td>充值:</td> <td><input type="number" name="qian" ></td> </tr> <tr> <td></td> <td align="center"><input type="submit" value="充值" align="center"><input type="reset" value="重置"></td> </tr> </table> </form> </body> </html>
useraddcl.php:
<?php /** * Created by PhpStorm. * User: dada * Date: 2019/6/4 * Time: 8:59 */ session_start(); $name =$_SESSION["name"]; include_once("conn.php"); if (!($_POST['qian'] )){ echo "<script>alert('输入不允许为空');history.go(-1);</script>"; }else { $qian=$_POST['qian']; $sqlstr1 = "select account from login where username='{$name}'"; $result1 = mysqli_query($conn, $sqlstr1); $rows = mysqli_fetch_row($result1);//将查询结果返回为数组 $number=$rows[0]; echo $number; $acc=$number +$qian; $sqlstr2 = "update login set account ='{$acc}' where username='{$name}'"; $result2 = mysqli_query($conn, $sqlstr2); if ($result2) { echo "<script>alert('充值成功');location='user.php';</script>"; } else { echo "<script>alert('充值失败');history.go(-1);</script>"; } } ?>