实验四 PHP 操作数据库


实验目的:

1、掌握在PHP中连接数据库

2、掌握常用的 MYSQL 数据库函数的用法

3、熟练基本 SQL 语句的使用

实验内容

1、 按下列要求完成各个页面:(提示:在做此题前需先建立一数据库,包含一张表,此表至少包含 5 个字段(姓名,性别,兴趣爱好,家庭住址,备注))(1)制作静态页面 ex01a.php 如图 1 所示,当点击“提交”按钮时,可以向数据库中添加数据,如果添加数据失败,能给出提示,如果添加成功,则跳转到另一页面ex01b.php,如图 2 所示,本页面可以把数据库中所有的学生的资料显示出来。

 

图 1

 

图 2

(2) 当点击 ex01b.php 中的“修改”时,可将网页连接到 ex01c.php 如图 3 所示,可以修改学生的信息。该页面包含一个表单,每个表单控件的默认值均为ex01.php 页面中的学生的信息值。当点击“修改”按钮时,可以将该学生的信息进行修改,并保存到数据库中,成功保存数据后,可将页面转至 ex01b.php。

(3) 当点击“删除”时,如果能成功删除数据,则给出提示“数据删除成功!”,如图4所示,并将网页转至ex01b.php。数据删除失败也给出提示“数据删除失败!”。

 

图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>

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
        $conn=mysqli_connect(‘localhost:3306’,’root’ ,’dhl111′);
        if($conn){
            $select = mysqli_select_db($conn,”test”);
            if($select){
                $sql=”select * from php”;
                mysqli_query($conn,’SET NAMES UTF8′);
                $ret = mysqli_query($conn,$sql);
                $datarow=mysqli_num_rows($ret);
                for ($i=0;$i<$datarow;$i++){
                   $sqlarr=mysqli_fetch_assoc($ret);
                echo ‘<tr>’;
                echo ‘<td>’.$sqlarr[‘name’].'</td>’;
                echo ‘<td>’.$sqlarr[‘sex’].'</td>’;
                echo ‘<td>’.$sqlarr[‘hobby’].'</td>’;
                echo ‘<td>’.$sqlarr[‘address’].'</td>’;
                echo ‘<td>’.$sqlarr[‘remark’].'</td>’;
                echo “<td><a href=’ex01c.php?id=”.$sqlarr[‘id’].”‘>修改</a>    <a href=’ex01d.php?id=”.$sqlarr[‘id’].”‘>删除</a></td>”;
                echo ‘</tr>’;
                }
            }
        }
        ?>

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

ex01c.php

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

</head>
<body>
<center>
    <?php
    // .连接数据库
    try{
        $pdo = new PDO(“mysql:host=localhost;dbname=test;”,”root”,”root”);
    }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>

act.php

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

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

} 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;
    }
}

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

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

} 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、 按下列要求完成各个页面:(提示:此题的数据库中的表有 5 个字段(姓名,性别,年龄,家庭住址,特长爱好))

(1) 建立一个登陆页面 ex02a.php,如图 5 所示。

 

 

图 5

(2) 如果你输入的个人资料(姓名和性别)在数据库中能够找到则将页面转到

ex02c.php。

(3) 如果你输入个人资料在数据库中找不到,则在 ex02b.php 页面上半部分会给出提示“XXX 先生(小姐),您好,对不起,没有找到您的个人资料,请填写您的详细信息!”,其中“XXX”为你输入的姓名,当你输入的性别为“男”时则显示“先生”,当你输入的性别为“女”时则显示“小姐”,如图 6 所示。

(4) 在 ex02b.php 页面的下半部分做一表单,如图 6 所示,包含“真实姓名”、“性别”、“年龄”、“家庭住址”、“特长爱好”等项目。

(5) 当点击保存按钮时,可以将你输入的信息保存到数据库中。如果保存成功则给出提示“您的资料保存成功!”,且能返回 ex02a.php 页面。

 

 

图 6

(6) 在 ex02c.php 页面中,在网页的上方插入一条红色水平线,在水平线上方书写文字:“您的个人资料如下:”,如图 7 所示。

(7) 在水平线下方建一表单,如图 7 所示,可以将你在 ex02a.php 网页中输入的这位同学的资料从数据库中调出并显示出来。

(8) 在表格的最下方建立“修改”和“删除”超链接,当点击“修改”时,可将网页连接到 ex02d.php 修改学生信息的页面,当点击“删除”时,如果能成功删除数据,则给出提示“数据删除成功!”,如图 8 所示,并将网页转至 ex02b.php。数据删除失败也给出提示“数据删除失败!”

 

图7

 

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();
// .连接数据库
try {
    $pdo = new PDO(“mysql:host=localhost;dbname=test;”, “root”, “root”);
} catch (PDOException $e) {
    die(“数据库连接失败” . $e->getMessage());
}
// .防止中文乱码
$pdo->query(“SET NAMES ‘UTF8′”);       // .拼接sql语句,取出信息
$sql = “SELECT * FROM stu WHERE name =”.$_SESSION[‘name’];

$stmt = $pdo->query($sql);//返回预处理对象
if ($stmt->rowCount() > 0) {
    $stu = $stmt->fetch(PDO::FETCH_ASSOC);
}
?>
<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[‘name’] ?>”/></td>
        </tr>

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

            </td>
        </tr>
        <tr>
            <td>家庭住址:</td>
            <td><input name=”address” value=”<?php echo $stu[‘address’] ?>”/>
            </td>
        </tr>
        <tr>
            <td>特长爱好:</td>
            <td><input name=”sex” value=”<?php echo $stu[‘hobby’] ?>”/>

            </td>
        </tr>
        <tr>
            <td colspan=”2″>
                <a href=’ex02d.php?id=<?php echo $stu[‘id’] ?>’>修改</a>
                <a href=’act02.php?action=del&id=<?php echo $stu[‘id’] ?>’>删除</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();
//操作数据库
try {
    //利用PDO类访问数据库
    $pdo = new PDO(“mysql:host=localhost;dbname=test;”, “root”, “root”);
} 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=’ex01b.php'</script>”;
        } else {
            echo “<script>alert(‘修改失败’);window.history.back()</script>”;
        }
        break;
    }
}

 

原创文章,作者:306829225,如若转载,请注明出处:https://blog.ytso.com/267438.html

(0)
上一篇 2022年6月15日
下一篇 2022年6月15日

相关推荐

发表回复

登录后才能评论