每周总结04

ex01_del

<?php
// 1. 接收传递过来的name
if(empty($_GET['name'])){
    echo $_GET['name'];
    exit('<h1>连接数据库失败</h1>');
}
$name = $_GET['name'];
// 2. 连接数据库
$link = mysqli_connect('localhost', 'root', '123456', 'db_php4');
mysqli_query($link,"set names 'utf8';");
// 3. 删除该条数据
$query = mysqli_query($link,"delete from student where name = '$name'");
// 4. 查询失败的处理
if (!$query) {
    exit('<h1>查询数据失败</h1>');
}
// 5. 受影响的行数
$affected_rows = mysqli_affected_rows($link);
// 6. 删除失败
if ($affected_rows <= 0) {
    exit('<h1>删除失败</h1>');
}else{

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

}


header('location:ex01b.php');
?>

 

ex01_update

<?php

if(empty($_GET['name'])){
    echo $_GET['name'];
    exit('<h1>连接数据库失败</h1>');
}

$name = $_GET['name'];

// 2. 连接数据库
$link = mysqli_connect('localhost', 'root', '123456', 'db_php4');
mysqli_query($link,"set names 'utf8';");
// 3. 删除该条数据
$query = mysqli_query($link,"select * from student where name = '$name'");

$row=mysqli_fetch_assoc($query);

$name1 = $row['name'];
$hobby = $row['hobby'];
$address = $row['address'];

mysqli_close($link);
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个人资料修改</title>
</head>

<body style="text-align: center;margin-top: 50px;margin-left: 700px">
<form action="ex01_update2.php" method="post">
    <table border="1px" cellspacing="0" width="40%">
        <tr>
            <th colspan="2">添加个人资料</th>
        </tr>
        <tr>
            <td>真实姓名:</td>
            <td><input type="text" name="name" value="<?php echo $name1 ?>"></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" value="<?php echo $hobby ?>"></td>
        </tr>
        <tr>
            <td>家庭住址:</td>
            <td><select name="address" id="">
                    <option value="<?php echo $address ?>"><?php echo $address ?></option>
                    <option value="江东区">江东区</option>
                    <option value="长安区">长安区</option>
                    <option value="江北区">江北区</option>
                </select></td>
        </tr>
        <tr>
            <td>备注:</td>
            <td><textarea name="describe" id="" cols="30" rows="10"></textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="提交" onclick="window.location.href='ex01_update2.php'">
                <input type="reset" value="重置">
            </td>

        </tr>

    </table>


</form>


</body>
</html>

<?php

?>

 

ex01_update2

<?php
$link = mysqli_connect('localhost', 'root', '123456', 'db_php4');
mysqli_query($link,"set names 'utf8';");
$name2 = $_POST['name'];
$sex1 = $_POST['sex'];
$hobby1 = $_POST['hobby'];
$address1 = $_POST['address'];

$query1 = mysqli_query($link, "UPDATE student SET name ='$name2',sex='$sex1',hobby='$hobby1',address='$address1' WHERE name='$name2'");

if (!$query1) {
    exit('<h1>查询数据失败</h1>');
}
$affected = mysqli_affected_rows($link);
if($affected!==1){
    exit('<h1>找不到你要编辑的数据</h1>');
}

echo "<script>alert('数据修改成功')</script>";
header('Location:ex01b.php');
mysqli_close($link);
?>

 

ex01a

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个人资料添加</title>
</head>

<body style="text-align: center;margin-top: 50px;margin-left: 700px">
<form action="" method="post">
    <table border="1px" cellspacing="0" width="40%">
        <tr>
            <th colspan="2">添加个人资料</th>
        </tr>
        <tr>
            <td>真实姓名:</td>
            <td><input type="text" name="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" ></td>
        </tr>
        <tr>
            <td>家庭住址:</td>
            <td><select name="address" id="">
                    <option value="江东区">江东区</option>
                    <option value="长安区">长安区</option>
                    <option value="江北区">江北区</option>
                </select></td>
        </tr>
        <tr>
            <td>备注:</td>
            <td><textarea name="describe" id="" cols="30" rows="10"></textarea></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="提交">
                <input type="reset" value="重置">
            </td>

        </tr>

    </table>


</form>


</body>
</html>



<?php


$con=mysqli_connect("localhost","root","123456","db_php4");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysqli_select_db($con,"db_php4");

mysqli_set_charset($con,"UTF-8");

@$name = $_POST['name'];
@$sex = $_POST['sex'];
@$hobby = $_POST['hobby'];
@$address = $_POST['address'];
@$describe = $_POST['describe'];

$result=mysqli_query($con,"select name from student where name = '$name'");

$row=mysqli_fetch_assoc($result);


ob_start();
if($name != '') {
    if($name == $row['name'])
    {
        //检查用户是否已经存在
        echo "<script>alert('用户名.$name.已经存在!请重新添加用户')</script>";

    }else {

        $sql_insert = "insert into student(name,sex,hobby,address) values('$name','$sex','$hobby','$address')";
        $query=mysqli_query($con, $sql_insert);
        header("location:ex01b.php");
    }
}


mysqli_close($con);

 

ex01b

<?php
$con=mysqli_connect("localhost","root","123456","db_php4");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysqli_select_db($con,"db_php4");

mysqli_set_charset($con,"UTF-8");

$result=mysqli_query($con,"select * from student ");

$row=mysqli_fetch_all($result, MYSQLI_ASSOC);



//while($row){
//    foreach ($row as $value){
//        echo $value;
//    }
//    echo $row['name'].$row['sex'].$row['hobby'].$row['address'].$row['describe'];


?>

<!doctype html>

<html lang="en">
<head>
    <meta charset="UTF-8">

    <title>学生个人信息表</title>
</head>
<body style="text-align: center;margin-left: 700px;margin-top: 50px;">
    <form action="" method="post">
        <table border="1" cellspacing="0" width="40%">
            <tr>
                <th colspan="6">学生个人信息表</th>
            </tr>
            <tr>
                <td>姓名</td>
                <td>性别</td>
                <td>爱好</td>
                <td>家庭住址</td>
                <td>备注</td>
                <td>&nbsp;&nbsp;</td>
            </tr>
            <?php

//                foreach ($row as $value) {
//                echo "<tr>".$value;
//                foreach ($value as $v ){
//                    echo "<td>" . $v. "</td>";
//                    echo "<td>" . "<a href=\"ex01a.php\">修改</a>&nbsp;<a href=\"ex01b.php\" id=\"delete\">删除</a>" . "</td>" . "</tr>";
//                }
//            }


            foreach ($row as $key => $value) {

                foreach ($value as $k => $v) {

                    $arr[$k] = $v;

                }

                echo "<tr>";


                echo "<td>{$arr['name']}</td>";

                echo "<td>{$arr['sex']}</td>";

                echo "<td>{$arr['hobby']}</td>";

                echo "<td>{$arr['address']}</td>";

                echo "<td>{$arr['describe']}</td>";


                echo "<td>
                         <a href='ex01_update.php?name={$arr['name']}'>修改</a>
                         
                         <a href='ex01_del.php?name={$arr['name']}'>删除</a>
                    </td>";

                echo "</tr>";
            }
            ?>
        </table>
<!--            <tr>-->
<!--                <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['describe']?><!--</td>-->
<!--                <td><a href="ex01a.php">修改</a>&nbsp;<a href="ex01b.php" id="delete">删除</a></td>-->
<!--            </tr>-->

                </form>
</body>
</html>
<?php mysqli_close($con);

 

ex02_del

<?php
// 1. 接收传递过来的name
if(empty($_GET['name'])){
echo $_GET['name'];
exit('<h1>连接数据库失败</h1>');
}
$name = $_GET['name'];
// 2. 连接数据库
$link = mysqli_connect('localhost', 'root', '123456', 'db_php5');
mysqli_query($link,"set names 'utf8';");
// 3. 删除该条数据
$query = mysqli_query($link,"delete from user where name = '$name'");
// 4. 查询失败的处理
if (!$query) {
exit('<h1>查询数据失败</h1>');
}
// 5. 受影响的行数
$affected_rows = mysqli_affected_rows($link);
// 6. 删除失败
if ($affected_rows <= 0) {
exit('<h1>删除失败</h1>');
}else{

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

}


header('location:ex02b.php');
?>

 

ex02_update

<?php
$link = mysqli_connect('localhost', 'root', '123456', 'db_php5');
mysqli_query($link,"set names 'utf8';");
$name = $_POST['name'];
$sex = $_POST['sex'];
$age = $_POST['age'];
$hobby = $_POST['hobby'];
$address = $_POST['address'];

$query1 = mysqli_query($link, "UPDATE user SET name ='$name',sex='$sex',age='$age',address='$address',hobby='$hobby' WHERE name='$name'");

if (!$query1) {
    exit('<h1>查询数据失败</h1>');
}
$affected = mysqli_affected_rows($link);
if($affected!==1){
    exit('<h1>找不到你要编辑的数据</h1>');
}

echo "<script>alert('数据修改成功')</script>";
header('Location:ex02c.php');
mysqli_close($link);
?>

 

ex02a

 <!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>用户登录</title>
</head>
<body style="text-align: center;margin-top: 50px">
<form action="" method="post">
    <h2>用户登录</h2>
    <table border="1" cellspacing="0" align="center" width="25%" >
        <tr>
            <td>姓名:</td>
            <td><input type="text" name="name"></td>
        </tr>
        <tr>
            <td>性别:</td>
            <td>
                <input type="radio" name="sex" value="先生"><input type="radio" name="sex" value="小姐"></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="提交">
                <input type="reset" value="重置">
            </td>
        </tr>

    </table>

</form>

</body>
</html>

<?php

$con=mysqli_connect("localhost","root","123456","db_php5");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysqli_select_db($con,"db_php5");

mysqli_set_charset($con,"UTF-8");

session_start();
// 声明一个名为 admin 的变量,并赋空值。

@$name = $_POST["name"];
@$sex = $_POST['sex'];

$_SESSION["name"] = $name;
$_SESSION["sex"] = $sex;

$result=mysqli_query($con,"select name from user where name = '$name'");

$row=mysqli_fetch_assoc($result);

ob_start();
if($name!= '') {
    if($name == $row['name']){
        header("location:ex02c.php");

    }else {

        header("location:ex02b.php");
    }
}


mysqli_close($con);

 

ex02b

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加个人资料</title>
</head>
<body>
    <div>
        <?php
        session_start();
            $name0 = $_SESSION['name'];
            $sex0 = $_SESSION['sex'];

        ?>
        <span style="color: #00aa00"><?php echo $name0.$sex0."您好,对不起,没有找到您的个人资料,请填写您的详细信息!" ?></span>
        <hr color="red">
    </div>

<div align="center">
    <form action="ex02b.php" method="post">
        <table border="1" cellspacing="0" width="30%">
            <tr>
                <th colspan="2">添加个人资料</th>
            </tr>
            <tr>
                <td>真实姓名:</td>
                <td><input type="text" name="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="checkbox" name="age" value="17">17
                    <input type="checkbox" name="age" value="18">18
                    <input type="checkbox" name="age" value="19">19
                    <input type="checkbox" name="age" value="20">20
                </td>
            </tr>
            <tr>
                <td>家庭住址:</td>
                <td><select name="address" id="">
                        <option value="长安区">长安区</option>
                        <option value="江西区">江西区</option>
                        <option value="江北区">江北区</option>
                    </select></td>
            </tr>
            <tr>
                <td>特长爱好:</td>
                <td><textarea name="hobby" id="" cols="30" rows="10"></textarea></td>
            </tr>

            <tr>
            <td colspan="2" style="text-align: center"><input type="submit" value="保存">
                <input type="reset" value="重置"></td>
            </tr>
        </table>
    </form>

</div>


</body>
</html>

<?php

$con=mysqli_connect("localhost","root","123456","db_php5");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysqli_select_db($con,"db_php5");

mysqli_set_charset($con,"UTF-8");

//session_start();
@$name = $_POST["name"];
@$sex = $_POST["sex"];
@$age = $_POST["age"];
@$address = $_POST['address'];
@$hobby = $_POST['hobby'];

$result=mysqli_query($con,"select name from user where name = '$name'");

$row=mysqli_fetch_assoc($result);

ob_start();
if($name !='') {
    if($name == $row['name']){

        echo "<script>alert('用户名.$name.已经存在!请重新注册')</script>";
    }else {
        $sql_insert = "insert into user(name,sex,age,address,hobby) values('$name','$sex','$age','$address','$hobby')";
        mysqli_query($con, $sql_insert);
        header("location:ex02a.php");
    }
}

mysqli_close($con);
?>

 

ex02c

<?php
session_start();

$name = $_SESSION['name'];

$con=mysqli_connect("localhost","root","123456","db_php5");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}
mysqli_select_db($con,"db_php5");

mysqli_set_charset($con,"UTF-8");

$result=mysqli_query($con,"select * from user where name = '$name'");

$row=mysqli_fetch_assoc($result);

$sex = $row['sex'];
$age = $row['age'];
$address = $row['address'];
$hobby = $row['hobby'];



?>




<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个人资料显示</title>
</head>
<body>
<div>
    <span style="color: #00aa00">您的个人资料如下:</span>
    <hr color="red">
</div>

<div>
    <form action="" method="post">
        <table align="center" border="1"cellspacing="0" width="25%" style="text-align: center">
            <tr>
                <th colspan="2">学生个人信息表</th>
            </tr>
            <tr>
                <td>您的姓名:</td>
                <td><?php  echo $name?></td>
            </tr>
            <tr>
                <td>性别:</td>
                <td><?php echo $sex ?></td>
            </tr>
            <tr>
                <td>年龄:</td>
                <td><?php echo $age ?></td>
            </tr>
            <tr>
                <td>家庭住址:</td>
                <td><?php echo $address ?></td>
            </tr>
            <tr>
                <td>特长爱好:</td>
                <td><?php echo $hobby ?></td>
            </tr>
            <tr>
                <td colspan="2" style="text-align: center">
                    <a href="ex02d.php?name=<?php echo $name ?>">修改</a>
                    <a href="ex02_del.php?name=<?php echo $name ?>">删除</a>
                </td>
            </tr>
        </table>
    </form>
</div>

</body>
</html>

<?php
mysqli_close($con);
?>

 

ex02d

<?php
if(empty($_GET['name'])){
    echo $_GET['name'];
    exit('<h1>连接数据库失败</h1>');
}

$name = $_GET['name'];

// 2. 连接数据库
$link = mysqli_connect('localhost', 'root', '123456', 'db_php5');
mysqli_query($link,"set names 'utf8';");
// 3. 删除该条数据
$query = mysqli_query($link,"select * from user where name = '$name'");

$row=mysqli_fetch_assoc($query);

$name1 = $row['name'];
$hobby = $row['hobby'];
$address = $row['address'];
$sex = $row['sex'];

mysqli_close($link);
?>

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>修改个人资料</title>
</head>
<body>
<div>
    <?php
    session_start();
    $name0 = $_SESSION['name'];
    $sex0 = $_SESSION['sex'];

    ?>
    <span style="color: #00aa00"><?php echo $name0.$sex0."您好,您可以在这里修改您的信息!" ?></span>
    <hr color="red">
</div>

<div align="center">
    <form action="ex02_update.php" method="post">
        <table border="1" cellspacing="0" width="30%">
            <tr>
                <th colspan="2">修改个人资料</th>
            </tr>
            <tr>
                <td>真实姓名:</td>
                <td><input type="text" name="name" value="<?php echo $name1?>"></td>
            </tr>
            <tr>
                <td>性别:</td>
                <td>
                    <input type="radio" name="sex" value="男" checked><input type="radio" name="sex" value="女"></td>
            </tr>
            <tr>
                <td>年龄:</td>
                <td>
                    <input type="checkbox" name="age" value="17" checked>17
                    <input type="checkbox" name="age" value="18">18
                    <input type="checkbox" name="age" value="19">19
                    <input type="checkbox" name="age" value="20">20
                </td>
            </tr>
            <tr>
                <td>家庭住址:</td>
                <td><select name="address" id="">
                        <option value="<?php echo $address?>"><?php echo $address?></option>
                        <option value="长安区">长安区</option>
                        <option value="江西区">江西区</option>
                        <option value="江北区">江北区</option>
                    </select></td>
            </tr>
            <tr>
                <td>特长爱好:</td>
                <td><textarea name="hobby" id="" cols="30" rows="10"><?php echo $hobby?></textarea></td>
            </tr>

            <tr>
                <td colspan="2" style="text-align: center"><input type="submit" value="保存" onclick="window.location.href='ex02_update.php5'">
                    <input type="reset" value="重置"></td>
            </tr>
        </table>
    </form>

</div>


</body>
</html>

 

posted @ 2022-06-14 20:33  年年ℓ  阅读(39)  评论(0)    收藏  举报