数据库的结构:
create table book(
id int not null auto_increment primary key,
bookname varchar(50) not null default '',
publisher varchar(50) not null default '',
author varchar(50) not null default '',
price double(5,2) not null default 0.00,
detail text
);
<?php
include 'conn.php';
if (isset($_POST['submit']) && $_POST['submit']) {
$sql="INSERT INTO book(id,bookname,publisher,author,price,detail) VALUES (NULL, '$_POST[name]', '$_POST[publisher]', '$_POST[author]', '$_POST[price]','$_POST[content]')";
$result=mysql_query($sql);
//页面跳转,实现方式为javascript
$url = "list.php";
echo "<script language='javascript' type='text/javascript'>";
echo "window.location.href='$url'"; //上一层页面跳转
echo "</script>";
}
?>
<script type="text/javascript">
function checkpost()
{
if(addForm.name.value=="")
{
alert("请输入书名!");
addForm.name.focus();
return false;
}
}
</script>
<FORM name="addForm" METHOD="POST" ACTION="add.php" onsubmit="return checkpost();">
书 名:<INPUT TYPE="text" NAME="name" /><br />
出版社:<INPUT TYPE="text" NAME="publisher" /><br />
作 者:<input type="text" name="author"/><br />
价 格:<input type ="text" name="price"/><br />
简 介:<TEXTAREA NAME="content" ROWS="8" COLS="30"></TEXTAREA><br />
<INPUT TYPE="submit" name="submit" value="添加" /></FORM> list.php将记录用列表显示:
<?php
include 'conn.php';
$result=mysql_query("select *from book order by id");
?>
<table align="center" width="80%" border="1">
<caption><h1>图书信息表</h1></caption>
<th>编号</th><th>图书名</th><th>出版社</th><th>作者</th><th>价格</th><th>简介</th>
<?php
while($row=mysql_fetch_array($result))
{
?>
<tr>
<td><?=$row['id']?></td><td><?=$row['bookname'] ?></td><td><?=$row['publisher'] ?></td><td><?=$row['author'] ?></td><td><?=$row['price'] ?></td><td><?=$row['detail'] ?></td>
<td><a href="delete.php?id=<?=$row['id']?>">删除</a>
<a href="preEdit.php?id=<?=$row['id']?>">编辑</a></td>
</tr>
<?php
}
?>
</table>
<?php echo "<div align='center'><a href='add.php'>继续添加</a></div>"; ?>
delete.php删除记录:
<?php
include 'conn.php';
$id = $_GET['id'];
$query="delete from book where id=";
$query.="$id";
mysql_query($query);
?>
<?php
//页面跳转,实现方式为javascript
$url = "list.php";
echo "<script language='javascript' type='text/javascript'>";
echo "window.location.href='$url'";
echo "</script>";
?> preEdit.php更新列表之前将之前的记录显示:
<pre name="code" class="php"><?php
include 'conn.php';
$id=$_GET['id'];
$query="SELECT * FROM book WHERE id =".$id;
$result=mysql_query($query);
while ($rs=mysql_fetch_array($result)){
?>
<FORM METHOD="POST" ACTION="postEdit.php">
<input type="hidden" name="id" value="<?=$rs['id']?>"/>
书 名:<INPUT TYPE="text" NAME="name" value="<?=$rs['bookname']?>"/><br />
出版社:<INPUT TYPE="text" NAME="publisher" value="<?=$rs['publisher']?>"/><br />
作 者:<input type="text" name="author" value="<?=$rs['author']?>"/><br />
价 格:<input type ="text" name="price" value="<?=$rs['price']?>"/><br />
简 介:<TEXTAREA NAME="content" ROWS="8" COLS="30"><?=$rs['detail']?></TEXTAREA><br />
<INPUT TYPE="submit" name="submit" value="edit"/>
</FORM>
<?php }?> postEdit.php更新,并将结果显示在列表:
<?php
include 'conn.php';
$query="update book set bookname='$_POST[name]',publisher='$_POST[publisher]',author='$_POST[author]',price='$_POST[price]',detail='$_POST[content]' where id='$_POST[id]'";
$result=mysql_query($query);
if($result&&mysql_affected_rows()>0)
echo "更新成功";
else echo "更新失败,错误为:".mysql_error()."<br>";
?>
<?php
//页面跳转,实现方式为javascript
$url = "list.php";
echo "<script language='javascript' type='text/javascript'>";
echo "window.location.href='$url'";
echo "</script>";
?>
本文介绍了一个简单的在线图书管理系统,包括添加、列表显示、删除和编辑图书的功能。
1万+

被折叠的 条评论
为什么被折叠?



