6月14日学习日志

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

ex02a.php代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
</head>
<body>
<div align="center">
    <H3 style="color: mediumspringgreen;font-size: 25px">用户登录</H3>
    <!--实现登录功能-->
    <form action="act02.php?action=login" method="post">
        <table border="1">
            <tr>
                <td>姓名:</td>
                <td><input id="name" name="name" type="text"/></td>
            </tr>

            <tr>
                <td>性别:</td>
                <td><input type="radio" name="sex" value="男" checked/><input type="radio" name="sex" value="女"/></td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="提交"/>
                    <input type="reset" value="重置"/>
                </td>
            </tr>

        </table>
    </form>
</div>
</body>
</html>

ex02b.php代码

<html>
<head>
    <title>添加</title>
</head>
<body>
<center>
    <h2 style="color: #24fa26"><?php session_start() ; echo $_SESSION['msg']?>对不起,没有找到您的个人资料,请填写您的详细信息</h2>
    <hr color="red">
    <form id="addinfo" name="addinfo" method="post" action="act02.php?action=add">
        <table border="1">
            <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="checkbox" name="age" id="age" value="17"/>17岁
                    <input type="checkbox" name="age" id="age" value="18"/>18岁
                    <input type="checkbox" name="age" id="age" value="19"/>19岁
                    <input type="checkbox" name="age" id="age" value="20"/>20岁
                </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="hobby" name="hobby" rows="5" cols="30">
</textarea></td>
            </tr>
            <tr>
                <td><input type="submit" value="保存"/>  </td>
                <td><input type="reset" value="重置"/>  </td>
            </tr>
        </table>
    </form>
</center>
</body>
</html>

ex02c.php代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户信息</title>
</head>
<body>
<?php
session_start();
// .连接数据库
$conn=mysqli_connect("localhost","root","123456.","mydb",'3307') or die("数据库连接失败");
// .防止中文乱码
mysqli_query($conn,'set names utf8');      // .拼接sql语句,取出信息
$sql = "SELECT * FROM stu WHERE name ='".$_SESSION['name']."'";
$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];

}
?>
<h2 style="color: #24fa26;text-align: center">您的个人资料如下</h2>
<hr color="red">
<div align="center">
    <H3 style="font-size: 25px">学生个人信息表</H3>
    <table border="1">
        <tr>
            <td>您的姓名:</td>
            <td><input  id="name" name="name" value="<?php echo $stu[1] ?>"/></td>
        </tr>

        <tr>
            <td>性别:</td>
            <td><input type="text" name="sex" value="<?php echo $stu[2] ?>"/>
            </td>
        </tr>
        <tr>
            <td>年龄:</td>
            <td><input name="age" value="<?php echo $stu[3] ?>"/>

            </td>
        </tr>
        <tr>
            <td>家庭住址:</td>
            <td><input name="address" value="<?php echo $stu[4] ?>"/>
            </td>
        </tr>
        <tr>
            <td>特长爱好:</td>
            <td><input name="sex" value="<?php echo $stu[5] ?>"/>

            </td>
        </tr>
        <tr>
            <td colspan="2">
                <a href='ex02d.php?id=<?php echo $stu[0] ?>'>修改</a>
                <a href='act02.php?action=del&id=<?php echo $stu[0] ?>'>删除</a>
            </td>
        </tr>
    </table>
    <a style="align-self: center" href="ex02a.php">返回首页</a>
</div>


</body>
</html>

ex02d.php代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改信息</title>
</head>
<body>
<hr color="red">
<div align="center">
    <H3 style="font-size: 25px">修改信息</H3>
    <form action="act02.php?action=edit&id=<?php echo $_GET['id'] ?>" method="post">
        <table border="1">
            <tr>
                <td>您的姓名:</td>
                <td><input   type="text" id="name" name="name" /></td>
            </tr>

            <tr>
                <td>性别:</td>
                <td><input type="text" name="sex"/>
                </td>
            </tr>
            <tr>
                <td>年龄:</td>
                <td><input type="checkbox" name="age" id="age" value="17"/>17岁
                    <input type="checkbox" name="age" id="age" value="18"/>18岁
                    <input type="checkbox" name="age" id="age" value="19"/>19岁
                    <input type="checkbox" name="age" id="age" value="20"/>20岁
                </td>
            </tr>
            <tr>
                <td>家庭住址:</td>
                <td><input  type="text" name="address"/>
                </td>
            </tr>
            <tr>
                <td>特长爱好:</td>
                <td><input type="text" name="sex"/>

                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="submit" value="提交">
                </td>
            </tr>
        </table>
    </form>
</div>


</body>
</html>

act02.php代码

<?php
session_start();
// .连接数据库
$conn=mysqli_connect("localhost","root","123456","mydb",'3307') or die("数据库连接失败");
mysqli_query($conn,'set names utf8');
// .通过action的值进行对应操作
switch ($_GET['action']) {
    //登录
    case 'login':
    {
        $name = $_POST['name'];
        $sex = $_POST['sex'];
        $str = "";
        if ($sex == '男')
            $str = $name . '先生';
        else
            $str = $name . '女士';
        $_SESSION['msg'] = $str;
        $_SESSION['name'] = $name;
        $sql = "SELECT *FROM stu WHERE NAME ='".$name."'";
        //查找出结果进入ex02c.php
        $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];
            $_SESSION['id']=$stu[0];
            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 (name,sex,age,address,hobby) VALUES ('".$name."','".$sex."','".$age."','".$address."','".$hobby."')";
            $rw =mysqli_query($conn,$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 =$conn->query($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."'";
        $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-14 21:17  张笑天  阅读(37)  评论(0编辑  收藏  举报