简介:
这是初学php的时候做的一个简单的增删改查,代码粗糙 , hxd们不要介意哈
1、首页
2.php文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <?php include('3.php'); $query="SELECT * from tb_bookinfo "; $result = mysqli_query($conn,$query); echo '<table border="1" align="center">'; echo '<caption><h3>图书管理系统</h3></caption>'; echo '<tr>'; echo '<th align="center">ID</th> <th align="center">书名</th> <th align="center">作者</th> <th align="center">价格</th> <th align="center">类别</th> <th colspan="2" align="center">操作</th>'; echo '</tr>'; while ($row = mysqli_fetch_array($result)){ echo '<tr>'; for ($i=0;$i<count($row)/2;$i++){ echo "<td>".$row[$i].' '."</td>"; } echo '<td><a href="update.php?id='.$row[0].'&&bookName='.$row[1].'&&author='.$row[2].'&&price='.$row[3].'&&type='.$row[4].'">修改</a></td>>'; echo '<td><a href="delete.php?id='.$row[0].'">删除</a></td>>'; echo '</tr>'; } echo '<tr align="right"><td colspan="6" ><a href="1.html"><input type="submit" value="查询"></a></td> <td ><a href="insert.html"><input type="submit" value="填加"></a></td> </tr>'; echo '<tr align="right"><td colspan="7">'.'共有记录:'.mysqli_num_rows($result).'条</td></tr>'; echo '</table>'; ?> </body> </html>
3.php文件(数据库链接文件)
<?php header("Content-Type:text/html;charset=utf-8"); $conn = mysqli_connect('localhost','root','root','book1') or exit('数据库连接失败!!');
首页效果图
查询部分
1.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>图书查询</title> </head> <body> <form method="post" action="query.php"> <input type="text" name="bookName" placeholder="请输入书名"> <input type="submit" name="sub" value="查询"> </form> </body> </html>
query.php文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <?php include('3.php'); $query="SELECT * from tb_bookinfo where bookname='".$_POST['bookName']."'"; $result = mysqli_query($conn,$query); echo '<table border="1" align="center">'; echo '<caption><h3>图书管理系统</h3></caption>'; echo '<tr>'; echo '<th align="center">ID</th> <th align="center">书名</th> <th align="center">作者</th> <th align="center">价格</th> <th align="center">类别</th>'; echo '</tr>'; while ($row = mysqli_fetch_array($result)){ echo '<tr>'; for ($i=0;$i<count($row)/2;$i++){ echo "<td>".$row[$i].' '."</td>"; } echo '</tr>'; } echo '<tr align="right"><td colspan="3">'.'共有记录:'.mysqli_num_rows($result).'条</td></tr>'; echo '</table>'; ?> </body> </html>
查询效果图
增加部分
add.php文件
<?php header('Content-Type:text/html;charset=utf-8'); include ('3.php'); $bookName=$_POST['bookName']; $author=$_POST['author']; $price=$_POST['price']; $type=$_POST['type']; if($bookName&&$author&&$price&&$type){ $sql="insert into tb_bookinfo(bookname,author,price,type) values('".$bookName."','".$author."','".$price."','".$type."')"; $result=mysqli_query($conn,$sql); if($result){ echo "添加成功,点击<a href='2.php'>这里</a>返回查询>"; } else{ echo "添加失败,点击<a href='insert.html'>这里</a>重新添加>"; } }else{ echo "输入的值不允许为空,点击<a href='javascript:οnclick=history.back()'>这里</a>返回>"; }
insert.html文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="add.php" method="post"> 书名:<input type="text" name="bookName"><br> 作者:<input type="text" name="author"><br> 价格:<input type="text" name="price"><br> 类型:<input type="text" name="type"><br> <input type="submit" value="添加"> </form> </body> </html>
添加效果图
修改部分
update.php
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="update_ok.php" method="post"> <input type="hidden" name="id" value="<?php echo $_GET["id"] ?>"><br> 书名:<input type="text" name="bookName" value="<?php echo $_GET["bookName"] ?>"><br> 作者:<input type="text" name="author" value="<?php echo $_GET["author"] ?>"> <br> 价格:<input type="text" name="price" value="<?php echo $_GET["price"] ?>"><br> 类型:<input type="text" name="type" value="<?php echo $_GET["type"] ?>"><br> <input type="submit" value="修改"> </form> </body> </html>
update_ok.php
<?php header('Content-Type:text/html;charset=utf-8'); include('3.php'); $sql ="update tb_bookinfo set bookname='".$_POST['bookName']."', author='".$_POST['author']."', price='".$_POST['price']."', type='".$_POST['type']."' where id= " .$_POST['id']; $result = mysqli_query($conn, $sql); if($result){ echo "修改成功,点击<a href='2.php'>这里</a>返回查询>"; } else{ echo "修改失败"; }
修改效果图
删除部分
delete.php文件
<?php header('Content-Type:text/html;charset=utf-8'); include('3.php'); $id=$_GET["id"]; $sql ="delete from tb_bookinfo where id=".$id; $result = mysqli_query($conn, $sql); if($result){ echo "删除成功,点击<a href='2.php'>这里</a>返回查看"; } else{ echo "删除失败"; }
删除效果图