<?php
$button=$_GET['submit'];
$search=$_GET['search'];
if(!$search)
echo "you did not enter a HAWB.";
else
{
if(strlen($search)<=2)
echo "Search term too short";
else
echo "You searched for <b> $search </b> <hr size='1'>";
mysql_connect("localhost","root","") or die ("cannot connect");
mysql_select_db("trip");
$search_exploded=explode("",$search);
$x=0;
$construct="";
foreach($search_exploded as $search_each)
{
$x++;
if ($x==1)
$construct="keywords LIKE '%search_each%'";
else
$construct="OR keywords LIKE '%search_each%'";
}
$construct="Select * from trip where $construct";
$run=mysql_query($construct);
$foundnum=mysql_num_rows($run);
if($foundnum==0)
` echo "No results found";
else
{
echo "$foundnum results found.<p>";
while ($runrows=mysql_fetch_assoc($run);
{
echo "<table border='1'>
<tr>
<th>TYPE</th>
<th>HAWB</th>
<th>DATE</th>
<th>TIME</th>
<th>PLATE NO.</th>
<th>NO. OF PIECES</th>
<th>CARGO MARSHAL</th>
<th>BY</th>
<th>REMARKS 1</th>
<th>REMARKS 2</th>
</tr>";
echo "<tr>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['HAWB'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Plate_no'] . "</td>";
echo "<td>" . $row['Pcs'] . "</td>";
echo "<td>" . $row['Cargo_Marshal'] . "</td>";
echo "<td>" . $row['By'] . "</td>";
echo "<td>" . $row['Remarks1'] . "</td>";
echo "<td>" . $row['Remarks2'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
}
?> //code formatted
问问题
77 次
2 回答
1
foreach($search_exploded as $search_each)
{
$x++;
if ($x==1)
$construct="keywords LIKE '%search_each%'";
else
$construct="OR keywords LIKE '%search_each%'";
}
你忘了$
签到search_each
foreach($search_exploded as $search_each)
{
$x++;
if ($x==1)
$construct="keywords LIKE '%$search_each%'"; //you might have to escape the statement
else
$construct="OR keywords LIKE '%$search_each%'";
}
另外,请记住关闭标签的横线。<hr />
不是<hr>
于 2013-09-02T01:09:23.200 回答
1
如果这是您正在编写的新脚本,我强烈建议您迁移到 MySQLi 而不是 MySQL,因为它现在已被弃用!
您的代码非常草率!花更多时间编写代码并使用体面的 IDE!
让我们从这里开始
$construct="";
foreach($search_exploded as $search_each)
{
$x++;
if ($x==1) {
$construct="keywords LIKE '%search_each%'";
}
else {
$construct="OR keywords LIKE '%search_each%'";
}
}
$construct="SELECT * FROM trip WHERE $construct";
为什么将 $construct 定义为 null 然后立即执行 if/else?您还以SELECT * FROM trip WHERE OR keywords LIKE..
个人身份在 else 中读取查询我不知道您要在那里完成什么
%search_each%
应该
%$search_each%
个人而言,if/else 变量不应该是类似的$construct
东西$condition
在第 33 行,你有一个流浪 `
while ($runrows=mysql_fetch_assoc($run);
{
不该有;
你在一段时间内打开你的桌子,然后在外面关闭它应该是
{
echo "$foundnum results found.<p>";
echo "<table border='1'>
<tr>
<th>TYPE</th>
<th>HAWB</th>
<th>DATE</th>
<th>TIME</th>
<th>PLATE NO.</th>
<th>NO. OF PIECES</th>
<th>CARGO MARSHAL</th>
<th>BY</th>
<th>REMARKS 1</th>
<th>REMARKS 2</th>
</tr>";
while ($runrows=mysql_fetch_assoc($run)
{
echo "<tr>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['HAWB'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Plate_no'] . "</td>";
echo "<td>" . $row['Pcs'] . "</td>";
echo "<td>" . $row['Cargo_Marshal'] . "</td>";
echo "<td>" . $row['By'] . "</td>";
echo "<td>" . $row['Remarks1'] . "</td>";
echo "<td>" . $row['Remarks2'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
你还有很多缺失的{ }
牙套!
我最初打算用 mysqli 重写,但我认为这是你需要自己学习的东西。
编辑!!!:你应该清理(去除 html/tags)你从用户输入的表单中得到的任何东西!
于 2013-09-02T01:17:26.733 回答