安照2-6和2-7课上讲的代码完全写了一遍还是出问题,不知如何是好所以才麻烦老师费点心了
userlist.php 代码 麻烦老师帮看一下运行的时候显示
<?php header('content-type:text/html;charset=utf-8'); $mysqli = new mysqli('127.0.0.1','root','YOUNI258','books'); if($mysqli->connect_errno){ die($mysqli->connect_error); } $mysqli->set_charset('utf8'); $sql="select * from user;"; $mysqli_result=$mysqli->query($sql); if($mysqli_result && $mysqli_result->num_rows>0){ while($row=$mysqli_result->fetch_assoc()){ $rows[]=$row; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title>Document</title> </head> <h2>用户列表-<a href="adduser.php">添加用户</a></h2> <body> <table border="1" cellpadding="0" cellspacing="0" bgcolor="aqua" width="80%"> <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="update.php">更新</a>|<a href="deluser.php">删除</a></td> </tr> <?php $i++; endforeach;?> </table> </body> </html>
adduser.php这个文件的代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <title>Document</title> </head> <body> <h2>添加用户</h2> <form action="action.php?act=adduser" method="post"> <table border="1" cellpadding="0" cellspacing="0" bgcolor="aquamarine" width="80%"> <tr> <td>用户名</td> <td><input type="text" name="username" id="username" required="required" placeholder="请输入合法用户名" /></td> </tr> <tr> <td>密码</td> <td><input type="password" name="password" id="password" required="required" placeholder="请输入密码" /></td> </tr> <tr> <td>年龄</td> <td><input type="number" name="age" id="age" required="required" placeholder="请输入合法年龄" /></td> </tr> <tr> <td colspan="2"><input type="submit" value="添加用户" /></td> </tr> </table> </form> </body> </html>
这个是action.php的代码
这是action.php运行时出现的错误,我的mysql密码是YOUNI258 帐号是root 数据库是books
这是我数据库的数据
mysql> select * from user;
+----+----------+----------+-----+
| id | username | password | age |
+----+----------+----------+-----+
| 1 | 第一个 | | 18 |
| 2 | 第二个 | | 19 |
| 3 | 第三个 | | 20 |
| 4 | 第四个 | | 21 |
| 5 | 第五个 | | 22 |
| 6 | 第六个 | | 23 |
| 7 | 第七个 | | 24 |
| 8 | 第八个 | | 25 |
| 9 | 第九个 | | 26 |
mysql> desc user;
+----------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------------+------+-----+---------+----------------+
| id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(20) | NO | | NULL | |
| password | varchar(20) | NO | | NULL | |
| age | tinyint(3) unsigned | NO | | NULL | |
+----------+---------------------+------+-----+---------+----------------++----+----------+----------+-----+