2

还在学习mysql和php的过程中。我有一个带有 1 个表的 mysql 数据库。我只想要一个搜索框,用户可以输入公司名称(例如百思买),然后它将输出在百思买购买的产品列表,其中包含价格以及数据库中的所有内容。编辑:我收到的错误是“查询失败:您的 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
?>
4

0 回答 0