0

我已经制作了一个搜索框,以便您可以输入您希望获得其信息的产品 ID。当我在产品 ID 框中输入数据时,没有返回结果,有人知道我做错了什么吗?我认为 'while ($row = mysql_fetch_array($result)) {' 是错误的,但不太确定,因为我尝试过的一切都不起作用。

  <div class="searchbox">
    <form action="Search.php" method="get">
       <fieldset>
       <input name="search" id="search" placeholder="Search for a Product" type="text" />
         <input id="submit" type="button" />
      </fieldset>
    </form>
 </div>
 <div id="content">
 <ul>        
 <?php

 // connect to the database
    include('base.php');


 $search = mysql_real_escape_string($_GET['search']);
 $query = "SELECT * FROM Product WHERE ProductID LIKE '%{$search}%'";
 $result = mysql_query($query); 
 while ($row = mysql_fetch_array($result)) {
 echo "<li><span class='name'><b>{$row['ProductID']}</b></span></li>";
 }
4

3 回答 3

0

您可以使用:

$result = mysql_query($query) or die($query."<br/><br/>".mysql_error());

确认数据正在返回。

于 2013-03-11T23:36:52.490 回答
0

不要使用 mysql 特定的语法,它已经过时了,以后会给你带来真正的麻烦,特别是如果你决定使用 sqlite 或 postgresql。

使用 PDO 连接,您可以像这样初始化一个:

// Usage:   $db = connectToDatabase($dbHost, $dbName, $dbUsername, $dbPassword);
// Pre:     $dbHost is the database hostname, 
//          $dbName is the name of the database itself,
//          $dbUsername is the username to access the database,
//          $dbPassword is the password for the user of the database.
// Post:    $db is an PDO connection to the database, based on the input parameters.
function connectToDatabase($dbHost, $dbName, $dbUsername, $dbPassword)
{
    try
    {
         return new PDO("mysql:host=$dbHost;dbname=$dbName;charset=UTF-8", $dbUsername, $dbPassword);
    }
    catch(PDOException $PDOexception)
    {
        exit("<p>An error ocurred: Can't connect to database. </p><p>More preciesly: ". $PDOexception->getMessage(). "</p>");
    }
}

然后初始化变量:

$host = 'localhost';
$user = 'root';
$dataBaseName = 'databaseName';
$pass = '';

现在您可以通过以下方式访问您的数据库

$db = connectToDatabase($host , $databaseName, $user, $pass); // You can make it be a global variable if you want to access it from somewhere else.

然后你应该确保你实际上有这个变量:

$search = isset($_GET['search']) ? $_GET['search'] : false;

因此,如果某些事情以某种方式失败,您实际上可以跳过数据库。

if(!$search)
{
    //.. return some warning error.
}
else
{
    // Do what follows.
}

现在您应该构造一个可以用作准备好的查询的查询,也就是说,它接受准备好的语句,以便您准备查询,然后执行一个变量数组,这些变量将被放入查询中,并且将避免 sql同时注入:

$query = "SELECT * FROM Product WHERE ProductID LIKE :search;"; // Construct the query, making it accept a prepared variable search.
$statement = $db->prepare($query); // Prepare the query.
$statement->execute(array(':search' => $search)); // Here you insert the variable, by executing it 'into' the prepared query.
$statement->setFetchMode(PDO::FETCH_ASSOC); // Set the fetch mode.

while ($row = $statement->fetch())
{
    $productId = $row['ProductID'];
    echo "<li class='name><strong>$productId</strong></li>";
}

哦,是的,不要使用 b 标签,它已经过时了。改用 strong (在单独的 css 文件中将 font-weight: bold; 应用于 .name 会更聪明。

如果有任何不清楚的地方,请随时提出问题。

于 2013-03-11T23:48:42.803 回答
0

移除 $search 前后的 {}。

应该:

$query = "SELECT * FROM Product WHERE ProductID LIKE '%$search%'";
于 2013-03-11T22:49:08.117 回答