0

I'm fairly new to MySQL and PHP and have been reading books and watching tutorials and trying examples but I'm stuck on getting this search query to work. I have a simple search form:

    <!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">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 5</title>
</head>

<body>
<h2>Search</h2>
 <form name="search" method="post" action="process_search.php">
Seach for: <input type="text" name="find" />
 <input type="submit" name="submit" value="search"/>
 </form>
</body>

</html>

and here is the part of the php form that processes the search request that I'm having issues with

$db_path ="localhost"; //---Host name or path to database
$db_username ="root"; //-----------------------------MySQL database username
$db_password = "password"; //----------------------------MySQL database password
$db_name = "database"; //--------------------------------MySQL database name
$tbl_name = "stuff"; //---------------------------------MySQL database table

// Connect to server and select databse
 mysql_connect("$db_path", "$db_username", "$db_password")or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

//filtering input for xss and sql injection
$input = @$_GET['find'];
$input = strip_tags( $input );
$input = mysql_real_escape_string( $input );
$input = trim( $input );





$sql = mysql_query("select * from $tbl_name WHERE first_name = '". $input . "'");

while ($rows = mysql_fetch_array($sql)){

I'm following the example given but the query doesn't return any results, it will display the table headers as it should but nothing in the tables, I added mysql_error(); to my code and it reports no errors. What am I doing wrong?

4

2 回答 2

0

您想将查询更改为...

$sql = mysql_query("select * from $tbl_name WHERE first_name LIKE '%". $input . "%'");

但是你真的不应该使用 mysql_*,它已被弃用,你应该使用 PDO 连接到数据库

编辑...

您的表单具有“发布”方法, 因此您应该使用此方法...

$input = $_POST['find'];

相信能解决你的问题!:)

编辑 2...

好的,您需要找出为什么您的表单没有发布变量...所以只需将其放在页面上...

 <form name="search" method="post" action="process_search.php">
  Seach for: <input type="text" name="find" />
  <input type="submit" name="submit" value="search"/>
 </form>

并且在您的 php 页面上只有...

<?php var_dump($_REQUEST);?>

告诉我你得到了什么......我会和你聊天,但你没有足够的代表点来聊天,因此需要进行多次编辑。

于 2013-06-07T20:59:36.507 回答
0

使用这个 - 我相信这有效。如果你仍然没有结果,你可能有另一个问题。

$db_path ="localhost"; //---Host name or path to database
$db_username ="root";  //-----------------------------MySQL database username
$db_password = "password"; //----------------------------MySQL database password
$db_name = "database"; //--------------------------------MySQL database name

// Connect to server and select databse
 mysql_connect("$db_path", "$db_username", "$db_password") or die("cannot connect"); 
 mysql_select_db("$db_name")or die("cannot select DB");

//filtering input for xss and sql injection
$input = mysql_real_escape_string(strip_tags(trim($_REQUEST['find'])));

$sql = mysql_query("SELECT * FROM stuff WHERE first_name='$input'");

while ($rows = mysql_fetch_array($sql)){
于 2013-06-07T21:00:15.133 回答