doAction.php页面,提交按钮,弹出添加成功之后

来源:2-7 用户管理之添加用户

坚持MyDream

2016-12-07 16:33

在跳转回userList.php页面前的一瞬间,闪过一个错误提示页面,只有一瞬间,但不知道为什么,我把代码贴出来,大家帮我看看= =

写回答 关注

3回答

  • 坚持MyDream
    2016-12-07 16:35:31

    //这是addUser.php页面代码

    <!DOCTYPE html>

    <html>

    <head>

        <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

        <title>添加用户</title>

    </head>

    <body>

    <h2>添加用户</h2>

    <form action="doAction.php?act=addUser" method="post">

    <table cellpadding="0" cellspacing="0" bgcolor="#ABCDEF" width="45%" border="1">

        <tr>

            <td>用户名</td>

            <td><input type="text" name="username" placeholder="请输入用户名" required="required"></td>

        </tr>

        <tr>

            <td>密码</td>

            <td><input type="password" name="password" placeholder="请输入密码" required="required"></td>

        </tr>

        <tr>

            <td>年龄</td>

            <td><input type="text" name="age" min="1" max="125" placeholder="请输入年龄"></td>

        </tr>

        <tr>

            <td colspan="2"><input type="submit" name="" value="提交"></td>

        </tr>

    </table>

    </form>

    </body>

    </html>


  • 坚持MyDream
    2016-12-07 16:35:05

    //这是doAction.php页面

    <?php

    header('content-type;text/html;charset=utf-8');

    $con=new mysqli('localhost','root','','test');

    if($con->connect_errno){

    die('ERROR:'.$con->connect_error);

    }

    $username=$_POST['username'];

    $username=$con->escape_string($username);

    $password=md5($_POST['password']);

    $age=$_POST['age'];

    $act=$_GET['act'];

        

        switch($act){

        case addUser:

        $sql="INSERT user(username,password,age) VALUES('$username','$password','$age')";

        $res=$con->query($sql);

        if($res){

        $insert_id=$con->insert_id;

        echo "<script type='text/javascript'> 

        alert('添加成功,你是网站的第{$insert_id}位用户');

        location.href='userList.php';

         </script>";

        }else{

        echo "<script> 

        alert('添加失败,重新添加');

        location.href='addUser.php';

         </script>";

        }

        break;

        }

    ?>

  • 坚持MyDream
    2016-12-07 16:34:18

    //这是userList页面代码

    <?php

    $con=new mysqli('localhost','root','','test');

    if($con->connect_errno){

    die('ERROR: '.$con->connect_error);

    }

    $sql="SELECT id,username,age FROM user";

    $res=$con->query($sql);

    if($res&&$res->num_rows>0){

    $row=$res->fetch_all(MYSQLI_ASSOC);

    }

    ?>


    <!DOCTYPE html>

    <html>

    <head>

    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">

    <title>用户列表</title>

    </head>

    <body>

    <h2>用户列表-<a href="addUser.php">添加用户</a></h2> 

    <table border='1' cellspacing="0" cellpadding="0" width="80%" bgcolor="#ABCDEF">

    <tr>

    <td>用户编号</td>

    <td>用户名</td>

    <td>年龄</td>

    <td>操作</td>

    </tr>

    <?php foreach ($row as $value): ?>

    <tr>

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

    <td><?php echo $value['username']?></td>

    <td><?php echo $value['age']?></td>

    <td><a href="doAction.php">更新</a>|<a href="delete.php">删除</a></td>

    </tr>

    <?php endforeach; ?>

    </table>

    </body>

    </html>


Duang~MySQLi扩展库来袭

本教程从面向对象和面向过程两个方面为你开启MySQLi学习之旅

28643 学习 · 181 问题

查看课程

相似问题