2017-08-30 8 views
1

テーブルを更新するためにselectオプションを使いたいと思います。 これはコードですが、試してみるとデータは表示されていません。ヘルプをしてください....phpとmysqlのselectオプションを使用してテーブルを更新する方法

これらのコードは選択オプションです。

<form method="POST"> 
 
      <text class="sort"><span>Course:</span></text> 
 
      
 
      <select class="sel" name="select_course" id="select_course"> 
 
       <?php 
 
       $query=mysqli_query($conn,"select DISTINCT Course from `tbl_student`"); 
 
\t \t   while($row=mysqli_fetch_array($query)){ 
 
\t \t \t  ?> 
 
       <option><?php echo $row['Course'];?></option> 
 
       <?php 
 
        } 
 
       ?> 
 
      </select> 
 
      <input class="btnsearch" type="button" name="submit" value="Search"> 
 
      <input class="btn" type="button" name="btn_add" value="Add Student"> 
 
      <input class="btn_import" type="button" name="import" value="Import File"> 
 
      

そして、これらのコードは、テーブルにデータを表示するためのものです。

<?php 
 
          echo '<table> 
 
         <tr> 
 
         <td>name</td> 
 
         <td>Id number</td> 
 
         <td>Course</td> 
 
         <td>School Year</td> 
 
         <td>Semester</td> 
 
         </tr>'; 
 
        if(isset($_POST['submit'])) 
 
        { 
 
         $result2 = mysqli_query($conn, "Select name,id_number,Course,school_year,semester from tbl_student where Course='".$_POST['select_course']."'"); 
 
          
 
         while($row=mysqli_fetch_row($result2)){ 
 
          echo '<tr> 
 
         <td>'.$row["name"].'</td> 
 
         <td>'.$row["id_number"].'</td> 
 
         <td>'.$row["Course"].'</td> 
 
         <td>'.$row["school_year"].'</td> 
 
         <td>'.$row["semester"].'</td> 
 
         </tr>'; 
 
         } 
 
          echo '</table>'; 
 
        } 
 
        ?> 
 
       
 
      </form>

私はテーブルが自動的に更新されます選択オプションから選択したときに私が欲しいのです。

+0

ajaxとonchangeイベントがなければ、選択した変更に基づいて実行することはできません。そのためのフォームをPHPに送信する必要があります – clearshot66

+0

あなたのコードは[** SQLインジェクション**](https://en.wikipedia.org/wiki/SQL_injection)攻撃に対して脆弱です。あなたは[** mysqli **](https://secure.php.net/manual/en/mysqli.prepare.php)または[** PDO **](https ://secure.php.net/manual/en/pdo.prepared-statements.php)ドライバ。 [**この記事**](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)には、いくつかの良い例があります。 –

答えて

0

これを行うための最善の方法は、ajaxですが、あなたのようなHTMLとPHPでそれを行うことができますのでjsは、ここでタグ付けされていません。

<form action="updateData.php"> 
    <select name="select_course" onchange="this.form.submit()"> 

onchange="this.form.submit()"選択がupdateData.phpに変化したとき、フォームを提出する、置きますここの表のデータを更新するコード。

0
while($row=mysqli_fetch_row($result2)){ 

ここにコードに誤りがあります。 mysqli_fetch_rowを使用していて、打撃ラインでは$ row ["name"]を使用しています。 mysqli_fetch_assocを使って、$ row ["name"]を使ってください。 mysqli_fetch_rowを使用する場合、データベースのインデックスを置く必要があります。

関連する問題