我用这行代码得到解析错误

时间:2013-09-02 01:05:49

标签: php mysql parse-error

<?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

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分: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.. 我个人不知道你想要在那里完成什么

%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 /标签)你从用户输入的表格中得到的任何东西!