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

$host = "xxx";
$user = "xxx";
$pass = "xxx";
$db = "xxx";

mysql_connect($host, $user, $pass);
mysql_select_db($db);
unset($host,$user,$pass,$db);


//$query = "SELECT produkt FROM checklist WHERE id IS "1";
$query = "SELECT produkt FROM checklist WHERE id LIKE '%$search%'";
$result = mysql_query($query);
  while ($row = mysql_fetch_array($result))
{

echo "<li><span class='name'><b>$row[produkt]</b><br /></span><span class='comment'><i>Tierische Stoffe: </i>$row[tierisch]</span><i> Alkohol: </i>$row[alkohol]</span></span></li>";
  }
?>


我的搜索框似乎出了点问题。目前,我只查看数据库中的所有项目,也想查看我要搜索的项目。

这样吗?

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

$host = "xxx";
$user = "xxx";
$pass = "xxx";
$db = "xxx";

mysql_connect($host, $user, $pass);
mysql_select_db($db);
unset($host,$user,$pass,$db);

$search = $_POST['search'];

$query = "SELECT produkt FROM checklist WHERE id LIKE '%".$_POST['search']."%'";
//$query = "SELECT produkt FROM checklist WHERE id LIKE '%$search%'";
$result = mysql_query($query);

最佳答案

<input id="$search" placeholder


您需要这里的$吗?它应该变成:

<input id="search" placeholder


要从搜索查询内部访问GET变量,请使用$ _GET数组,如下所示:

$query = "SELECT produkt FROM checklist WHERE id LIKE '%{$_GET['search']}%'";


但是为了安全起见,避免mysql注入,您可能想要这样做:

$search = mysql_real_escape_string($_GET['search']);
$query = "SELECT produkt FROM checklist WHERE id LIKE '%{$search}%'";


编辑:在输入中忘记名称属性:

<input name="search" id="search" placeholder="Search for a Product" type="text" />


然后在你的PHP

$search = mysql_real_escape_string($_GET['search']);
$query = "SELECT produkt FROM checklist WHERE id LIKE '%{$search}%'";

$result = mysql_query($query);
while ($row = mysql_fetch_array($result)){
    echo "<li><span class='name'><b>{$row['produkt']}</b></span></li>";
}

08-08 08:16
查看更多