【问题标题】:i get parse error with this lines of codes [closed]我得到这行代码的解析错误[关闭]
【发布时间】:2013-09-05 01:05:08
【问题描述】:
<?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

【问题讨论】:

  • 在您的问题正文中添加一些文本,解释代码并发布实际的错误消息将使您的问题得到更高的评价。就目前而言,这是一个结构不佳的问题。
  • 那不可读。使用代码标签
  • 使用一个体面的 IDE 和语法高亮,让这个词变得更好。
  • PSA: mysql_* 函数是 deprecated in PHP 5.5。不建议编写新代码,因为它会阻止您将来升级。相反,请使用 MySQLiPDObe a better PHP Developer

标签: php mysql parse-error


【解决方案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%'";
}

另外,请记住关闭标签的横线。 &lt;hr /&gt; 不是&lt;hr&gt;

【讨论】:

    【解决方案2】:

    如果这是您正在编写的新脚本,我强烈建议您迁移到 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? 您还将查询在您的 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/标签)您从用户输入的表单中获得的任何内容!

    【讨论】:

      猜你喜欢
      • 2016-01-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多