php商品分页,修改,删除等
继上次 php添加商品 文章之后,继续
add.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"/>
<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>
修改页面modify.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>
<link rel="stylesheet" type="text/css" href="../style/common.css"/>
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<title>修改商品页面</title>
</head>
<body>
<?php
$id=$_GET['id'];//获取需要修改商品的编号
//连接数据库
mysql_connect('localhost','root','root')or die(mysql_error());
mysql_select_db('phpmysql');
mysql_query('set names gbk');
$sql="select * from products where proid=$id";
$rs=mysql_query($sql);
$rows=mysql_fetch_assoc($rs);
//修改业务逻辑
if(isset($_POST['button'])){
//获取提交的数据
$proname=$_POST['proname'];
$proguige=$_POST['proguige'];
$proprice=$_POST['proprice'];
$proamount=$_POST['proamount'];
$proimages=$_POST['proimages'];
$proweb=$_POST['proweb'];
//拼接sql语句
$sql="update products set proname='$proname' ,
proguige='$proguige',proprice='$proprice',proamount='$proamount',proimages='$proimages',proweb='$proweb' where proid=$id";
if(mysql_query($sql))
{
header('location:admin.php');
}else{
echo '修改失败';
exit();
}
}
?>
<form action="" name="form1" method="post" >
<table width="500" border="1">
<tr>
<th colspan="2">修改商品</th>
</tr>
<tr>
<td>商品名称</td>
<td><input name="proname" id="proname" type="text" value="<?php echo $rows['proname']?>" /></td>
</tr>
<tr>
<td>商品规格</td>
<td><input name="proguige" id="proguige" type="text" value="<?php echo $rows['proguige']?>" /></td>
</tr>
<tr>
<td>价格</td>
<td><input name="proprice" id="proprice" type="text" value="<?php echo $rows['proprice']?>"/></td>
</tr>
<tr>
<td>库存量</td>
<td><input name="proamount" id="proamount" type="text" value="<?php echo $rows['proamount']?>" /></td>
</tr>
<tr>
<td>图片地址</td>
<td><input name="proimages" id="proimages" type="text" value="<?php echo $rows['proimages']?>" /></td>
</tr>
<tr>
<td>网址</td>
<td><input name="proweb" id="proweb" type="text" value="<?php echo $rows['proweb']?>" />
</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='admin1/admin.php'"/></td>
</tr>
</table>
</form>
</body>
</html>
删除del.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>
</head>
<body>
<?php
//获得需要删除的编号
$id=$_GET['id'];
//连接数据库
mysql_connect('localhost','root','root') or die(mysql_error());
mysql_select_db('phpmysql');
mysql_query('set names gbk');
$sql="delete from products where proid=$id";
if(mysql_query($sql)){
header('location:admin.php');
}else{
die(mysql_error());
}
?>
</body>
</html>
下面来写一个分页
fenye.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 count(*) from products ');
$rows=mysql_fetch_row($s);//将资源匹配成索引数组
$recordcount=$rows[0];//总记录数
//定义页面大小
$pagesize=2;
//求总页数
$pagecount=ceil($recordcount/$pagesize);
//点击页码,获得当前页面
$pageno=isset($_GET['pageno'])?$_GET['pageno']:1;//get请求是否有值,有值就用自身
if($pageno<1){
$pageno=1;
}
if($pageno>$pagecount){
$pageno=$pagecount;
}
//获取当前页的起始位置
$startno=($pageno-1)*$pagesize;
//获取当前页的内容
$sql="select * from products limit $startno,$pagesize";
$rs=mysql_query($sql);
?>
<table width="500" border="1">
<tr>
<th>编号</th>
<th>商品名称</th>
<th>规格</th>
<th>价格</th>
<th>库存量</th>
<th>图片</th>
<th>网址</th>
</tr>
<?php
while ($rows=mysql_fetch_row($rs)){
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>
<table border="1" width="500">
<tr>
<td>
【<a href="?pageno=1">首页</a>】
【<a href="?pageno=<?php echo $pageno-1?>">上一页</a>】
【<a href="?pageno=<?php echo $pageno+1?>">下一页</a>】
【<a href="?pageno=<?php echo $pagecount?>">末页</a>】
</td>
<td>
<?php
for($i=1;$i<=$pagecount;$i++){
echo '<a href="?pageno='.$i.'">'.$i.'</a> ';
}
?>
</td>
</tr>
</table>
</body>
</html>