简体   繁体   English

MySQL,PHP是否使用LIKE语法?

[英]MySQL, PHP Using LIKE Syntax?

I'm trying to make a user search with the following code: 我正在尝试使用以下代码进行用户搜索:

<?php
session_start();
include("../BD/bd.php");

$searched_for = $_POST['searched_for'];
$query = @mysql_query("SELECT * FROM user_media WHERE nombre LIKE '%$searched_for%'") or die(mysql_error());

while($got_users = @mysql_fetch_array($query)){
    echo '<div class="searched-content-info">'.
         '<div class="searched-photo"><img src="'.$got_users['foto'].'"></div>
         <div class="searched-names"><h3>'.$got_users['nombre'].'</h3></div>
         <div class="searched-dates"><h3>'.'Miembro desde: '.$got_users['created_on'].'</h3></div>  
         </div> 
         <div class="divisor-search-user"></div>';
}

?>

But I'm getting all the rows, I just want to display the searched users info, seems like the $query is receiving a clean $searched_for 但是我正在获取所有行,我只想显示搜索到的用户信息,似乎$query正在接收干净的$searched_for

Any help here? 这里有什么帮助吗? Btw, I'm a little newbie here, please don't bully :) 顺便说一句,我是这里的新手,请不要欺负:)

EDIT: I tried changing $got_users['nombre']; 编辑:我试图更改$got_users['nombre']; with $searched_for to see if $searched_for is empty and yes it doesn't return any string that's why I am getting all the rows. $searched_for查看$searched_for是否为空,是的,它不返回任何字符串,这就是为什么我要获取所有行的原因。 $query is getting an empty variable but Why? $query得到一个空变量,但是为什么呢?

Here's my HTML: 这是我的HTML:

<form target="u-n" id="search_input" action="search_user.php" method="post">
    <input id="search-input" name="searched_for" type="search" placeholder="Search">
</form>

You used <input type="search" /> which is a HTML5 feature. 您使用了HTML5功能的<input type="search" /> Older browsers may not support this. 较旧的浏览器可能不支持此功能。 Replace this input with type="text" . 将此输入替换为type="text"

Then, your $_POST['searched_for'] should populate properly, that is: 然后,您的$_POST['searched_for']应该正确填充,即:

<input name="searched_for" type="text" placeholder="Search" />

Also, you used the same id multiple times, which is an invalid HTML syntax. 另外,您多次使用相同的id ,这是无效的HTML语法。

Reference: HTML input tag at MDN 参考: MDN上的HTML输入标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM