简单的图书管理系统php实现

本文介绍了一个简单的在线图书管理系统,包括添加、列表显示、删除和编辑图书的功能。

数据库的结构:

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
);


add.php添加记录,并用列表显示:

<?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>"; 
?> 




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值