添加失败,notice id未定义

//doaction.php
<?php
header('content-type:text/html;charset=utf-8');
$mysqli= new mysqli('localhost','root','127656','class');
if($mysqli->connect_errno){
    die($mysqli->connect_error);
}
$mysqli->set_charset('utf8');

$username=$_POST['username'];
$username=$mysqli->escape_string($username);
$password=($_POST['password']);
$age=$_POST['age'];
$act=$_GET['act'];
$id=$_GET['id'];
switch($act){
        case 'adduser':
        $sql="INSERT user(username,password,age) VALUES('{$username}','{$password}','{$age}')";
        $res=$mysqli->query($sql);
        if($res){
            $insert_id=$mysqli->insert_id;
            echo "<script type='text/javascript'>
                alert('添加成功,网站的第{$insert_id}位用户');
                location.href='userlist.php';
            </script>";
            exit;
        }
        else{
            echo "<script type='text/javascript'>
                alert('添加失败,重新添加');
                location.href='adduser.php';
            </script>";
            exit;
        }
        break;
        case 'deluser':
        //echo '删除记录'.$id;
        $sql=" DELETE FROM user WHERE id=".$id;
        $res=$mysqli->query($sql);
        if($res){
            $mes='删除成功';
        }
        else{
            $mes ='删除失败';
        }
        $url='userlist.php';
        echo "<script type='text/javascript'>
                alert('{$mes}');
                location.href='{$url}';
            </script>";
            exit;
        break;
   }
?>

//userlist.php
<?php
header('content-type:text/html;charset=utf-8');
$mysqli=new mysqli('localhost','root','127656','class');
if($mysqli->connect_errno){
    die('CONNECT ERROR:'.$mysqli->connect_error);
}
$mysqli->set_charset('utf-8');
$sql="SELECT id,userName,age FROM user";
$mysqli_result=$mysqli->query($sql);
if($mysqli_result && $mysqli_result->num_rows>0){
    while($row=$mysqli_result->fetch_assoc()){
        $rows[]=$row;
    }
}
print_r($rows);
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    
    </head>
    <body>
        <h2>用户列表-<a href="adduser.php">添加用户</a></h2>
        <table border="1" cellpadding="0" cellspacing="0" width="80%" bgcolor="#abcdef">
            <tr>
                <td>编号</td>
                <td>用户名</td>
                <td>年龄</td>
                <td>操作</td>
            </tr>
            <?php $i=1;foreach($rows as $row):?>
            <tr>
                <td><?php echo $i;?></td>
                <td><?php echo $row['userName'];?></td>
                <td><?PHP echo $row['age'];?></td>
                <td><a href="edituser.php">更新</a>|<a href="doaction.php?act=deluser&id=<?php 
                    echo $row['id'];?>">删除</a></td>
            </tr>
            <?php $i++; endforeach;?>
        </table>
    </body>
</html>

//adduser.php
<! DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    
    </head>
    <body>
        <form action="doaction.php?act=adduser" method='post'>
            <table border="1" cellpadding="0" cellspacing="0" width="80%" bgcolor="#abcdef">
            <tr>
                <td>用户名</td>
                <td><input type="text" name="username" id="" placeholder="请输入合法用户名" required="required"/></td>
            </tr>
              <tr>
                <td>密码</td>
                <td><input type="password" name="password" id="" placeholder="请输入密码" required="required"/></td>
            </tr>
              <tr>
                <td>年龄</td>
                <td><input type="number" name="age" id="" min='1' max="125" placeholder="请输入年龄" required="required"/></td>
            </tr>
              <tr>
                <td colspan="2"><input type="submit" value="添加用户"/></td>
            </tr>
            </table>
    </body>
</html>


qq_Onecallaway_1
浏览 1305回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP