5.22php

实验四 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 页面中的学生的信息值。当点击“修改”按钮时,可以将该学生的信息进行修改,并保存到数据库中如图4,成功保存数据后,可将页面转至 ex01b.php如图5

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

 

3

 

4

 

5

 

6

 

7

ex01a.php

<?php

include('./conn.php');

if(!empty($_POST)){

    $name=$_POST['name'];

    $sex=$_POST['sex'];

    $hobby=$_POST['hobby'];

    $address=$_POST['address'];

    $remark=$_POST['remark'];

    $sql = "insert into student(name,sex,hobby,address,remark) values('$name','$sex','$hobby','$address','$remark')";

    $result = mysqli_query($conn, $sql) or die("数据插入失败!");

    header("location:ex01b.php");

}

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>添加个人信息</title>

</head>

 

<body>

        <div align="center">

        <h1>添加个人信息</h1>

    <form name="addForm" method="post" action="#">

        <p>

            <label for="name" class="label">真实姓名:</label>

            <input id="name" name="name" type="text" class="input" />

        <p/>

        <p>

            <label for="sex" class="label">性别:</label>

            <input name="sex" type="radio" value="男" >男

            <input name="sex" type="radio"value="女">女

        <p/>

        <p>

            <label for="hobby" class="label">兴趣爱好:</label>

            <input id="hobby" name="hobby" type="text" class="input" />

        <p/>

        <p>

            <label for="address" class="label">家庭住址:</label>

            <select name="address">

                <option value="">请选择您的居住区域</option>

                <option value="A区">A区</option>

                <option value="B区">B区</option>

                <option value="C区">C区</option>

                <option value="D区">D区</option>

            </select>

        <p/>

        <p>

            <label for="remark" class="label">备注:</label>

            <textarea name="remark" cols="30" rows="6"></textarea>

        </p>

        <p>

            <input type="reset" value=" 重 置 "/>

            <input type="submit" name="submit" value="  提  交  " class="left" />

        </p>

    </form>

        </div>

</body>

</html>

ex01b.php

<?php

include('./conn.php');

$sql="select * from student";

$result = mysqli_query($conn, $sql) or die("数据查询失败!");

if(!empty($_GET)){

    $delete=$_GET['delete'];

    if($delete='1'){

        echo "<script>alert('删除成功!');</script>";

    }else{

        echo "<script>alert('删除失败!');</script>";

    }

 

}

?>

 

<!DOCTYPE html>

<html>

<head>

    <meta charset="UTF-8">

    <title>学生管理系统</title>

    <script type="text/javascript">

        function deleteStudent(id) {

            if (confirm("您是否要删除该项?")) {

                location.href = "ex01b_delete.php?id="+id;

            }

        }

    </script>

</head>

<body>

<div>

    <h1 align="center">学生个人信息表</h1>

    <h3 align="center">

        <a href="ex01a.php">添加学生信息</a>

    </h3>

</div>

<div >

    <table style="width: 100%;">

        <thead>

        <tr>

            <th><p align="left">编号</p></th>

            <th><p align="left">姓名</p></th>

            <th><p align="left">性别</p></th>

            <th><p align="left">兴趣爱好</p></th>

            <th><p align="left">家庭住址</p></th>

            <th><p align="left">备注</p></th>

            <th colspan="2"><p align="left">操作</p></th>

 

        </tr>

 

        </thead>

        <tbody>

        <?php

        while($row=mysqli_fetch_array($result)){

        ?>

        <tr>

            <td><?php echo $row['id'];?></td>

            <td><?php echo $row['name'];?></td>

            <td><?php echo $row['sex'];?></td>

            <td><?php echo $row['hobby'];?></td>

            <td><?php echo $row['address'];?></td>

            <td><?php echo $row['remark'];?></td>

 

            <td><a

                    href="ex01c.php?id=<?php echo $row['id'];?>">修改</a></td>

            <td><a href="javascript:;" onclick="deleteStudent(<?php echo $row['id'];?>)">删除</a></td>

        </tr>

            <?php

        }

        ?>

        </tbody>

    </table>

</div>

</body>

</html>

ex01b_delete.php

<?php

include('./conn.php');

if(!empty($_GET)){

    $id=$_GET['id'];

    $sql_d="delete from student where id='$id'";

    $result_d = mysqli_query($conn, $sql_d) or die("数据查询失败!");

    if($result_d){

        header("location:ex01b.php?delete=1");

    }else{

        header("location:ex01b.php?delete=0");

    }

}

ex01b_update.php

<?php

include('./conn.php');

 

if(!empty($_POST) ){

    $id=$_POST['id'];

    $name=$_POST['name'];

    $sex=$_POST['sex'];

    $hobby=$_POST['hobby'];

    $address=$_POST['address'];

    $remark=$_POST['remark'];

    $sql="update  student set name='$name',sex='$sex',hobby='$hobby',address='$address',remark='$remark' where id='$id'";

    $result = mysqli_query($conn, $sql) or die("数据更新失败!");

    $row=mysqli_fetch_array($result);

    header("location:ex01b.php");

}

 

ex01c.php

<?php

include('./conn.php');

if(!empty($_GET) ){

    $id=$_GET['id'];

    $sql="select * from student where id='$id'";

    $result = mysqli_query($conn, $sql) or die("数据查询失败!");

    $row=mysqli_fetch_array($result);

}

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>修改学生信息</title>

</head>

 

<body>

<div align="center">

    <form name="addForm" method="post" action="ex01b_update.php">

        <input name="id" value="<?php echo $row['id'];?>" hidden="hidden">

        <p>

            <label for="name" class="label">真实姓名:</label>

            <input id="name" name="name" type="text" class="input" value="<?php echo $row['name'];?>"/>

        <p/>

        <p>

            <label for="sex" class="label">性别:</label>

            <input name="sex" type="radio" value="男"

                <?php

                    if($row['sex']=='男'){

                        echo "checked='checked'";

                    }

 

                ?>

            >男

            <input name="sex" type="radio" value="女"

                <?php

                if($row['sex']=='女'){

                    echo "checked='checked'";

                }

 

                ?>

            >女

        <p/>

        <p>

            <label for="hobby" class="label">兴趣爱好:</label>

            <input id="hobby" name="hobby" type="text" class="input" value="<?php echo $row['hobby'];?>"/>

        <p/>

        <p>

            <label for="address" class="label">家庭住址:</label>

            <select name="address">

                <option value="">请选择您的居住区域</option>

                <option value="A区"

                    <?php

                    if($row['address'] == 'A区') {

                        echo "selected = 'selected'";

                    } ?>

                >A区</option>

                <option value="B区"

                    <?php

                    if($row['address'] == 'B区') {

                        echo "selected = 'selected'";

                    } ?>

                >B区</option>

                <option value="C区"

                    <?php

                    if($row['address'] == 'C区') {

                        echo "selected = 'selected'";

                    } ?>

                >C区</option>

 

                <option value="C区"

                    <?php

                    if($row['address'] == 'D区') {

                        echo "selected = 'selected'";

                    } ?>

                >D区</option>

            </select>

            

        <p/>

        <p>

            <label for="remark" class="label">备注:</label>

            <textarea name="remark" cols="30" rows="6">

                <?php echo $row['remark'];?>

            </textarea>

        </p>

        <p>

            <input type="reset" value="重置"/>

            <input type="submit" name="submit" value="  确 定  " class="left" />

        </p>

    </form>

    </div>

</body>

</html>

conn.php

<?php

$conn=mysqli_connect("localhost","root","738450","newsql") or die("数据库连接失败");

mysqli_query($conn,'set names utf8');

?>

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

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

 

 

8

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

ex02c.php如图9

 

9

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

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

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

 

10

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

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

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

 

图11

 

12

ex02a.php

<?php

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>用户登入</title>

 

 

 

</head>

 

<body>

 

        <div align="center">

    <form name="loginForm" method="post" action="check.php" >

        <p>

            <label for="name" class="label">用户名:</label>

            <input id="name" name="name" type="text" class="input" />

        <p/>

        <p>

            <label for="sex" class="label">性别:</label>

            <input name="sex" type="radio" value="男" >男

            <input name="sex" type="radio"value="女">女

        <p/>

        <p>

            <input type="reset" value="重置"/>

            <input type="submit" name="submit" value="  确 定  " class="left" />

        </p>

    </form>

    </div>

</body>

</html>

ex02b.php

<?php

include('./conn.php');

$info='';

if(!empty($_GET)){

    if(isset($_GET['name'])){

        $name=$_GET['name'];

        $sex=$_GET['sex'];

        if($sex=='女'){

            $info="$name"."小姐,您好,对不起,没有找到您的个人资料,请填写您的详细信息!";

        }else{

            $info="$name"."先生,您好,对不起,没有找到您的个人资料,请填写您的详细信息!";

        }

    }

 

    if(isset($_GET['delete'])){

        if($_GET['delete']=='1'){

            echo "<script>alert('删除成功!');</script>";

        }else{

            echo "<script>alert('删除失败!');</script>";

        }

    }

}

if(!empty($_POST)){

    $name=$_POST['name'];

    $sex=$_POST['sex'];

    $love=$_POST['love'];

    $address=$_POST['address'];

    $age=$_POST['age'];

    $sql = "insert into user2(name,sex,age,address,love) values('$name','$sex','$age','$address','$love')";

//    echo $sql;

    $result = mysqli_query($conn, $sql) or die("数据插入失败!");

    header("location:ex02a.php");

}

 

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>添加个人信息</title>

</head>

 

<body>

<div>

    <h2 style="color: #2fab31" align="center">

        <?php echo $info;?>

    </h2>

    <hr style="border-color: red;"/>

</div>

<div align="center">

    <form name="addForm" method="post" action="#">

        <p>

            <label for="name" class="label">真实姓名:</label>

            <input id="name" name="name" type="text" class="input" />

        <p/>

        <p>

            <label for="sex" class="label">性别:</label>

            <input name="sex" type="radio" value="男" >男

            <input name="sex" type="radio"value="女">女

        <p/>

        <p>

            <label for="age" class="label">年龄:</label>

            <input name="age" type="checkbox" value="20" >20岁

            <input name="age" type="checkbox"value="21">21岁

            <input name="age" type="checkbox"value="22">22岁

            <input name="age" type="checkbox"value="23">23岁

        <p/>

        <p>

            <label for="address" class="label">家庭住址:</label>

            <select name="address">

                <option value="">请选择您的居住区域</option>

                <option value="A区">A区</option>

                <option value="B区">B区</option>

                <option value="C区">C区</option>

                <option value="D区">D区</option>

            </select>

        <p/>

 

        <p>

            <label for="love" class="label">特长爱好:</label>

            <textarea name="love" cols="30" rows="6"></textarea>

        </p>

        <p>

            <input type="reset" value="重置"/>

            <input type="submit" name="submit" value="  确 定  " class="left" />

        </p>

    </form>

</div>

</body>

</html>

ex02c.php

<?php

include('./conn.php');

$info='';

if(!empty($_GET)){

    if(isset($_GET['name'])){

        $name=$_GET['name'];

        $sex=$_GET['sex'];

        if($sex=='女'){

            $info="$name"."小姐,您好,对不起,没有找到您的个人资料,请填写您的详细信息!";

        }else{

            $info="$name"."先生,您好,对不起,没有找到您的个人资料,请填写您的详细信息!";

        }

    }

 

    if(isset($_GET['delete'])){

        if($_GET['delete']=='1'){

            echo "<script>alert('删除成功!');</script>";

        }else{

            echo "<script>alert('删除失败!');</script>";

        }

    }

}

if(!empty($_POST)){

    $name=$_POST['name'];

    $sex=$_POST['sex'];

    $love=$_POST['love'];

    $address=$_POST['address'];

    $age=$_POST['age'];

    $sql = "insert into user2(name,sex,age,address,love) values('$name','$sex','$age','$address','$love')";

//    echo $sql;

    $result = mysqli_query($conn, $sql) or die("数据插入失败!");

    header("location:ex02a.php");

}

 

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>添加个人信息</title>

</head>

 

<body>

<div>

    <h2 style="color: #2fab31" align="center">

        <?php echo $info;?>

    </h2>

    <hr style="border-color: red;"/>

</div>

<div align="center">

    <form name="addForm" method="post" action="#">

        <p>

            <label for="name" class="label">真实姓名:</label>

            <input id="name" name="name" type="text" class="input" />

        <p/>

        <p>

            <label for="sex" class="label">性别:</label>

            <input name="sex" type="radio" value="男" >男

            <input name="sex" type="radio"value="女">女

        <p/>

        <p>

            <label for="age" class="label">年龄:</label>

            <input name="age" type="checkbox" value="20" >20岁

            <input name="age" type="checkbox"value="21">21岁

            <input name="age" type="checkbox"value="22">22岁

            <input name="age" type="checkbox"value="23">23岁

        <p/>

        <p>

            <label for="address" class="label">家庭住址:</label>

            <select name="address">

                <option value="">请选择您的居住区域</option>

                <option value="A区">A区</option>

                <option value="B区">B区</option>

                <option value="C区">C区</option>

                <option value="D区">D区</option>

            </select>

        <p/>

 

        <p>

            <label for="love" class="label">特长爱好:</label>

            <textarea name="love" cols="30" rows="6"></textarea>

        </p>

        <p>

            <input type="reset" value="重置"/>

            <input type="submit" name="submit" value="  确 定  " class="left" />

        </p>

    </form>

</div>

</body>

</html>

ex02d.php

<?php

include('./conn.php');

if(!empty($_GET)){

    $id=$_GET['id'];

    $sql="select * from user2 where id='$id'";

    $result = mysqli_query($conn, $sql) or die("数据查询失败!");

    $row=mysqli_fetch_array($result);

}

 

?>

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>修改个人信息</title>

</head>

 

<body>

 

<div align="center">

    <form name="addForm" method="post" action="update2.php">

        <input name="id" value="<?php echo $row['id'];?>" hidden="hidden">

        <p>

            <label for="name" class="label">真实姓名:</label>

            <input id="name" name="name" type="text" class="input" value="<?php echo $row['name'];?>"/>

        <p/>

        <p>

            <label for="sex" class="label">性别:</label>

            <input name="sex" type="radio" value="男"

                <?php

                if($row['sex']=='男'){

                    echo "checked='checked'";

                }

 

                ?>

            >男

            <input name="sex" type="radio" value="女"

                <?php

                if($row['sex']=='女'){

                    echo "checked='checked'";

                }

 

                ?>

            >女

        <p/>

        <p>

            <label for="age" class="label">年龄:</label>

            <input name="age" type="checkbox" value="20"

                <?php

                if($row['age']=='20'){

                    echo "checked='checked'";

                }

                ?>

            >20岁

            <input name="age" type="checkbox"value="21"

                <?php

                if($row['age']=='21'){

                    echo "checked='checked'";

                }

                ?>

            >21岁

            <input name="age" type="checkbox"value="22"

                <?php

                if($row['age']=='22'){

                    echo "checked='checked'";

                }

                ?>

            >22岁

            <input name="age" type="checkbox"value="23"

                <?php

                if($row['age']=='23'){

                    echo "checked='checked'";

                }

                ?>

            >23岁

        <p/>

        <p>

            <label for="address" class="label">家庭住址:</label>

            <select name="address">

                <option value="">请选择您的居住区域</option>

                <option value="A区"

                    <?php

                    if($row['address'] == 'A区') {

                        echo "selected = 'selected'";

                    } ?>

                >A区</option>

                <option value="B区"

                    <?php

                    if($row['address'] == 'B区') {

                        echo "selected = 'selected'";

                    } ?>

                >B区</option>

                <option value="C区"

                    <?php

                    if($row['address'] == 'C区') {

                        echo "selected = 'selected'";

                    } ?>

                >C区</option>

                <option value="D区"

                    <?php

                    if($row['address'] == 'D区') {

                        echo "selected = 'selected'";

                    } ?>

                >D区</option>

            </select>

        <p/>

 

        <p>

            <label for="love" class="label">特长爱好:</label>

            <textarea name="love" cols="30" rows="6">

                <?php echo $row['love'];?>

            </textarea>

        </p>

        <p>

            <input type="reset" value="重置"/>

            <input type="submit" name="submit" value="  确 定  " class="left" />

        </p>

    </form>

    </div>

</body>

</html>

update2.php

<?php

include('./conn.php');

if(!empty($_POST)){

    $id=$_POST['id'];

    $name=$_POST['name'];

    $sex=$_POST['sex'];

    $love=$_POST['love'];

    $address=$_POST['address'];

    $age=$_POST['age'];

    $sql="update  user2 set name='$name',sex='$sex',love='$love',address='$address',age='$age' where id='$id'";

    echo $sql;

    $result = mysqli_query($conn, $sql) or die("数据更新失败!");

    header("location:ex02c.php?id=$id&update=1");

}

check.php

<?php

include('./conn.php');

if(!empty($_POST)){

    $name=$_POST['name'];

    $sex=$_POST['sex'];

    $sql = "select * from user2 where name='$name'and sex='$sex'";

    $result = mysqli_query($conn, $sql) or die("数据插入失败!");

    $row=mysqli_fetch_array($result);

    if(count($row)==0){

        header("location:ex02b.php?name=$name&sex=$sex");

    }else{

        $id=$row['id'];

        header("location:ex02c.php?id=$id");

    }

}

delete.php

<?php

include('./conn.php');

if(!empty($_GET)){

    $id=$_GET['id'];

    $sql_d="delete from user2 where id='$id'";

    $result_d = mysqli_query($conn, $sql_d) or die("数据查询失败!");

    if($result_d){

        header("location:ex02b.php?delete=1");

    }else{

        header("location:ex02b.php?delete=0");

    }

}

 

posted @ 2021-05-22 21:28  居酿圆子  阅读(82)  评论(0编辑  收藏  举报