2017-05-01 9 views
-3

私はPHPで検索フォームとダイプレイテーブルを作成したいと思います。HTMLフォーム内で検索して同じPHPページの表内に表示するにはどうすればいいですか?

私はHTML形式で検索し、phpMyAdminのデータからテーブルを表示するPHPコードを作成しました。

しかし、私の検索フォームは動作していますが、PHPの表示テーブルにエラーが表示されます。

<html> 
<div class="container-fluid"> 
    <form class="form col-md-8" id="form_Show" role="form" form action="Show.php" method="POST"> 
     <legend>Show Customers table</legend> 
     <h5>Put any characters in Customer's informaiton to search the data.</h5> 
     <div class="form-group"> 
      <label for="Cstm_name" class="col-sm-2">Customer Information</label> 
      <div class="col-sm-4"> 
       <input type="text" class="form-control" name="show" id="show" placeholder="e.g) A, L, J, 1 or 8"> 
      </div> 
      <div class="row"></div> 
      <div class="form-group"> 
       <div class="col-sm-offset-2 col-sm-10"> 
        <button type="submit" class="btn btn-primary">Search</button> 
       </div> 
      </div> 
     </div> 
     <div class="col-md-offset-2 col-md-10"> 
      <table class="table table-condensed" id="s_result"> 
       <thread class="s_result"> 
        <tr class="info"> 
         <th>Customer ID</th> 
         <th>Customer Name</th> 
         <th>Customer address</th> 
         <th>Customer Cellphone</th> 
        </tr> 
       </thread> 

       <?php 
       $host = ""; 
       $user = ""; 
       $password = ""; 
       $database = ""; 
       foreach ($_POST as $key => $value) {${$key}=$value;}; 
       mysql_connect($host, $user, $password) or die ("error"); 
       mysql_select_db($database) or die ("db error"); 

       if($_POST ['show']!='') 
       { 
        $search=$_POST['show']; 
        $search = preg_replace("#[^0-9a-z]#i","",$search); 
        $qry = "SELECT * FROM Customers 
        WHERE Cstm_id LIKE '%$search%' 
        OR Cstm_name LIKE '%$search%' 
        OR Cstm_addrs LIKE '%$search%' 
        OR CCell_no LIKE '%$search%';"; 

        $rst = mysql_query($qry);} 
        echo "<h4>Search results:<span> </span>$search</h4>"; 
        while ($row1 = mysql_fetch_row($rst)) 
        { 
         echo "<tbody>"; 
         echo "<th>"$row\['Cstm_id'\]"</th>"; 
         echo "<th>"$row\['Cstm_name'\]"</th>"; 
         echo "<th>"$row\['Cstm_addrs'\]"</th>"; 
         echo "<th>"$row\['CCell_no'\]"</th>"; 
         echo "</tbody>"; 
        } 
        ?> 
       </table> 
      </form> 
     </div> 
    </div> 
    </html> 
+0

あなたはどのようなエラーが出るのですか? –

+0

FYI、[新しいコードでは 'mysql_ *'関数を使うべきではありません](http://stackoverflow.com/questions/12859942/)。それらはもはや維持されておらず(正式に推奨されていません)(https://wiki.php.net/rfc/mysql_deprecation)。 [red box](http://php.net/manual/en/function.mysql-connect.php)を参照してください。代わりに[* prepared statements *](https://en.wikipedia.org/wiki/Prepared_statement)について学び、[PDO](http://php.net/pdo)または[MySQLi](http:// php.net/mysqli) - [この記事](http://php.net/manual/en/mysqlinfo.api.choosing.php)は、あなたに最適なものを決定するのに役立ちます。 –

+0

あなたのスクリプトは[SQL Injection Attack]の危険にさらされています(http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)[Little Bobby Tables](http://bobby-tables.com/)[あなたが入力を逃れている場合でも、その安全ではありません!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around -mysql-real-escape-string)[prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)を使用します。 –

答えて

-2

てみif(isset($_POST['show'])){//do stuff...}

+0

コードダンプは良い答えにはなりません。どうやって彼らの問題を解決するのか説明してください。 「[良い答えを書くにはどうすればいいですか?]](http://stackoverflow.com/help/how-to-answer) –

+2

Do not do。試着はありません。 〜マスターヨーダ –

+0

ありがとう!私はこのコードでヒントを得ました! – JiHo

関連する問題