PHP 初学之登录查询小case
说明:如误入本文,请忽略即可,内容仅为记录。
功能:登录(不验证),查询所有列表,删除记录。--很简单,仅为熟悉代码。
// MySQL,新建数据库data,导入如下sql
SET FOREIGN_KEY_CHECKS=0; -- ---------------------------- -- Table structure for stu -- ---------------------------- DROP TABLE IF EXISTS `stu`; CREATE TABLE `stu` ( `stuid` int(11) NOT NULL AUTO_INCREMENT, `stuname` varchar(20) NOT NULL, `stusex` char(1) NOT NULL, `stuage` tinyint(4) NOT NULL, `stuadd` varchar(100) NOT NULL, PRIMARY KEY (`stuid`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COMMENT='学生表'; -- ---------------------------- -- Records of stu -- ---------------------------- INSERT INTO `stu` VALUES ('1', '篱笆', '男', '18', '北京'); INSERT INTO `stu` VALUES ('2', '绾绾', '女', '13', '上海');
//新建login页面
1 <?php 2 if (isset($_POST['username']) && isset($_POST['pwd'])) { 3 header("location:welcome.php?username={$_POST['username']}"); 4 }else{ 5 echo '用户名或密码不能为空!<br/>'; 6 } 7 ?> 8 9 <form action="" method="post"> 10 <table> 11 <tr> 12 <td>用户名:</td> 13 <td>密码</td> 14 </tr> 15 <tr> 16 <td><input type="text" name="username"></td> 17 <td><input type="password" name="pwd"></td> 18 </tr> 19 <tr> 20 <td><input type="submit" name="submit"></td> 21 </tr> 22 </table> 23 24 </form>
//新建welcome页面
1 welcome! user: 2 <b> 3 <?php 4 if (isset($_GET['username'])) { 5 echo $_GET['username']; 6 } 7 ?> 8 </b> 9 10 <br/> 11 12 <script type="text/javascript"> 13 function go(){ 14 location.href='list.php'; 15 } 16 </script> 17 <input type="button" value="query list" onclick="go()"/>
//新建list页面
1 <script type="text/javascript"> 2 function del(id){ 3 location.href='del.php?id='+id; 4 } 5 </script> 6 7 <table> 8 <tr> 9 <th>stuid</th> 10 <th>stuname</th> 11 <th>stusex</th> 12 <th>stuage</th> 13 <th>stuadd</th> 14 </tr> 15 16 <?php 17 18 $link = mysqli_connect('localhost','yourusername','yourpwd') or die(mysql_error()); 19 mysqli_select_db($link, 'data') or die(mysql_error); 20 mysqli_query($link, 'set names utf8'); 21 22 $rs = mysqli_query($link, 'select * from stu'); 23 24 while ($row = mysqli_fetch_array($rs)) { 25 $id = $row['stuid']; 26 echo "<tr>"; 27 echo "<td>".$row['stuid']."</td>"; 28 echo "<td>".$row['stuname']."</td>"; 29 echo "<td>".$row['stusex']."</td>"; 30 echo "<td>".$row['stuage']."</td>"; 31 echo "<td>".$row['stuadd']."</td>"; 32 echo "<td>"."<a href='javascript:void()' onclick='del({$id})'>删除(不要点啊)</a>"."</td>"; 33 34 echo "</tr>"; 35 } 36 37 ?> 38 </table>
//新建del页面
1 <?php 2 if(isset($_GET['id'])){ 3 $link = mysqli_connect('localhost','yourusername','yourpwd') or die(mysql_error); 4 mysqli_select_db($link, 'data') or die(mysql_error); 5 $rs = mysqli_query($link, "delete from stu where stuid = {$_GET['id']}"); 6 var_dump($rs); 7 8 echo "已经成功删除了id为{$_GET['id']}的记录<br/>"; 9 } 10 ?>