此项目是由于本人刚刚入门php且在校代刷网课而编写的,由于在上课时间不方便接单,故特意写一个自助下单系统来实现客户自助下单。
本项目主要实现以下功能:
1.用户下单
2.用户支付
3.用户通过账号查询订单
4.管理员登录查看所有订单
设计思路:项目比较简单,基本就是与数据库之间的通信,进行数据的增删改查。支付使用的是第三方支付接口。
以下是具体代码:
用户提交订单页面
index.php
1 <?php 2 header(‘Content-type: text/html; charset=UTF8‘); 3 ?> 4 <html> 5 <head> 6 <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> 7 </head> 8 <body> 9 <div class="content" align="center"> 10 <form action="add.php" method="post"> 11 <span>账号:</span> 12 <input type="text" name="id" placeholder="账号" required><br><br> 13 <span>密码:</span> 14 <input type="password" name="pwd" placeholder="密码" required><br><br> 15 <span>学校全称:</span> 16 <input type="text" name="school" placeholder="学校全称" required><br><br> 17 <span>课程平台:</span> 18 <input type="text" name="platform" placeholder="课程平台" value="智慧树" required><br><br> 19 <span>课程门数:</span> 20 <input type="text" name="count" placeholder="课程门数" id="num" required><br><br> 21 <span>课程名称:</span><br> 22 <textarea name="classname" placeholder="请输入课程名称,多门课程用逗号分开" required> 23 </textarea><br> 24 <div class="list"> 25 <p class="p1">总价:</p> 26 <input id="price" name="sprice"class="p1" value="0" readonly="readonly"> 27 <p class="p1">元</p> 28 </div> 29 <br> 30 <input type="submit" value="提交" class="submit" id="button"> 31 32 <input type = "reset" value = "重置" class="reset"> 33 </form> 34 </div> 35 </body> 36 <style> 37 body{ 38 background:url(image/bg2.jpg); 39 } 40 .list{ 41 width:120px; 42 height:50px; 43 background:url(image/bg5.jpg); 44 } 45 .p1{ 46 float:left; 47 } 48 #price{ 49 margin-top:15px; 50 width:50px; 51 color:red; 52 } 53 textarea{ 54 height:100px; 55 padding: 5px 0px 0px 5px; 56 width: 70%; 57 } 58 </style> 59 <script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"> 60 </script> 61 <script> 62 $(function(){ 63 $(‘#num‘).on(‘input propertychange‘,function(){ 64 var num = $(‘#num‘).val(); 65 var price = num*15; 66 $("#price").val(price); 67 }) 68 }); 69 </script> 70 </html>
处理订单并生成支付数据页面
add.php
1 <?php 2 header(‘Content-type: text/html; charset=UTF8‘); 3 ?> 4 <?php 5 require_once "connect.php"; 6 $id=$_POST["id"]; 7 $pwd=$_POST["pwd"]; 8 $school=$_POST["school"]; 9 $platform=$_POST["platform"]; 10 $count=$_POST["count"]; 11 $classname=$_POST["classname"]; 12 $price=$_POST["sprice"]; 13 if($id==null){ 14 echo "<script>alert(‘请输入账号!‘);location=‘index.php‘;</script>"; 15 }; 16 if($pwd==null){ 17 echo "<script>alert(‘请输入密码!‘);location=‘index.php‘;</script>"; 18 }; 19 if($school==null){ 20 echo "<script>alert(‘请输入学校全称!‘);location=‘index.php‘;</script>"; 21 }; 22 if($platform==null){ 23 echo "<script>alert(‘请输入平台名称!‘);location=‘index.php‘;</script>"; 24 }; 25 if($count==null){ 26 echo "<script>alert(‘请输入课程数量!‘);location=‘index.php‘;</script>"; 27 }; 28 $sql="insert into orderdetail (id,pwd,school,platform,count,classname,time)values(‘$id‘,‘$pwd‘,‘$school‘,‘$platform‘,‘$count‘,‘$classname‘,now())"; 29 mysql_query("set names ‘utf8‘"); 30 mysql_query($sql); 31 mysql_close; 32 //echo "<script>alert(‘信息提交成功,跳转付款页面!‘);location=‘chosetopay.html‘;</script>"; 33 ?> 34 35 <html> 36 <head> 37 <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"> 38 <title>刷客在线支付</title> 39 </head> 40 <body> 41 <center> 42 <h1>请您确认订单信息:</h1> 43 <span>账号:</span><?php echo $id ?><br> 44 <span>密码:</span><?php echo $pwd ?><br> 45 <span>学校:</span><?php echo $school ?><br> 46 <span>平台:</span><?php echo $platform ?><br> 47 <span>课程名:</span><?php echo $classname ?> 48 <br><br><br> 49 </center> 50 <div align="center"> 51 <form> 52 <p><input id="inputmoney" type="text" name="inputmoney" class="form-control" placeholder="请输入金额" required value="<?php echo $price.‘.00‘?>" readonly="readonly"></p> 53 <div class="radio"> 54 <label> 55 <p><input type="radio" name="demo1" id="demo1-alipay" value="43" checked=""> 56 支付宝支付</p> 57 </label> 58 </div> 59 <div class="radio"> 60 <label> 61 <p><input type="radio" name="demo1" id="demo1-weixin" value="44"> 62 微信支付</p> 63 </label> 64 </div> 65 <button type="button" id="demoBtn1">确认支付</button> 66 </form> 67 </div> 68 <form style=‘display:none;‘ id=‘formpay‘ name=‘formpay‘ method=‘post‘ action=‘https://api.6688pay.com:8080/?input_charset=utf-8‘> 69 <input name=‘order_no‘ id=‘order_no‘ type=‘text‘ value=‘‘/> 70 <input name=‘subject‘ id=‘subject‘ type=‘text‘ value=‘‘ /> 71 <input name=‘pay_type‘ id=‘pay_type‘ type=‘text‘ value=‘‘ /> 72 <input name=‘money‘ id=‘money‘ type=‘text‘ value=‘‘/> 73 <input name=‘app_id‘ id=‘app_id‘ type=‘text‘ value=‘‘/> 74 <input name=‘extra‘ id=‘extra‘ type=‘text‘ value=‘‘/> 75 <input name=‘sign‘ id=‘sign‘ type=‘text‘ value=‘‘/> 76 <input type=‘submit‘ id=‘submitdemo1‘> 77 </form> 78 79 <!-- Jquery files --> 80 <script type="text/javascript" src="https://cdn.staticfile.org/jquery/1.11.1/jquery.min.js"></script> 81 <script type="text/javascript"> 82 $().ready(function(){ 83 function getistype(){ 84 return ($("#demo1-alipay").is(‘:checked‘) ? "43" : "44" ); 85 } 86 87 $("#demoBtn1").click(function(){ 88 $.get( 89 "pay.php", 90 { 91 money : $("#inputmoney").val(), 92 pay_type : getistype(), 93 }, 94 function(data){ 95 $("#order_no").val(data.order_no); 96 $(‘#subject‘).val(data.subject); 97 $("#pay_type").val(data.pay_type); 98 $(‘#money‘).val(data.money); 99 $(‘#app_id‘).val(data.app_id); 100 $(‘#extra‘).val(data.extra); 101 $(‘#sign‘).val(data.sign); 102 $(‘#submitdemo1‘).click(); 103 }, "json" 104 ); 105 }); 106 }); 107 </script> 108 <style> 109 body{ 110 background:url("image/bg2.jpg"); 111 } 112 </style> 113 </body> 114 </html>
数据库连接页面
connect.php
1 <?php 2 define(‘DB_HOST‘, ‘localhost‘); 3 define(‘DB_USER‘, ‘root‘); 4 define(‘DB_PWD‘, ‘11111111‘); 5 define(‘DB_CHARSET‘, ‘UTF8‘); 6 define(‘DB_DBNAME‘, ‘zzxd‘); 7 8 $con=mysql_connect(DB_HOST,DB_USER,DB_PWD); 9 if(!$con){ 10 die(‘数据库连接失败!‘.$mysql_error()); 11 } 12 mysql_select_db("zzxd"); 13 ?>
支付用的是第三方支付接口,因为本案例比较简单(主要是本人暂时用不来api)所以直接使用了它提供的Demo
(此处使用的是小叮当支付)
以下是Demo内容(Demo中的主页被我合并到add.php中了,就是add.php中的html部分):
pay.php
1 <?php 2 /** 3 * ---------------------参数生成页------------------------------- 4 */ 5 include ‘define.php‘; 6 //从网页传入money:支付价格, pay_type:支付渠道:43-支付宝;44-微信支付 7 $order_no = date(‘Ymd‘) . str_pad(mt_rand(1, 99999), 5, ‘0‘, STR_PAD_LEFT); //订单号 8 $subject = ‘商品名称‘; 9 $pay_type = $_GET["pay_type"]; 10 $money = $_GET["money"]; 11 $extra = ""; 12 13 $sign = md5("order_no=" . $order_no . "&subject=" . $subject . "&pay_type=" . $pay_type . "&money=" . $money . "&app_id=" . $app_id . "&extra=" . $extra . "&" . $app_secret); 14 //经常遇到有研发问为啥sign值返回错误,大多数原因:1.参数的排列顺序不对;2.上面的参数少传了,但是这里的sign值又带进去计算了,导致服务端sign算出来和你的不一样。 15 16 $returndata[‘order_no‘] = $order_no; 17 $returndata[‘subject‘] = $subject; 18 $returndata[‘pay_type‘] = $pay_type; 19 $returndata[‘money‘] = $money; 20 $returndata[‘app_id‘] = $app_id; 21 $returndata[‘extra‘] =$extra; 22 $returndata[‘sign‘] =$sign; 23 echo jsonSuccess("OK",$returndata,""); 24 25 26 //返回错误 27 function jsonError($message = ‘‘,$url=null) 28 { 29 $return[‘msg‘] = $message; 30 $return[‘data‘] = ‘‘; 31 $return[‘code‘] = -1; 32 $return[‘url‘] = $url; 33 return json_encode($return); 34 } 35 36 //返回正确 37 function jsonSuccess($message = ‘‘,$data = ‘‘,$url=null) 38 { 39 return json_encode($data); 40 } 41 42 ?>
define.php
1 <?php 2 $app_id = "*****"; 3 $app_secret = "62f949392a2b4e2f***********"; 4 ?> 5 ``` 6 notify.php 7 8 ``` 9 <?php 10 /** 11 * ---------------------异步通知页面------------------------------- 12 * 13 * 此页就是接口后台的notify_url页的网址 14 * 支付成功,平台会通知这里。 15 * 16 * -------------------------------------------------------------- 17 */ 18 include ‘define.php‘; 19 20 //同步回调页面 21 //接收参数 22 $order_no = $_POST["order_no"]; 23 $subject = $_POST["subject"]; 24 $pay_type = $_POST["pay_type"]; 25 $money = $_POST["money"]; 26 $realmoney = $_POST["realmoney"]; 27 $result = $_POST["result"]; 28 $xddpay_order = $_POST["xddpay_order"]; 29 $app_id = $_POST["app_id"]; 30 $extra = $_POST["extra"]; 31 $sign = $_POST["sign"]; 32 33 //计算签名 34 $mysign_forstr = "order_no=" . $order_no . "&subject=" . $subject . "&pay_type=" . $pay_type . "&money=" . $money . "&realmoney=" . $realmoney . "&result=" . $result . "&xddpay_order=" . $xddpay_order . "&app_id=" . $app_id . "&extra=" . $extra . "&" . $app_secret; 35 $mysign = strtoupper(md5($mysign_forstr)); 36 37 if ($sign == $mysign) //验签 38 { 39 if ($result == "success"){ 40 //建议业务处理放在notify.asp页面,本页仅用于显示支付结果 41 //此处在您数据库中查询:此笔订单号是否已经异步通知给您付款成功了。如成功了,就给他返回一个支付成功的展示。 42 echo "success"; 43 } 44 else{ 45 echo "支付失败"; 46 } 47 } 48 else 49 { 50 echo "mysign_forstr=" . $mysign_forstr; //调试时开启 51 echo "<br>sign=" . $sign; 52 echo "<br>mysign=" . $mysign; 53 echo "<br><br>认证签名失败"; 54 } 55 56 ?>
return.php
1 <?php 2 /** 3 * ---------------------支付成功,用户会跳转到这里------------------------------- 4 * 5 * 此页就是接口后台的return_url页的网址 6 * 支付成功,平台会把用户跳转回这里。 7 * 8 * -------------------------------------------------------------- 9 */ 10 include ‘define.php‘; 11 12 //同步回调页面 13 //接收参数 14 $order_no = $_GET["order_no"]; 15 $subject = $_GET["subject"]; 16 $pay_type = $_GET["pay_type"]; 17 $money = $_GET["money"]; 18 $realmoney = $_GET["realmoney"]; 19 $result = $_GET["result"]; 20 $xddpay_order = $_GET["xddpay_order"]; 21 $app_id = $_GET["app_id"]; 22 $extra = $_GET["extra"]; 23 $sign = $_GET["sign"]; 24 25 //计算签名 26 $mysign_forstr = "order_no=" . $order_no . "&subject=" . $subject . "&pay_type=" . $pay_type . "&money=" . $money . "&realmoney=" . $realmoney . "&result=" . $result . "&xddpay_order=" . $xddpay_order . "&app_id=" . $app_id . "&extra=" . $extra . "&" . $app_secret; 27 $mysign = strtoupper(md5($mysign_forstr)); 28 29 if ($sign == $mysign) //验签 30 { 31 if ($result == "success"){ 32 //建议业务处理放在notify.asp页面,本页仅用于显示支付结果 33 //此处在您数据库中查询:此笔订单号是否已经异步通知给您付款成功了。如成功了,就给他返回一个支付成功的展示。 34 echo "恭喜,支付成功!,订单号:".$order_no; 35 } 36 else{ 37 echo "支付失败"; 38 } 39 } 40 else 41 { 42 echo "mysign_forstr=" . $mysign_forstr; //调试时开启 43 echo "<br>sign=" . $sign; 44 echo "<br>mysign=" . $mysign; 45 echo "<br><br>认证签名失败"; 46 } 47 48 ?>
到这里已经实现了用户下单以及支付功能,剩下的功能在此扩展即可
管理员登录页面:
admin.php
1 <?php 2 header(‘Content-type:text/html;charset="UTF8"‘); 3 ?> 4 <html> 5 <body> 6 <center> 7 <form action="admin_see.php" method="post"> 8 <span>账号:</span><input type="text" name="userid" placeholder="请输入账号"><br><br> 9 <span>密码:</span><input type="password" name="pwd" placeholder="请输入密码"><br><br> 10 <input type="submit" value="确定"> 11 </form> 12 </center> 13 </body> 14 </html>
管理员登录成功查看数据页面:
admin_see.php
1 <?php 2 $HOST_ID=$_POST[‘userid‘]; 3 $HOST_PWD=$_POST[‘pwd‘]; 4 $con=mysql_connect("localhost",$HOST_ID,$HOST_PWD); 5 if(!$con){ 6 echo "<script>alert(‘信息有误,请重新输入!‘);location=‘admin.php‘;</script>"; 7 } 8 mysql_select_db("zzxd", $con); 9 $result1=mysql_query("SELECT * from orderdetail"); 10 echo "<table><tr><td>===账号===|</td><td>===密码===|</td><td>===学校===|</td><td>===平台===|</td><td>===课程===</td></tr>"; 11 while($row=mysql_fetch_array($result1)){ 12 echo "<tr>"; 13 echo "<td>".$row[‘id‘]."</td>"; 14 echo "<td>".$row[‘pwd‘]."</td>"; 15 echo "<td>".$row[‘school‘]."</td>"; 16 echo "<td>".$row[‘platform‘]."</td>"; 17 echo "<td>".$row[‘classname‘]."</td>"; 18 echo "</tr>"; 19 } 20 echo "</table>"; 21 22 mysql_close($con); 23 24 ?>
用户输入订单号查询订单页面
user.php
1 <?php 2 header(‘Content-type="text/html";charset="UTF8"‘); 3 ?> 4 <html> 5 <body> 6 <center> 7 <h3>请输入账号进行订单查询</h3> 8 <form action="user_view.php" method="post"> 9 <span>账号:</span><input type="text" name="userid" placeholder="请输入账号"><br><br> 10 <input type="submit" value="确定"> 11 </form> 12 </center> 13 </body> 14 </html>
订单查询成功显示页面:
user_view.php
1 <?php 2 header(‘Content-type="text/html";charset="UTF8"‘); 3 $USER_ID=$_POST[‘userid‘]; 4 require_once(‘connect.php‘); 5 $sql="select * from orderdetail where id = ‘$USER_ID‘"; 6 $result1=mysql_query($sql); 7 echo "<table><tr><td>===账号===|</td><td>===密码===|</td><td>===学校===|</td><td>===平台===|</td><td>===课程===</td></tr>"; 8 while($row=mysql_fetch_array($result1)){ 9 echo "<tr>"; 10 echo "<td>".$row[‘id‘]."</td>"; 11 echo "<td>".$row[‘pwd‘]."</td>"; 12 echo "<td>".$row[‘school‘]."</td>"; 13 echo "<td>".$row[‘platform‘]."</td>"; 14 echo "<td>".$row[‘classname‘]."</td>"; 15 echo "</tr>"; 16 } 17 echo "</table>"; 18 19 mysql_close($con); 20 ?>
到这里要实现的目标功能全部实现,当然还有很多功能可以扩展,也还有很多地方可以优化和改进,此处就不再写了,有其它扩展我再来更新。
数据库比较简陋就不发了。
原文地址:https://www.cnblogs.com/fangmr/p/11192152.html
时间: 2024-11-07 16:18:25