duanju8431 2013-05-06 16:40
浏览 24

创建一个php搜索栏[关闭]

I want to put a search box so that when you type in a persons name it selects that persons details from the database.

This is my search box:

<html>
<body>

<form action="users.php" method="GET">
<input id="search" type="text" placeholder="Type here">
<input id="submit" type="submit" value="Search">
</form>
</body>
</html>

Then here is my PHP to return the users:

<?php 

$connection = mysql_connect("localhost","root","");

mysql_select_db("blog1")or die(mysql_error());


$result = mysql_query("SELECT username FROM member");
 while ($row = mysql_fetch_assoc($result)) {
echo "<div id='link' onClick='addText(\"".$row['username']."\");'>" . $row['username'] . "</div>";  
 }


  ?>

How to get it to just return the user that i type in the search bar instead of all the users? Any help would be great as I am just learning PHP.

  • 写回答

5条回答 默认 最新

  • doukui9491 2013-05-06 16:43
    关注

    You need to add a WHERE clause to your SQL query to filter for users with matching usernames (using =) or similar usernames (using LIKE). For example:

    SELECT * FROM users WHERE username LIKE '%querystring%'
    

    Will return all users whose username contains "querystring".

    Also, as you're likely to be bombarded with in the comments, the mysql_* functions are deprecated. You should at least switch to mysqli_*, or even better, switch to PDO.

    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探