搜索框查询名称mysql / php

时间:2012-12-17 21:37:03

标签: php mysql database

还在mysql和php的学习过程中。我有一个带有1个表的mysql数据库。我只想要一个用户可以输入公司名称(例如Best Buy)的搜索框,然后它将输出在Best Buy购买的产品列表以及数据库中的所有内容。编辑:错误接收是“查询失败:您的SQL语法中有错误;检查与您的MySQL服务器版本对应的手册,以便在第1行的'资源ID#3'附近使用正确的语法 资源ID#3“

<html>
    <head>
        <title>Search the Database</title>
    </head>
    <body>
    <form action="index.php" method="post">
     Search: <input type="text" name="vendor" /><br />
    <input type="submit" name="submit" value="Submit" />
    </form>
    </body>
</html>

<?php
$mysql_host = 'localhost';  
$mysql_user = 'user_name';  
$mysql_pass = '12345';  
$Name = "user_db";
$Table = "table1";
mysql_connect ($mysql_host, $mysql_user, $mysql_pass, $Name) or die ('Error connecting to mysql');
mysql_select_db("$Name") or die ("unable to select DB");

      echo $_POST['vendor'];
      $vendor2 = $_POST['vendor'];
      $sqlquery = mysql_query("Select * From $Table WHERE `purchases`.`vendorname` LIKE '%$vendor2%';");
      $result = mysql_query($sqlquery) or die('Query failed: ' . mysql_error() . "<br />\n$sqlquery");  ;
      $number = mysql_num_rows($result);
?>

<table cellspacing=0 cellpadding=4 border=1>
<tr>
<th>Vendor</th>
<th>Product</th>
<th>DateOrdered</th>
<th>Cost</th>
</tr>


<?php
for($counter = 0; $counter < mysql_num_rows($result); $counter++) {
?>

<tr>
<td><?php echo mysql_result($result,$counter,"vendorname")?></td>
<td><?php echo mysql_result($result,$counter,"product")?></td>
<td><?php echo mysql_result($result,$counter,"date")?> </td>
<td><?php echo mysql_result($result,$counter,"price1")?> </td>
</tr>

<?php
}
?>

</table>
<?php
?>

0 个答案:

没有答案