一. 数据库操作

DDL:

DML: insesrt update,delete

DQL: select

1 更新数据

  1. update tableName set field = newValue; // 更新全表数据
  2. update tableName set field = newValue where 条件; // 更新满足条件的数据
  3. update tableName set field = newValue,field = newValue; // 更新多个字段的全表数据

2. 删除数据 delete

delete from tableName;
delete from tableName where condition;

3. 删除数据 truncate

truncate tableName;

4. delete 和 truncate 的区别

1、truncate 命令的执行速度,快于 delete 命令。

2、delete 命令删除记录时,会记录大量的日志信息。

3、truncate 命令删除记录后,会恢复 auto_increment 默认值。

4、通过事务,无法恢复 truncate 命令删除的记录。

5.插入单条完整记录

insert into tableName values(value1, value2,....);

注意:插入数据时,值和表中字段列需要一一对应.

6. 插入单条指定字段数据的记录

insert into tableName(field1, field2,...) values(value1, value2,....);

注意: 查询时指定的字段和值需要一一对应.

7. 插入多条记录

全字段完整记录

insert into tableName values(value1, value2,....),(value1, value2,....),...;

指定字段的记录

insert into tableName(field1, field2,...) values (value1, value2,....),(value1, value2,....),...;

二.PDO操作

PHP 中,所有对数据的增删改操作,都使用 exec()方法执行

int PDO::exec(String sql);

三.案例练习(班级信息增删改)

classList.php

<?php
    header("content-type:text/html;charset=utf8");
    // 连接数据库
    $url = "mysql:host=mysql;dbname=database_lesson_32091_14_11223";
    $user = "lesson_32091_14_11223";
    $passwd = "95a08403e5048dcfc27bfd7425cf55ff";
    $db = new PDO($url,$user,$passwd);

    // 判断请求中是否有参数,如果有就是执行删除操作
    if(isset($_GET['name'])) {
        // 如果存在请求参数,取出并拼接SQL语句
        $sql = "delete from class where name = '{$_GET['name']}'";
        echo $sql;
        // 执行SQL
        $row = $db->exec($sql);
        // 如果执行结果影响行数大于0,说明操作成功
        if($row > 0){
            // 弹出弹框,并重定向到当前页面,目的是去除到页面中的请求参数
          echo "<script type='text/javascript'>alert('删除成功'); window.location='classList.php'</script>";
        }
        else
            echo "<script type='text/javascript'>alert('删除失败');</script>";
    }



    // 查询所有数据,包括在删除操作后的结果查询
    // 所以先判断是否应该删除,然后再查询结果
    $sql = "select * from class";
    $st = $db->query($sql);
    $res = $st->fetchAll(PDO::FETCH_ASSOC);
?>


<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
       *{
          margin: 0;
      }
      #content{
        width:1200px;
        margin:0 auto;
        background: #f2f2f2;
        text-align: center;
      }
      table{
          border-collapse: collapse;
          text-align: center;
          margin: 0 auto;
      }
      tr,th,td{
            border:1px solid black;
      }
  </style>
</head>

<body>
    <div id='content'>
        <h1>班级信息管理</h1>
        <table width='800px'>
            <tr>
                <th>班级名称</th>
                <th>班级人数</th>
                <th>操作</th>
            </tr>
            <?php foreach ($res as $value) { ?>
                <tr>
                    <td> <?php echo $value['name']; ?></td>
                    <td><?php echo rand(30,50) . "人"?></td>
                    <td>
                        <span>
                        <a href=<?php echo "upDataClass.php?name={$value['name']}";?> >修改</a>
                        </span>

                         <span>
                            <a href=<?php echo "classList.php?name={$value['name']}";?> >删除</a>
                        </span>
                    </td>
                </tr>
            <?php } ?>
        </table>
    </div>
</body>

</html>

upDataClass.php

<?php
    // 获取参数中的旧班级名
    $name = $_GET['name'];
    echo "oldName:", $name;
    // 
    if(isset($_GET['newname'])){
        $newname = $_GET['newname'];
        $num = $_GET['num'];
        // 判断输入的人数是否是数字
        if(strlen($newname) > 0 && is_numeric($num)){
            header("content-type:text/html;charset=utf8");
            // 连接数据库
            $url = "mysql:host=mysql;dbname=database_lesson_32091_14_11223";
            $user = "lesson_32091_14_11223";
            $passwd = "95a08403e5048dcfc27bfd7425cf55ff";
            $db = new PDO($url,$user,$passwd);

            // 修改数据
            $sql = "update class set name='{$newname}' where name = '{$name}'";
            echo $sql;
            $row = $db->exec($sql);
            // 判断是否修改成功
            if($row > 0){
                echo "<script>alert('修改成功'); window.location='classList.php';</script>";
            }else{
               echo "<script>alert('修改失败'); '</script>"; 
            }
        }else{
            echo "<script>alert('输入数据不合法'); </script>";
        }
    }


?>



<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
           *{
              margin: 0;
          }
          body{
              background: #f2f2f2;
          }
          #content{
            width:500px;
            margin:0 auto;
            background: #f2f2f2;
            text-align: center;
            border: 1px solid orange;
            margin-top: 100px;
            padding: 30px;
          }
          h1{
              margin-bottom: 20px;
          }
          span{
              display: inline-block;
              width:100px;
              height: 30px;
              text-align: left;
          }
          .inp{
              width:200px;
              height: 30px;
          }
          .btn{
              width:100px;
              height: 30px;
              background: blue;
              color:white;
              border-radius:5px;
          }
          p{
              height: 60px;
          }

        </style>
    </head>

    <body>
        <div id="content">
            <h1>修改班级信息</h1>
            <form action="upDataClass.php">
                <!--因为使用get方式传参数时,指定地址后不能写参数字符串
                所以解决办法,要么将提交方式改成post,要么使用隐藏域的方式 -->
                <!--下面添加一个隐藏域标签,在页面渲染时不显示,但是提交时会将数据一起提交上去-->
                <input type='hidden' name='name' value="<?php echo $name;?>" >
                <p>
                    <span class='title'>班级名称:</span>
                    <input class='inp' type="text" placeholder="请输入班级名称" name='newname'>
                </p>

                <p>
                     <span class='title'>人数:</span>
                    <input class='inp' type="text" placeholder="请输入班级人数" name='num'>
                </p>

                <input class='btn' type='submit' value="修改">
            </form>
        </div>
    </body>
</html>

addClass.php

<?php

    if(isset($_GET['newname'])){
        $newname = $_GET['newname'];
        $num = $_GET['num'];
        // 判断输入的人数是否是数字
        if(strlen($newname) > 0 && is_numeric($num)){
            header("content-type:text/html;charset=utf8");
            // 连接数据库
            $url = "mysql:host=mysql;dbname=database_lesson_32091_14_11223";
            $user = "lesson_32091_14_11223";
            $passwd = "95a08403e5048dcfc27bfd7425cf55ff";
            $db = new PDO($url,$user,$passwd);

            // 修改数据
            $sql = "insert into class(name) values('{$newname}')";
            echo $sql;
            $row = $db->exec($sql);
            // 判断是否修改成功
            if($row > 0){
                echo "<script>alert('添加成功'); window.location='classList.php';</script>";
            }else{
               echo "<script>alert('添加操作失败'); '</script>"; 
            }
        }else{
            echo "<script>alert('输入数据不合法'); </script>";
        }
    }   
?>



<!DOCTYPE html>
<html lang="en">

    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <style>
           *{
              margin: 0;
          }
          body{
              background: #f2f2f2;
          }
          #content{
            width:500px;
            margin:0 auto;
            background: #f2f2f2;
            text-align: center;
            border: 1px solid orange;
            margin-top: 100px;
            padding: 30px;
          }
          h1{
              margin-bottom: 20px;
          }
          span{
              display: inline-block;
              width:100px;
              height: 30px;
              text-align: left;
          }
          .inp{
              width:200px;
              height: 30px;
          }
          .btn{
              width:100px;
              height: 30px;
              background: blue;
              color:white;
              border-radius:5px;
          }
          p{
              height: 60px;
          }

        </style>
    </head>

    <body>
        <div id="content">
            <h1>修改班级信息</h1>
            <form action="addClass.php">
                <p>
                    <span class='title'>班级名称:</span>
                    <input class='inp' type="text" placeholder="请输入班级名称" name='newname'>
                </p>

                <p>
                     <span class='title'>人数:</span>
                    <input class='inp' type="text" placeholder="请输入班级人数" name='num'>
                </p>

                <input class='btn' type='submit' value="修改">
            </form>
        </div>
    </body>
</html>