博主剛剛接到了一個小活,要求做一個二手交易市場,因爲“客戶”是個初學者,要求用最簡單的語法來實現,正好可以複習一下相關知識。因爲先前有相關的開發經驗,因此較快的將其完成了,以下是博主在開發過程中的一些經驗和心得,在這裏分享給大家:
首先製作首頁,“客戶”那邊做出了一個前臺界面要我完成後面的程序(代碼我就不貼出來了,畢竟是人家寫的)
博主首先做的是登錄註冊界面,博主前端開發用的是bootstrap:
<?php
session_start();
require 'fun.php';
if($_SERVER['REQUEST_METHOD']=='POST'){
$username=$_POST['username'];
$password=$_POST['password'];
if(($username=='')||($password==''))
{ echo $username."<br>".$password."<br>";
header('refresh:3;url=index.html');
echo "用戶名或密碼不能爲空,請重新登錄";
exit;
}
$result = mysql_query("SELECT * FROM user where username='$username'");
$row = mysql_fetch_array($result);//轉成數組,且返回第一條數據,當不是一個對象時候退出
if($password!=$row['password'])
{
header('location:index.html');
echo "用戶名或密碼錯誤,請重新登錄";
exit;
}
else if(($username==$row['username'])&&($password==$row['password']))
{
$_SESSION['username']=$username;
$_SESSION['password']=$password;//設置session值,一般保存時間爲20分鐘
setcookie("username",$username,time()+7*24*60*60);//cookie值保存時間以秒爲單位
setcookie("password",$password,time()+7*24*60*60);
echo $_SESSION['username'];
//require "search.php";
header('location:top.php');
}
mysql_close($con);
}
?>
login.php 判斷登錄查詢數據庫並保存cookie與session
接下來是註冊界面:
<?php
require 'fun.php';
if($_SERVER['REQUEST_METHOD']=='POST')
{
$username=$_POST['username'];
$password=$_POST['password'];
$repassword=$_POST['repassword'];
$address="暫無地址";
$tel="暫無聯繫方式";
$account="0";
if($password===$repassword){
if (mysql_query("insert into user (username,password,address,tel,account) values('$username','$password','$address','$tel','$account')")){//註冊用戶插入數據
echo '<font size="4" color="red">註冊成功,3秒後跳轉到登錄界面</font>';
header('refresh:3;url=index.html');
}else{
echo '<font size="4" color="red">註冊失敗,3秒後重新跳轉到註冊界面</font>';
header('refresh:3;url=register.html');
}
}
}
?>
register.php 獲取註冊界面輸入的數據並判斷然後將數據插入數據庫
登錄成功後進入商品首頁:
<body >
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{ @session_start();
$type=$_POST['type1'];
$_SESSION['shoppingtype']=$type;
setcookie("shoppingtype",$type,time()+60);
}
?>
<div style="width:100%;height:90px">
<table><tr><th>
<div style="padding: 80px 480px 80px;">
<form class="bs-example bs-example-form" action="top.php" method="post" role="form">
<div class="col-lg-6">
<div class="input-group">
<input style="width:300px" type="text" id="type1" name="type1" class="form-control" placeholder="搜索您想要的商品">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">搜索</button>
</span>
</div>
</div>
</form>
</div>
</th><th width=200>
<font size="+3" color="#ff6464">曾哥二手市場</font><br><font size="+1" color="#ff6464">Second Hand Market</font>
</th></tr></table>
<nav class="navbar navbar-default" role="navigation">
<div class="container-fluid">
<div class="navbar-header" style="padding: 0px 50px 0px;">
<a class="navbar-brand">商品分類</a>
</div>
<div>
<ul class="nav navbar-nav">
<li><a href="#"> 服裝</a></li>
<li><a href="#"> 食品</a></li>
<li><a href="#"> 化妝品</a></li>
<li><a href="#"> 生活用品</a></li>
<li><a href="#"> 生日禮物</a></li>
<li><a href="#"> 數碼產品</a></li>
<li><a href="#"> 學習用品</a></li>
<li><a href="#"> 生活用品</a></li>
<li><a href="#"> 名牌鞋服</a></li>
<li><a href="#"> 其他產品</a></li>
<li><a href="managertop.php"> 我要發佈</a></li>
<li><a href="myinfo.php"> 編輯</a></li>
<li><a href="logout.php"> 退出系統</a></li>
<li><a href="index.html"> 登錄</a></li>
</ul>
</div>
</div>
</nav>
</div>
<?php require 'shoppingindex.php'; ?>
</body>
</html>
這裏完成的是首頁的top,即界面頂部界面
<?php
require 'fun.php';
$result = mysql_query("SELECT * FROM products ");
$i=0;
?>
<br><table><tr>
<?php
if(isset($_SESSION['shoppingtype'])){
$shoppingtype=$_SESSION['shoppingtype'];//判斷是否獲取了輸入框中的值
session_destroy();
setcookie('shoppingtype','',time()-1);//銷燬
}
if(isset($shoppingtype)){
$result = mysql_query("SELECT * FROM products where name like'%$shoppingtype%' ");
}
while($row=mysql_fetch_array($result)){
$image=$row['images'];
$i++;
$image="images/".$image;
$id=$row['id'];
?>
shoppingindex.php 用於頂部查詢欄中接收數據進行模糊查詢並遍歷查詢相關商品信息
<br><br><br><br><br><br>
<?php
require 'fun.php';
$result = mysql_query("SELECT * FROM products ");
$i=0;
?>
<br><table><tr>
<?php
if(isset($_SESSION['shoppingtype'])){
$shoppingtype=$_SESSION['shoppingtype'];//判斷是否獲取了輸入框中的值
session_destroy();
setcookie('shoppingtype','',time()-1);//銷燬
}
if(isset($shoppingtype)){
$result = mysql_query("SELECT * FROM products where name like'%$shoppingtype%' ");
}
while($row=mysql_fetch_array($result)){
$image=$row['images'];
$i++;
$image="images/".$image;
$id=$row['id'];
?>
<th style="width:40px"></th><th>
<div style="background-color:#edf7fa;width:450px;height:290px;border-radius:10px;">
<table border=0 style="width:440px;height:280px"><tr><th rowspan=5 style="width:280px;height:160px" align="center"><img src="<?=$image?>" style="width:260px;height:260px" class="img-rounded"></th>
<th height=50><h4> <span class="glyphicon glyphicon-tower" style="color: rgb(255, 140, 60);"> <?=$row['shopname']?></span></h4></th>
<tr><th><h4 class="display-4"> <?=$row['name']?></h4></th></tr>
<tr><th><font size="+1"color="red"> <?=$row['price']?>RMB</th></tr>
<tr><th> <span class="label label-warning">標籤:<?=$row['type']?></span></th></tr>
<tr><th> <a href="shopping.php?id=<?=$id?>"><button type="button" class="btn btn-success">進店看看</button></a></th></tr>
</table>
</div>
</th>
<th style="width:20px"></th>
<?php
if($i%4==0)
echo "<tr><th height=21></th></tr><tr>";
}
?>
</body>
完整的shoppingindex.php
接着,便可點擊進店看看來查詢商品詳情:
通過超鏈接< a href=“shopping.php?id=$id”>來通過GET方式來傳遞數據
<?php
$id=$_GET['id'];
require "fun.php";
if(isset($id)){
$result = mysql_query("SELECT * FROM products where id='$id' ");
$row=mysql_fetch_array($result);
$shopname=$row['shopname'];//店鋪名稱
$name=$row['name'];//商品名
$detail=$row['detail'];//描述
$price=$row['price'];//單價
$num=$row['num'];//庫存數目
$image=$row['images'];//圖片
$type=$row['type'];
$image="images/".$image;
}
?>
shopping.php通過獲取的id值然後查詢數據庫獲取該商品的詳細信息並遍歷出來
點擊購買後會執行判斷,若當前用戶已經登錄,則可以進行購買,否則會跳轉至登錄界面
輸入密碼後點擊確認付款,會執行兩個判斷:支付密碼是否正確,賬戶餘額是否充足
點擊首頁的編輯,可以進行個人信息的修改
<?php
require 'fun.php';
if($_SERVER['REQUEST_METHOD']=='POST')
{
$id=$_POST['id'];
$username=$_COOKIE['username'];
$name=$_POST['name'];
$password=$_POST['password'];
$paypassword=$_POST['paypassword'];
$tel=$_POST['tel'];
$address=$_POST['address'];//商品名稱
if(isset($id)){
if(mysql_query("update user set password='$password',paypassword='$paypassword',tel='$tel',address='$address',name='$name' where id=$id "))
header("refresh:0;url=myinfo.php");
}else
header("refresh:0;url=myinfo.php");
}
?>
提交修改信息 myinfoupdate.php
接着,對於二手市場而言,用戶可以自己發佈自己的商品:
點擊首頁的 我要發佈,進入後臺界面,用戶可以對自己發佈的商品進行增刪改查操作:
managertop.php界面
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$conn = new mysqli("localhost", "root", "px980305", "secondhandmarket")or die ("You cannot connect the database!<br>".mysqli_error());
$id=$_POST['id'];//商品id
$name=$_POST['name'];//商品名稱
$price=$_POST['price'];//單價
$content=$_POST['content'];//詳情
$num=$_POST['num'];//庫存
$adress=$_POST['adress'];
$type=$_POST['type'];//商品類型
$shopname=$_POST['shopname'];//店鋪名稱
$username=$_POST['username'];
if($_POST['submit']=='添加')
{
$sql="insert into products (shopname,username,password,images,name,type,price,num,detail) values('$shopname','$username','123456','$adress','$name','$type','$price','$num','$content')";
if ($conn->query($sql) === TRUE) {
header("location:managertop.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
if($_POST['submit']=='修改'){
$sql="select * from products where id='$id'";
$result =mysqli_query($conn,$sql);
$row = mysqli_fetch_array($result);
$price=$price?$price:$row['price'];
$content=$content?$content:$row['detail'];
$num=$num?$num:$row['num'];
$name=$name?$name:$row['name'];
$type=$type?$type:$row['type'];
$shopname=$shopname?$shopname:$row['shopname'];
$adress=$adress?$adress:$row['images'];
$sql="update products set shopname='$shopname',name='$name',num='$num',detail='$content',images='$adress',type='$type' where id=$id ";
if ($conn->query($sql) === TRUE) {
header("location:managertop.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
if($_POST['submit']=='刪除'){
$sql="delete from products where id='$id'";
if ($conn->query($sql) === TRUE) {
header("location:managertop.php");
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
}
?>
submit.php, 提交數據並處理,包含增加,刪除,修改
此外系統還包含用戶管理,退出登錄等功能,在這裏就不一一贅述了。
後臺登錄界面: adminlogin.html
後臺登錄驗證:adminlogin.php
<?php
session_start();
require 'fun.php';
if($_SERVER['REQUEST_METHOD']=='POST'){
$username=$_POST['username'];
$password=$_POST['password'];
if(($username=='')||($password==''))
{ echo $username."<br>".$password."<br>";
header('refresh:3;url=index.html');
echo "用戶名或密碼不能爲空,請重新登錄";
exit;
}
$result = mysql_query("SELECT * FROM admin where adminname='$username'");
$row = mysql_fetch_array($result);//轉成數組,且返回第一條數據,當不是一個對象時候退出
if($password!=$row['password'])
{
header('location:index.html');
echo "用戶名或密碼錯誤,請重新登錄";
exit;
}
else if(($username==$row['adminname'])&&($password==$row['password']))
{
$_SESSION['adminusername']=$username;
$_SESSION['adminpassword']=$password;//設置session值,一般保存時間爲20分鐘
setcookie("adminusername",$username,time()+7*24*60*60);//cookie值保存時間以秒爲單位
setcookie("adminpassword",$password,time()+7*24*60*60);
echo $_SESSION['username'];
//require "search.php";
header('location:adminindex.php');
}
mysql_close($con);
}
?>
訂單管理 adminorder.php
<table class="table table-bordered" style="width:90%">
<caption>訂單管理</caption>
<thead>
<tr>
<th style="height:50px">編號</th>
<th>訂單編號</th>
<th>用戶名</th>
<th>商家編號</th>
<th>商品名</th>
<th>價格</th>
<th>收貨地址</th>
<th>展示圖片</th>
<th>聯繫方式</th>
</tr>
</thead>
<tbody>
<?php
require 'fun.php';
$result = mysql_query("SELECT * FROM orderuser");
while($row = mysql_fetch_array($result))//轉成數組,且返回第一條數據,當不是一個對象時候退出
{$img=$row['productimage'];
$id=$row['id'];
?>
<tr>
<th style="height:50px"><p class="text-danger"><?=$row['id']?></th>
<th><?=$row['orderid']?></th>
<th><?=$row['username']?></th>
<th><?=$row['shopid']?></th>
<th><?=$row['productname']?></th>
<th><p class="text-danger"><?=$row['price']?></th>
<th><p class="text-danger"><?=$row['useraddress']?></th>
<th><img src="<?=$img?>" style="width:65px;height:65px"></th>
<th><p class="text-danger"><?=$row['usertel']?></th>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<br><br><br>
用戶管理 adminuser.php
<table class="table table-bordered" style="width:90%">
<caption>用戶管理</caption>
<thead>
<tr>
<th style="height:50px">編號</th>
<th>用戶名</th>
<th>暱稱</th>
<th>賬戶餘額</th>
<th>聯繫方式</th>
<th>註銷用戶</th>
<th>充值金額</th>
<th>賬戶充值</th>
</tr>
</thead>
<tbody>
<?php
require 'fun.php';
$result = mysql_query("SELECT * FROM user");
while($row = mysql_fetch_array($result))//轉成數組,且返回第一條數據,當不是一個對象時候退出
{
$id=$row['id'];
?>
<tr>
<form action="adminsubmit.php" method="post">
<th style="height:50px"><p class="text-danger"><input type="text" name="id" id="id" value="<?=$row['id']?>" style="width:50px;height:30px;border:none" readonly></th>
<th><?=$row['username']?></th>
<th><?=$row['name']?></th>
<th><?=$row['account']?></th>
<th><?=$row['tel']?></th>
<th><button type="submit" name="submit" id="submit" onclick="return window.confirm('確定刪除?')" value="刪除" class="btn btn-danger">刪除用戶</button></th>
<th><input type="text" name="account" id="account" value="" style="width:50px;height:30px">
<th><button type="submit" name="submit" id="submit" onclick="return window.confirm('確定充值?')" value="充值" class="btn btn-danger">賬戶充值</button></th>
</form>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<br><br><br>
商品店鋪管理 adminindex.php
<table class="table table-bordered" style="width:90%">
<caption>商品店鋪管理</caption>
<thead>
<tr>
<th style="height:50px">編號</th>
<th>用戶名</th>
<th>店鋪名</th>
<th>商品名</th>
<th>價格</th>
<th>類型</th>
<th>展示圖片</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php
require 'fun.php';
$result = mysql_query("SELECT * FROM products");
while($row = mysql_fetch_array($result))//轉成數組,且返回第一條數據,當不是一個對象時候退出
{$img="images/".$row['images'];
$id=$row['id'];
?>
<tr>
<th style="height:50px"><p class="text-danger"><?=$row['id']?></th>
<th><?=$row['username']?></th>
<th><?=$row['shopname']?></th>
<th><?=$row['name']?></th>
<th><p class="text-danger"><?=$row['price']?></th>
<th><?=$row['type']?></th>
<th><img src="<?=$img?>" style="width:65px;height:65px"></th>
<th><a href="admindelete.php?id=<?=$id?>"><button type="button" onclick="return window.confirm('確定刪除?')" class="btn btn-danger">刪除產品</button></a></th>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
<br><br><br>
附上項目文件目錄:
碼字不易,給個讚唄!