6月13日学习日志

今天完成了php的数据库操作实验第一部分。

ex01a.php代码

<html>
<head>
    <title>添加</title>
</head>
<body>
<center>
    <form method="post" action="act01.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>

ex01b.php代码

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <title></title>
    <script>
        function doDel(id) {
            if (confirm("确定要删除么?")) {
                window.location = 'act01.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>
            <th>操作</th>
        </tr>
        <?php
        //1.连接数据库
        $conn=mysqli_connect("localhost","root","123456","mydb",'3307') or die("数据库连接失败");
        //2.解决中文乱码问题
        mysqli_query($conn,'set names utf8');
        //3.执行sql语句,并实现解析和遍历
        $sql="select * from info";
        $result=mysqli_query($conn,$sql)or die("数据查询失败");
        while($row=mysqli_fetch_row($result)){
            $cols=count($row);
            echo "<tr>";
            for($i=0;$i<$cols-1;$i++){
                echo "<td>$row[$i]</td>";
            }
            echo "<td>
                     <a href='javascript:doDel({$row[5]})'>删除</a>
                     <a href='ex01c.php?id=({$row[5]})'>修改</a>
                   </td>";
            echo "</tr>";
        }

        ?>

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

ex01c.php代码

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

</head>
<body>
<center>
    <?php
    // .连接数据库
    $conn=mysqli_connect("localhost","root","123456","mydb",'3307') or die("数据库连接失败");
    // .防止中文乱码
    mysqli_query($conn,'set names utf8');      // .拼接sql语句,取出信息
    $sql = "SELECT * FROM info WHERE id =".$_GET['id'];
    $result=mysqli_query($conn,$sql)or die("数据查询失败");
    if($row=mysqli_fetch_row($result)){
        $cols=count($row);
        for($i=0;$i<$cols;$i++)
            $stu[$i]=$row[$i];

    }else{
        die("没有要修改的数据!");
    }
    ?>
    <form id="editstu" name="editstu" method="post" action="act01.php?action=edit">
        <input type="hidden" name="id" id="id" value="<?php echo $stu[5];?>"/>

        <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[0];?>"/></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[2]?>"/></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>
<?php
//操作数据的增删改查

// .连接数据库
$conn=mysqli_connect("localhost","root","123456","mydb",'3307') or die("数据库连接失败");
mysqli_query($conn,'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 (name,sex,hobby,address,remark) VALUES ('".$name."','".$sex."','".$hobby."','".$address."','".$remark."')";
        $rw =mysqli_query($conn,$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 =$conn->query($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."'";
        $result=mysqli_query($conn,$sql)or die("数据更新失败".mysqli_error($conn));
        if ($result > 0) {
            echo "<script>alert('修改成功');window.location='ex01b.php'</script>";
        } else {
            echo "<script>alert('修改失败');window.history.back()</script>";
        }
        break;
    }

}

 

posted @ 2021-06-13 20:35  张笑天  阅读(42)  评论(0编辑  收藏  举报