[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=utf-8" />
<title>商品添加页面</title>
<script type="text/javascrip">
function fun1(){
//通过属性跳转
location.href='admin.php';
}
</script>
<script type="text/javascrip">
function check(){
//商品名称不能为空
var proname=document.getElementById('proname');
if(proname.value==''){
alert('商品名称不能为空');
proname.focus();
return false;
}
//商品规格不能为空
var proguide=document.getElementById('proguide');
if(proguide.value==''){
alert('商品规格不能为空');
proguide.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['submit'])){
//获取数据
$proID=$_POST['proID'];
$proname=$_POST['proname'];
$proguide=$_POST['proguide'];
$proprice=$_POST['proprice'];
$proamount=$_POST['proamount'];
$proimages=$_POST['proimages'];
$proweb=$_POST['proweb'];
//连接数据库
require '../public/dbcn.php';
$dbcn->exec('set names utf8');//设置数据库编码
//sql语句
$sql="insert into products values(null, '$proname', '$proguide',
'$proprice','$proamount', '$proimages','$proweb')";
echo $sql;
if($dbcn->exec($sql)){
echo'添加商品成功!<br/>';
header('location:admin.php');
}else{
echo'添加商品失败!';
}
}
?>
<form action="" method="post" onsubmit="return check()">
<table width="585" border="1" align="center">
<tr>
<th colspan="2"align="center"><strong>添加商品</strong></td>
</tr>
<tr>
<td width="120">商品名称</td>
<td width="449">
<input type="text" name="proname" id="proname" /></td>
</tr>
<tr>
<td>商品规格</td>
<td><input type="text" name="proguide" id="proguide" /></td>
</tr>
<tr>
<td>价格</td>
<td>
<input type="text" name="proprice" id="proprice" /></td>
</tr>
<tr>
<td>库存量</td>
<td><label for="proamount"></label>
<input type="text" name="proamount" id="proamount" /></td>
</tr>
<tr>
<td>图片地址</td>
<td>
<input type="text" name="proimages" id="proimages" /></td>
</tr>
<tr>
<td>网址</td>
<td>
<input type="text" name="proweb" id="proweb" /></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" name="submit" id="submit" value="提交" />
<input type="button" name="return" id="return" value="返回" onClick="fun1()"/>
</td>
</tr>
</table>
</form>
</body>
</html>
初级程序员
by: 云代码会员 发表于:2019-04-21 14:25:46 顶(0) | 踩(0) 回复
对的
回复评论