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

2 に答える 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

これがあなたが書いている新しいスクリプトである場合は、MySQL ではなく MySQLi に移行することを強くお勧めします。

あなたのコードは非常にずさんです! コードを書くときにより多くの時間を割き、適切な 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%

$construct個人的には、if/else 変数は、次のようなものであってはなりません。$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/タグを削除) する必要があります。

于 2013-09-02T01:17:26.733 に答える