php添加商品和显示商品的业务逻辑
程序
数据库
数据库字段设计
一个模拟的后台管理和前台登录,后续功能会增加上
登录
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title><link rel="stylesheet" type="text/css" href="/style/common.css"/>
</head>
<body>
<?php
if (isset($_POST['button'])){ //判断是否点击登录按钮
//用户输入的用户名和密码
$username=$_POST['username'];
$pwd=$_POST['pwd'];
//连接数据库
mysql_connect("localhost",'root','root') or die(mysql_errno());
mysql_select_db("phpmysql");
mysql_query('set names utf8');
$sql="select * from user where username='$username' and password ='$pwd'";
$rs=mysql_query($sql);
//获取结果集的记录数
if(mysql_num_rows($rs)==1){
// echo '登录成功';
//登录成功后,跳转到商品显示页面;用head跳转,后面跟的是地址
header('location:showgoods.php');
}else {
echo '登录失败';
}
}
?>
<form name="form1" method="post" action="">
<table width="500" border="1" align="center">
<tr>
<th colspan="2">用户登录</th>
</tr>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" id="username"/></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="pwd" id="pwd"/></td>
</tr>
<tr>
<th colspan="2"><input type="submit" value="登录"id="button" name="button"/></th>
</tr>
</table>
</form>
</body>
</html>
商品显示
showgoods.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title><link rel="stylesheet" type="text/css" href="style/common.css"/>
</head>
<body>
<?php
//1.连接数据库
$link=@mysql_connect('localhost','root','root') or die('数据库连接失败');
//2-1选择数据库库 :方法一
//mysql_query('use data') or die('数据库连接失败');
// 2-2
mysql_select_db('phpmysql') or die('数据库选择失败');
//设置客户端编码
mysql_query('set names gbk');
//获取数据
$s=mysql_query('select * from products ');
//$rows=mysql_fetch_row($s);
?>
<table>
<tr>
<th>编号</th>
<th>商品名称</th>
<th>规格</th>
<th>价格</th>
<th>库存量</th>
<th>图片</th>
<th>网址</th>
</tr>
<?php
while ($rows=mysql_fetch_row($s)){
echo '<tr>';
echo '<td>'.$rows[0].'</td>';
echo '<td>'.$rows[1].'</td>';
echo '<td>'.$rows[2].'</td>';
echo '<td>'.$rows[3].'</td>';
echo '<td>'.$rows[4].'</td>';
echo $rows[5]==''?'<td>图片替换</td>':'<td><img src="'.$rows[5].'"></td>';
echo '<td>'.$rows[6].'</td>';
echo '</tr>';
}
?>
</table>
</body>
</html>
管理员界面
admin.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
<title>无标题文档</title><link rel="stylesheet" type="text/css" href="../style/common.css"/>
</head>
<body>
<?php
//1.连接数据库
$link=@mysql_connect('localhost','root','root') or die('数据库连接失败');
//2-1选择数据库库 :方法一
//mysql_query('use data') or die('数据库连接失败');
// 2-2
mysql_select_db('phpmysql') or die('数据库选择失败');
//设置客户端编码
mysql_query('set names GBK');
//获取数据
$rs=mysql_query('select * from products order by proid desc');
?>
<a href="add.php">添加商品</a>
<table width="800" >
<tr>
<th>编号</th>
<th>商品名称</th>
<th>规格</th>
<th>价格</th>
<th>库存量</th>
<th>图片</th>
<th>网址</th>
</tr>
<?php
while ($rows=mysql_fetch_assoc($rs)){
echo '<tr>';
echo '<td>'.$rows['proID'].'</td>';
echo '<td>'.$rows['proname'].'</td>';
echo '<td>'.$rows['proguige'].'</td>';
echo '<td>'.$rows['proprice'].'</td>';
echo '<td>'.$rows['proamount'].'</td>';
echo $rows['proimages']==''?'<td>图片替换</td>':'<td><img src="../'.$rows['proimages'].'"/></td>';
echo '<td>'.$rows['proweb'].'</td>';
echo '<td><input type="button" value="修改"></input></td>';
echo '<td><input type="button" value="删除"></input></td>';
echo '</tr>';
}
?>
</table>
</body>
</html>
增加商品
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>无标题文档</title><link rel="stylesheet" type="text/css" href="../style/common.css"/>
<title>无标题文档</title>
<script type="text/javascript">
function check()
{
var proame=document.getElementById("proname");
if(proame.value=='')
{
alert("商品名称不能为空");//获得焦点
proame.focus();
return false;
}
var proguige=document.getElementById("proguige");
if(proguige.value=='')
{
alert("商品规格不能为空");//获得焦点
proguige.focus();
return false;
}
//验证价格
var proprice=document.getElementById('proprice');
if(proprice.value=='' ||isNaN(proprice.value))
{
alert("价格必须是一个数字");//获得焦点
proprice.select();
return false;
}
//验证库存量
var proamount=document.getElementById("proamount");
if(proamount.value==''||isNaN(proamount.value)||proamount.value.indexof('.')!=-1){
alert('库存量必须是一个整数');
proamount.select();
return false;
}
}
</script>
</head>
<body>
<?php
if(isset($_POST['button'])){
$proname=$_POST['proname'];
$proguige=$_POST['proguige'];
$proprice=$_POST['proprice'];
$proamount=$_POST['proamount'];
$proimages=$_POST['proimages'];
$proweb=$_POST['proweb'];
//连接数据库
mysql_connect('localhost','root','root')or die(mysql_error());
mysql_select_db('phpmysql');
mysql_query('set names gbk');
$sql="insert into products values(null,'$proname','$proguige','$proprice','$proamount','$proimages','$proweb')";
if(mysql_query($sql)){
header('location:admin.php');//跳转到admin.php页面
}
else{
echo '插入失败';
}
}
?>
<form action="" name="form1" method="post" onsubmit="return check()">
<table width="500" border="1">
<tr>
<th colspan="2">添加商品</th>
</tr>
<tr>
<td>商品名称</td>
<td><input name="proname" id="proname" type="text" /></td>
</tr>
<tr>
<td>商品规格</td>
<td><input name="proguige" id="proguige" type="text" /></td>
</tr>
<tr>
<td>价格</td>
<td><input name="proprice" id="proprice" type="text" /></td>
</tr>
<tr>
<td>库存量</td>
<td><input name="proamount" id="proamount" type="text" /></td>
</tr>
<tr>
<td>图片地址</td>
<td><input name="proimages" id="proimages" type="text" /></td>
</tr>
<tr>
<td>网址</td>
<td><input name="proweb" id="proweb" type="text" />
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="button" id="button" value="提交" />
<input type="submit" name="button2" id="button2" value="返回" onclick="location.href='admin.php'"/></td>
</tr>
</table>
</form>
</body>
</html>
另外记住对象的分层结构
今天先写到这里,过几天会把其他功能,增加上。