PHP+MySQL增删改查

PHP+MySQL增删改查

PHP和Mysql可以对数据库进行简单的增删改查,本文介绍了书籍管理。

1.创建数据库

在这里插入图片描述

2.创建文件conn.php用于数据库的连接

<?php
/**
 * Created by PhpStorm.
 * User: zwq
 * Date: 2019/2/27
 * Time: 14:59
 */

function fun_conn($sql){
    $conn=mysqli_connect('localhost','root','123456','book_db');
    if(!$conn){ //如果失败
        die('连接mysql数据库失败'.$conn-> connect_errno); //显示出错误信息
    }
    mysqli_select_db($conn,'book');
    $result=mysqli_query($conn,$sql); //数据库操作
    mysqli_close($conn);
    return $result; //返回结果集
}

?>

3.创建文件index.php用于显示结果

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<table width="600" border="1">
    <tr>
        <th>书籍编号</th>
        <th>书籍名称</th>
        <th>出版社</th>
        <th>价格</th>
        <th>数量</th>
        <th>操作</th>
    </tr>
<?php
include_once "conn.php";
$sql ="select * from book";
$result= fun_conn($sql);
while($row = mysqli_fetch_array($result)) {
    echo "<tr>";
    echo "<td>{$row['id']}</td>";
    echo "<td>{$row['name']}</td>";
    echo "<td>{$row['press']}</td>";
    echo "<td>{$row['price']}</td>";
    echo "<td>{$row['number']}</td>";
    echo "<td>
  <a href='update.php?id={$row['id']}'>修改</a>
  <a href='javascript:del({$row['id']})'>删除</a>
 </td>";
    echo "</tr>";
}
?>
</table>
<a href="add.html">增加书籍</a>
<script type="text/javascript">
    function del(id) {
        if(confirm("确定删除??")){
            window.location ="del.php?id="+id;
        }

    }

</script>
</body>
</html>
页面如图

在这里插入图片描述

4.增加信息

4.1 创建add.html页面添加数据
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>添加</h1>
<form action="add.php" method="post">
    书名:<input type="text" name="name"><p></p>
    出版社:<input type="text" name="press"><p></p>
    价格:<input type="number" name="price"><p></p>
    数量:<input type="number" name="number"><p></p>
    <input type="submit" value="添加">
</form>
</body>
</html>
4.2创建处理增加信息的服务端文件add.php
<?php
/**
 * Created by PhpStorm.
 * User: zwq
 * Date: 2019/2/27
 * Time: 15:27
 */
include "conn.php";
$name =$_POST['name'];
$press =$_POST['press'];
$price =$_POST['price'];
$number =$_POST['number'];
$sql ="insert into book(name,press,price,number) value('$name','$press','$price','$number') ";
if(fun_conn($sql))
{
    echo "增加成功";
}

header("location:index.php"); //跳转到主页
如图所示

添加页面:
在这里插入图片描述
添加结果:
在这里插入图片描述

5.删除信息

<?php
/**
 * Created by PhpStorm.
 * User: zwq
 * Date: 2019/2/27
 * Time: 22:16
 */
include_once "conn.php";
$id =$_GET ['id']; //用get方法获取到要删除数据的id
$sql= "delete from book where id = '$id'";
fun_conn($sql);
header("location:index.php");

结果如图:
在这里插入图片描述
删除结果:
在这里插入图片描述

6.修改信息

6.1 点击修改按钮跳转到update.php进行修改
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
<?php
/**
 * Created by PhpStorm.
 * User: zwq
 * Date: 2019/2/27
 * Time: 21:47
 */
include_once "conn.php";
$id =$_GET['id'];
$sql="select * from book where id =$id";
$result= fun_conn($sql);
$row = mysqli_fetch_assoc($result);
?>
<h1>修改</h1>
<form action="do_update.php" method="post">
    ID<input readonly="readonly" type="text" name="id" value="<?php echo $row['id']?>"><br> //id只可读,不可修改
    书名:<input type="text" name="name" value="<?php echo $row['name']?>"><br>
    出版社:<input type="text" name="press" value="<?php echo $row['press']?>"><br>
    价格:<input type="number" name="price" value="<?php echo $row['price']?>"><br>
    数量:<input type="number" name="number" value="<?php echo $row['number']?>"><br>
    <input type="submit" value="修改">
</form>
</body>
</html>
6.2 通过服务端文件do_update.php进行修改处理
<?php
/**
 * Created by PhpStorm.
 * User: zwq
 * Date: 2019/2/28
 * Time: 15:36
 */
$id =$_POST['id'];
$name =$_POST['name'];
$press =$_POST['press'];
$price =$_POST['price'];
$number =$_POST['number'];

include_once 'conn.php';
$sql ="update book set name='$name',press='$press',price='$price',number='$number' where id= $id";
fun_conn($sql);
header("location:index.php");
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值