php实验4

1.act.php

<?php
//操作数据的增删改update

// .连接数据库
try {
    $pdo = new PDO("mysql:host=localhost;dbname=db;", "root", "lyf123456");

} catch (PDOException $e) {
    die("数据库连接失败" . $e->getMessage());
}
// .防止中文乱码
$pdo->query("SET NAMES 'UTF8'");
// .通过action的值进行对应操作
switch ($_GET['action']) {
    case 'add':
    {   //增加操作
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $hobby = $_POST['hobby'];
        $address = $_POST['address'];
        $remark = $_POST['remark'];

        //写sql语句
        $sql = "INSERT INTO info VALUES (NULL ,'{$name}','{$sex}','{$hobby}','{$address}','{$remark}')";
        $rw = $pdo->exec($sql);
        if ($rw > 0) {
            echo "<script> //alert('增加成功');
                               window.location='ex01b.php'; //跳转
                    </script>";
        } else {
            echo "<script> alert('增加失败');
                               window.history.back(); //返回上一页
                    </script>";
        }
        break;
    }
    case "del":
    {    // .获取表单信息
        //通过id删除信息
        $id = $_GET['id'];
        $sql = "DELETE FROM info WHERE id='{$id}'";
        $rw = $pdo->exec($sql);
        if ($rw > 0) {
            echo "<script> alert('数据删除成功!');
                               window.location='ex01b.php'; //跳转
                    </script>";
        } else {
            echo "<script> alert('数据删除失败!');
                               window.history.back(); //返回上一页
                    </script>";
        }

        break;
    }
    case "edit" :
    {   // .获取表单信息
        //原id
        $id = $_POST['id'];
        //表单信息
        //修改之后的信息
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $hobby = $_POST['hobby'];
        $address = $_POST['address'];
        $remark = $_POST['remark'];
        $sql = "UPDATE info SET name='{$name}',sex='{$sex}',address='{$address}',remark='{$remark}',hobby='{$hobby}' WHERE id='{$id}'";
        $rw = $pdo->exec($sql);
        if ($rw > 0) {
            echo "<script>alert('修改成功');window.location='ex01b.php'</script>";
        } else {
            echo "<script>alert('修改失败');window.history.back()</script>";
        }
        break;
    }

}

2.act02.php

<?php
session_start();
//操作数据库
try {
  //利用PDO类访问数据库
  $pdo = new PDO("mysql:host=localhost;dbname=db;", "root", "lyf123456");
} catch (PDOException $e) {
  die("数据库连接失败" . $e->getMessage());
}

$pdo->query("SET NAMES 'UTF8'");
switch ($_GET['action']) {
  //登录
  case 'login':
  {
    $name = $_POST['name'];
    $sex = $_POST['sex'];
    $str = "";
    if ($sex == '')
      $str = $name . ' 小姐';
    else
      $str = $name . ' 先生';
    $_SESSION['msg'] = $str;
    $sql = "SELECT *FROM stu WHERE name ='{$name}'";
    //查找出结果进入ex02c.php
    $stmt = $pdo->query($sql);//返回预处理对象
    if($stmt->rowCount()> 0 ){
      $stu = $stmt->fetch(PDO::FETCH_ASSOC);//按照关联数组进行解析
      //ex0c通过id查询信息
      $_SESSION['id']=$stu['id'];
      echo "<script>

            window.location='ex02c.php' </script>";
    } else {
      echo "<script>window.location='ex02b.php'</script>";
    }
    break;

  }


  //添加
  case 'add':
    {
      $name = $_POST['name'];
      $sex = $_POST['sex'];
      $hobby = $_POST['hobby'];
      $address = $_POST['address'];
      $age = $_POST['age'];
      $_SESSION['name']=$name;
      $sql = "INSERT INTO stu VALUES (NULL ,'{$sex}','{$name}','{$age}','{$address}','{$hobby}')";
      $rw = $pdo->exec($sql);
      if ($rw > 0) {
        echo "<script> alert('您的资料保存成功');
                               window.location='ex02c.php'; //跳转
                    </script>";
      } else {
        echo "<script> alert('增加失败');
                               window.history.back(); //返回上一页
                    </script>";
      }
    }
    break;

  case 'del':
  {
    $id = $_GET['id'];
    $sql = "DELETE FROM stu WHERE id='{$id}'";
    $rw = $pdo->exec($sql);
    if ($rw > 0) {
      echo "<script> alert('数据删除成功!');
                               window.location='ex02b.php'; //跳转
                    </script>";
    } else {
      echo "<script> alert('数据删除失败!');
                               window.history.back(); //返回上一页
                    </script>";
    }

    break;

  }


  case 'edit':
  {
    $id = $_POST['id'];
    $name = $_POST['name'];
    $sex = $_POST['sex'];
    $hobby = $_POST['hobby'];
    $address = $_POST['address'];
    $age = $_POST['age'];
    $sql = "UPDATE stu SET name='{$name}',sex='{$sex}',age='{$age}',address='{$address}',hobby='{$hobby}' WHERE id='{$id}'";
    $rw = $pdo->exec($sql);
    if ($rw > 0) {
      echo "<script>alert('修改成功');window.location='ex02b.php'</script>";
    } else {
      echo "<script>alert('修改失败');window.history.back()</script>";
    }
    break;
  }
}

3.ex01a.php

<html>
<head>
    <title>添加</title>
</head>
<body>
<center>
    <form id="addinfo" name="addinfo" method="post" action="act.php?action=add">
        <table>
            <tr style="background: #666666;font-size: 30px ;text-align: center">
                <td colspan="2">添加个人资料</td>
            </tr>
            <tr>
                <td>姓名</td>
                <td><input id="name" name="name" type="text"/></td>
            </tr>
            <tr>
                <td>性别</td>
                <td><input type="radio" name="sex" value=""/><input type="radio" name="sex" value=""/></td>
            </tr>

            <tr>

                <td>兴趣爱好</td>
                <td><input type="text" name="hobby" id="hobby"/></td>
            </tr>

            <tr>

                <td>家庭住址</td>
                <td><select id="address" name="address">
                        <option value="">请选择您的居住区域</option>
                        <option value="上海">上海</option>
                        <option value="广州">广州</option>
                        <option value="北京">北京</option>
                    </select></td>
            </tr>
            <tr>
                <td>备注</td>
                <td><textarea id="remark" name="remark" rows="5" cols="30">
</textarea></td>
            </tr>
            <tr>
                <td><input type="submit" value="提交"/>  </td>
                <td><input type="reset" value="重置"/>  
            </tr>
        </table>
    </form>
</center>
</body>
</html>

4.ex01b.php

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script>
        function doDel(id) {
            if (confirm("确定要删除么?")) {
                window.location = 'act.php?action=del&id=' + id;
            }
        }
    </script>
</head>
<style>

    th, td {
        width: 100%;
        word-break: keep-all; /* 不换行 */
        white-space: nowrap; /* 不换行 */
        overflow: hidden; /* 内容超出宽度时隐藏超出部分的内容 */
        text-overflow: ellipsis; /* 当对象内文本溢出时显示省略标记(...) ;需与overflow:hidden;一起使用。*/
    }
</style>
<body>
<center>
    <table width="600" border="1">
        <tr style="background: #666666;font-size: 30px ;text-align: center">
            <td colspan="6">学生个人信息表</td>
        </tr>
        <tr>

            <th>姓名</th>
            <th>性别</th>
            <th>爱好</th>
            <th>家庭住址</th>
            <th>备注</th>

        </tr>
        <?php
        //1.连接数据库
        try {
            $pdo = new PDO("mysql:host=localhost;dbname=db;", "root", "lyf123456");
        } catch (PDOException $e) {
            die("数据库连接失败" . $e->getMessage());
        }
        //2.解决中文乱码问题
        $pdo->query("SET NAMES 'UTF8'");
        //3.执行sql语句,并实现解析和遍历
        $sql = "SELECT * FROM info ";
        foreach ($pdo->query($sql) as $row) {
            echo "<tr>";
            echo "<td>{$row['name']}</td>";
            echo "<td>{$row['sex']}</td>";
            echo "<td>{$row['hobby']}</td>";
            echo "<td>{$row['address']}</td>";
            echo "<td>{$row['remark']}</td>";
            echo "<td>
                     <a href='javascript:doDel({$row['id']})'>删除</a>
                     <a href='ex01c.php?id=({$row['id']})'>修改</a>
                   </td>";
            echo "</tr>";
        }

        ?>

    </table>
</center>
</body>
</html>

5.ex01c.php

<html>
<head>
    <meta charset="UTF-8">
    <title>学生信息管理</title>

</head>
<body>
<center>
    <?php
    // .连接数据库
    try{
        $pdo = new PDO("mysql:host=localhost;dbname=db;","root","lyf123456");
    }catch(PDOException $e){
        die("数据库连接失败".$e->getMessage());
    }
    // .防止中文乱码
    $pdo->query("SET NAMES 'UTF8'");       // .拼接sql语句,取出信息
    $sql = "SELECT * FROM info WHERE id =".$_GET['id'];
    $stmt = $pdo->query($sql);//返回预处理对象
    if($stmt->rowCount()> 0 ){
        $stu = $stmt->fetch(PDO::FETCH_ASSOC);//按照关联数组进行解析
    }else{
        die("没有要修改的数据!");
    }
    ?>
    <form id="editstu" name="editstu" method="post" action="act.php?action=edit">
        <input type="hidden" name="id" id="id" value="<?php echo $stu['id'];?>"/>
        <table>
            <tr style="background: #666666;font-size: 30px ;text-align: center">
                <td colspan="2">修改学生信息</td>
            </tr>
            <tr>
                <td>姓名</td>
                <td><input id="name" name="name" type="text" value="<?php echo $stu['name'];?>"/></td>
            </tr>
            <tr>
                <td>性别</td>
                <td><input type="radio" name="sex" value="" /><input type="radio" name="sex" value="" /></td>
            </tr>

            <tr>

                <td>兴趣爱好</td>
                <td><input type="text" name="hobby" id="hobby" value="<?php echo $stu.['hobby']?>"/></td>
            </tr>

            <tr>

                <td>家庭住址</td>
                <td><select id="address" name="address" >
                        <option value ="">请选择您的居住区域</option>
                        <option value ="上海">上海</option>
                        <option value="广州">广州</option>
                        <option value="北京">北京</option>
                    </select></td>
            </tr>
            <tr>
                <td>备注</td>
                <td><textarea  id="remark" name="remark"  rows="5" cols="30">
</textarea></td>
            </tr>
            <tr>
                <td><input type="submit" value="修改"/>  </td>
            </tr>
        </table>

    </form>

</center>
</body>
</html>

 

posted @ 2022-05-26 19:57  Lindseyyip  阅读(27)  评论(0编辑  收藏  举报