首页上一页 1 下一页尾页 1 条记录 1/1页
向数据库中添加数据
发表在PHP图书答疑
2009-10-11
是否精华
是
否
版块置顶:
是
否
无法运行Mr\14\sl\04中的实例。无法向数据库中添加数据操作,代码如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>添加数据</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<center>
<table width="799" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="106" background="images/banner.jpg"> </td>
</tr>
<tr>
<td>
<table width="100%" height="27" border="0" cellpadding="0" cellspacing="0" background="images/link.jpg">
<tr>
<td width="243" align="center" valign="middle"><b>
<?php
echo date("Y-m-d")." ";
echo date("l");
?>
</b></td>
<td width="90" align="center" valign="middle"><a href="index.php?action=show" class="a">浏览目录</a></td>
<td width="94" align="center" valign="middle"><a href="index.php?action=insert" class="a">添加图书</a></td>
<td width="94" align="center" valign="middle"><a href="#" class="a">简单查询</a></td>
<td width="91" align="center" valign="middle"><a href="#" class="a">高级查询</a></td>
<td width="94" align="center" valign="middle"><a href="#" class="a">分组统计</a></td>
<td width="93" align="center" valign="middle"><a href="#" class="a">退出系统</a></td>
</tr>
</table>
</td>
</tr>
</table>
<table width="799" height="300" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle">
<?php
$conn = mysql_connect("localhost","root","888888");
mysql_query("set names gb2312");
mysql_select_db("db_mingrisoft");
//浏览图书
if (("action" == null) or ($_GET["action"] == "insert")){
?>
<form name="form1" id="form1" method="post">
<table width="90%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="5%" height="25" class="top">id</td>
<td width="30%" class="top">书名</td>
<td width="10%" class="top">价格</td>
<td width="20%" class="top">出版时间</td>
<td width="10%" class="top">类别</td>
<td width="10%" class="top">操作</td>
</tr>
<?php
$sqlstr = "select * from tb_mrbook order by id";
$result = mysql_query($sqlstr,$conn);
while ($rows = mysql_fetch_row($result)){
echo "<tr>";
for($i = 0; $i < count($rows); $i++){
echo "<td height='25' align='center' class='m_td'>".$rows["$i"]."</td>";
}
echo "<td class='m_td'><a href='#'>修改</a>/<a href='#'>删除</a></td>";
echo "</tr>";
}
?>
</table>
</form>
<?php
}
//添加操作
if ($_POST['action'] == "insert"){
if (!($_POST["bookname"] and $_POST["price"] and $_POST["f_time"] and $_POST["type"]))
echo "输入不允许为空。点击<a href='javascript:onclick=history.go(-1)'>这里</a> 返回";
else{
$sqlstr1 = "insert into tb_mrbook values('','".$_POST["bookname"]."','".$_POST["price"]."','".$_POST["f_time"]."','".$_POST["type"]."')";
$result = mysql_query($sqlstr1,$conn);
if ($result){
echo "添加成功,点击<a href='index.php'>这里</a>查看";
}
else
echo "<script>alert('添加失败');history.go(-1);</script>";
}
}
//插入界面
if($_GET["action"] == "insert"){
?>
<form name="intFrom" method="post" action="index.php">
<table width="100%" height="200" border="0" cellpadding="0" cellspacing="0">
<tr align="center" valign="middle">
<td width="30%" class="c_td"> </td>
<td width="10%" align="right" class="c_td"> </td>
<td width="30%" class="c_td"> </td>
<td width="30%" class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">书名:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="bookname"></td>
<td class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">价格:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="price"></td>
<td class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">出版时间:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="f_time"></td>
<td class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">所属类别:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="type"></td>
<td class="c_td"> </td>
</tr>
<tr align="center" valign="middle">
<td class="c_td"> </td>
<td colspan="2" class="c_td">
<input type="hidden" name="action" value="insert"/>
<input type="submit" name="Submit" value="添加"/>
<input type="reset" name="reset" value="重置"/></td>
<td class="c_td"> </td>
</tr>
</table>
</form>
<?php
}
?></td>
</tr>
</table>
<table width="799" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="67" background="images/bottom.jpg"> </td>
</tr>
</table>
</center>
</body>
</html>
注:本地数据库连接正常。
以上是:mr\14\sl\04中的实例代码。请老师指点一下。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>添加数据</title>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<center>
<table width="799" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="106" background="images/banner.jpg"> </td>
</tr>
<tr>
<td>
<table width="100%" height="27" border="0" cellpadding="0" cellspacing="0" background="images/link.jpg">
<tr>
<td width="243" align="center" valign="middle"><b>
<?php
echo date("Y-m-d")." ";
echo date("l");
?>
</b></td>
<td width="90" align="center" valign="middle"><a href="index.php?action=show" class="a">浏览目录</a></td>
<td width="94" align="center" valign="middle"><a href="index.php?action=insert" class="a">添加图书</a></td>
<td width="94" align="center" valign="middle"><a href="#" class="a">简单查询</a></td>
<td width="91" align="center" valign="middle"><a href="#" class="a">高级查询</a></td>
<td width="94" align="center" valign="middle"><a href="#" class="a">分组统计</a></td>
<td width="93" align="center" valign="middle"><a href="#" class="a">退出系统</a></td>
</tr>
</table>
</td>
</tr>
</table>
<table width="799" height="300" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="center" valign="middle">
<?php
$conn = mysql_connect("localhost","root","888888");
mysql_query("set names gb2312");
mysql_select_db("db_mingrisoft");
//浏览图书
if (("action" == null) or ($_GET["action"] == "insert")){
?>
<form name="form1" id="form1" method="post">
<table width="90%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="5%" height="25" class="top">id</td>
<td width="30%" class="top">书名</td>
<td width="10%" class="top">价格</td>
<td width="20%" class="top">出版时间</td>
<td width="10%" class="top">类别</td>
<td width="10%" class="top">操作</td>
</tr>
<?php
$sqlstr = "select * from tb_mrbook order by id";
$result = mysql_query($sqlstr,$conn);
while ($rows = mysql_fetch_row($result)){
echo "<tr>";
for($i = 0; $i < count($rows); $i++){
echo "<td height='25' align='center' class='m_td'>".$rows["$i"]."</td>";
}
echo "<td class='m_td'><a href='#'>修改</a>/<a href='#'>删除</a></td>";
echo "</tr>";
}
?>
</table>
</form>
<?php
}
//添加操作
if ($_POST['action'] == "insert"){
if (!($_POST["bookname"] and $_POST["price"] and $_POST["f_time"] and $_POST["type"]))
echo "输入不允许为空。点击<a href='javascript:onclick=history.go(-1)'>这里</a> 返回";
else{
$sqlstr1 = "insert into tb_mrbook values('','".$_POST["bookname"]."','".$_POST["price"]."','".$_POST["f_time"]."','".$_POST["type"]."')";
$result = mysql_query($sqlstr1,$conn);
if ($result){
echo "添加成功,点击<a href='index.php'>这里</a>查看";
}
else
echo "<script>alert('添加失败');history.go(-1);</script>";
}
}
//插入界面
if($_GET["action"] == "insert"){
?>
<form name="intFrom" method="post" action="index.php">
<table width="100%" height="200" border="0" cellpadding="0" cellspacing="0">
<tr align="center" valign="middle">
<td width="30%" class="c_td"> </td>
<td width="10%" align="right" class="c_td"> </td>
<td width="30%" class="c_td"> </td>
<td width="30%" class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">书名:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="bookname"></td>
<td class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">价格:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="price"></td>
<td class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">出版时间:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="f_time"></td>
<td class="c_td"> </td>
</tr>
<tr>
<td class="c_td"> </td>
<td align="right" valign="middle" class="c_td">所属类别:</td>
<td align="center" valign="middle" class="c_td"><input type="text" name="type"></td>
<td class="c_td"> </td>
</tr>
<tr align="center" valign="middle">
<td class="c_td"> </td>
<td colspan="2" class="c_td">
<input type="hidden" name="action" value="insert"/>
<input type="submit" name="Submit" value="添加"/>
<input type="reset" name="reset" value="重置"/></td>
<td class="c_td"> </td>
</tr>
</table>
</form>
<?php
}
?></td>
</tr>
</table>
<table width="799" border="0" cellpadding="0" cellspacing="0">
<tr>
<td height="67" background="images/bottom.jpg"> </td>
</tr>
</table>
</center>
</body>
</html>
注:本地数据库连接正常。
以上是:mr\14\sl\04中的实例代码。请老师指点一下。