我使用的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\n", 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>";
}
}
?>
