dream_life5200 2014-02-07 11:21
浏览 19
已采纳

将表单中的数据添加到查询中

I'm building a very basic html website for use on a smartphone, which would look something like this

<body>
Some Text
<form name="input" action="query.php" method="get">
Search: <input type="text" name="search">
<input type="submit" value="Submit">
</form> 
</body>

Then in the query.php I have this

select ProductName, PalletSpace, Quantity, TransactionDate from Table1
having Table1.PalletSpace='B005E';

How can I link the two up so that the text typed into the search box is inserted into the query (where it says B005E?)?

EDIT: query.php now contains this:

<?
$username="xxx";
$password="xxx";
$database="xxx";



$con = mysqli_connect("xxx", "xxx", "xxx", "xxx");

$result = mysqli_query($con, "SELECT ProductName, PalletSpace, Quantity, TransactionDate FROM ProductTrans
WHERE ProductName = ($_REQUEST["search"])");

echo "<table border='1'>
<tr>
<th>Product Code</th>
<th>Pallet Space</th>
<th>Quantity</th>
<th>Date</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ProductName'] . "</td>";
  echo "<td>" . $row['PalletSpace'] . "</td>";
  echo "<td>" . $row['Quantity'] . "</td>";
  echo "<td>" . $row['TransactionDate'] . "</td>";
  echo "</tr>";
  }
echo "</table>";
?>

query.php and the html file (which is still the same) are in the same directory. However, when I try a search, it gives me this error

Parse error: syntax error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/u921305435/public_html/query.php on line 11
  • 写回答

2条回答 默认 最新

  • dpk20361 2014-02-10 15:08
    关注

    Not sure if I should do this, but in case anyone is stuck on the same thing

    search.html ended like this

    <html>
    <body>
    Some Text
    <form name="input" action="query.php" method="GET">
    Search: <input type="text" name="search">
    <br /><input type="submit" value="Submit">
    </form> 
    </body>
    </html>
    

    query.php like this

    <?php
        mysql_connect("host", "username", "password") or die("Error connecting to database: ".mysql_error());
    
    mysql_select_db("database") or die(mysql_error());
    ?>
    
    <!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>
    <title>Search results</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="style.css"/>
    </head>
    <body>
    <?php
    $query = $_GET['search'];
    
    $min_length = 0;
    
    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then
    
        $query = htmlspecialchars($query); 
    
        $query = mysql_real_escape_string($query);
    
        $raw_results = mysql_query("SELECT * FROM ProductTrans
            WHERE (`ProductName` LIKE '%".$query."%') OR (`PalletSpace` LIKE '%".$query."%')") or die(mysql_error());
    
        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
    
            echo "<table border='1'>
        <tr>
        <th>Product Code</th>
        <th>Pallet Space</th>
        <th>Quantity</th>
        <th>Date</th>
        </tr>";
    
        while($row = mysql_fetch_array($raw_results))
        {
        echo "<tr>";
        echo "<td>" . $row['ProductName'] . "</td>";
        echo "<td>" . $row['PalletSpace'] . "</td>";
        echo "<td>" . $row['Quantity'] . "</td>";
        echo "<td>" . $row['TransactionDate'] . "</td>";
        echo "</tr>";
        }
        echo "</table>";
    
        }
        else{ // if there is no matching rows do following
            echo "No results";
        }
    
    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }
    ?>
    </body>
    </html>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳