<?php //顯示所有數(shù)據(jù) //var_dump($_GET); //搜索開始 如果存在我們就拼接一個where條件即可 $where = ''; if (!empty($_GET['name'])) { //$where = " WHERE name LIKE '%".$_GET['name']."%' "; $where[] = "name LIKE '%{$_GET['name']}%'"; } if(!empty($_GET['age'])){ $where[]= "age = {$_GET['age']}"; } if (!empty($_GET['city'])) { $where[] = "city LIKE '%{$_GET['city']}%'"; } //var_dump($where); if(!empty($where)){ $where =' WHERE '.implode(' and ',$where); } //echo $where; //echo $where; $link = mysqli_connect('localhost','root','123456'); if (mysqli_connect_errno($link)>0) { echo mysqli_connect_error($link);exit; } mysqli_select_db($link,'ss34'); mysqli_set_charset($link,'utf8'); $sql="SELECT id,name,sex,age,city FROM info {$where}"; $result = mysqli_query($link,$sql); if ($result && mysqli_num_rows($result)>0) { //聲明一個新數(shù)組 $userlist = array(); while($row = mysqli_fetch_assoc($result)){ $userlist[]=$row; } } //var_dump($userlist); //定義一個變量用來聲明編號 $i=1; ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h3><a href="add.html">添加用戶</a></h3> <!-- 如果你要做搜索內(nèi)容 method="get" --> <form action="index.php" method="get"> 姓名: <input type="text" name="name"> 年齡: <input type="text" name="age"> <input type="submit" value="搜索"> </form> <hr> <table border="1" width="800" align="center"> <caption><h3>學(xué)員管理系統(tǒng)v1.0</h3></caption> <tr> <th>編號</th> <th>姓名</th> <th>年齡</th> <th>性別</th> <th>城市</th> <th>操作</th> </tr> <?php if(!empty($userlist)){?> <?php foreach($userlist as $value){?> <tr> <td><?php echo $i++?></td> <td><?php echo $value['name']?></td> <td><?php echo $value['age']?></td> <td> <?php $sex = $value['sex']; switch($sex){ case 0: echo '女'; break; case 1: echo '男'; break; case 2: echo '保密'; break; case 3: echo '琦琦'; break; default: echo '人妖'; } ?> </td> <td><?php echo $value['city']?></td> <td><a href="./del.php?id=<?php echo $value['id']?>" onclick="return confirm('數(shù)據(jù)無價 謹(jǐn)慎操作')">刪除</a>|<a href="edit.php?id=<?php echo $value['id']?>">修改</a></td> </tr> <?php } ?> <?php }else{?> <tr><td colspan="6">暫無數(shù)據(jù)</td></tr> <?php }?> </table> </body> </html>
點(diǎn)擊 "運(yùn)行實(shí)例" 按鈕查看在線實(shí)例
微信掃碼
關(guān)注PHP中文網(wǎng)服務(wù)號
QQ掃碼
加入技術(shù)交流群
Copyright 2014-2025 http://ipnx.cn/ All Rights Reserved | php.cn | 湘ICP備2023035733號