如何防止mysql查询中的$ result以错误的方式循环

时间:2018-05-06 04:48:01

标签: php mysql

我的以下代码中存在问题。我希望在文本框和表格内查看与下拉列表中所选id相关联的所有数据。

当我运行我的代码时,查询结果创建了一个重复的结果,因为数据库中的数据超过1行。

我在这里缺少什么? Inmake在数据库规范化方面有错吗?有人可以帮帮我吗?

下面是我在search.php中的代码

<?php
//including the database connection file
include_once("dbconnect.php");

$query1 = "SELECT * FROM kursus";
$result1 = mysqli_query($mysqli, $query1);
$result2 = mysqli_query($mysqli, "SELECT * FROM kursus");

?>

<html>
<head>

<script>
function showUser(str) {
  if (str=="") {
   document.getElementById("txtHint").innerHTML="";
   return;
  }
  if (window.XMLHttpRequest) {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  } else { // code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
    xmlhttp.onreadystatechange=function() {
     if (xmlhttp.readyState==4 && xmlhttp.status==200) {
       document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
     }
  }
   xmlhttp.open("GET","search2.php?q="+str,true);
   xmlhttp.send();
}
</script>

</head>
<body>

<form>
    Carian :  <select id="id_kursus" name="id_kursus" onchange="showUser(this.value)">
                <option>Sila Pilih</option>
                  <?php while($row1 = mysqli_fetch_array($result1)){
                    echo "<option value='".$row1['id_kursus']."'>".$row1['nama_kursus']."</option>";
                  }                      
                  ?>
              </select>



  </form>
    <br>
  <div id="txtHint"><b>Result will be display here.</div>

  </body>
  </html>

下面是我在search2.php中的代码

 <html>
 <head>

 <?php

 $q = intval($_GET['q']);

 $con = mysqli_connect('localhost','root','admin','tracer');
 if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
 }

 mysqli_select_db($con,"ajax_demo");

 $sql = "SELECT 
            a.*,
            b.*,
            c.*,         
            a.id_kursus AS id_kursus

          FROM pelatih a
          LEFT JOIN kursus b ON b.id_kursus = a.id_kursus
          RIGHT JOIN  status_pelatih c ON c.id_status_semasa = a.id_status_semasa

          WHERE a.id_kursus = '.$q.'";



 $result = mysqli_query($con,$sql);

 $no = 1;

 while($row = mysqli_fetch_array($result)) {  

  echo "<table class='table1'>
    <tr>
      <td>Kod Kursus : </td>
      <td><input type='text' value= '$row[kod_kursus]'></td>
    </tr>
    <tr>
      <td>Nama Kursus : </td>
      <td><input type='text' size='40' value= '$row[nama_kursus]'></td>          
    </tr>
    <tr>
      <td>Sesi : </td>
      <td><input type='text' value= '$row[sesi_kursus]''></td>
    </tr>
    <tr>
      <td>Batch : </td>
      <td><input type='text' value= '$row[batch_kursus]'></td>
    </tr>                  
    <tr>
      <td>Tempoh Kursus :</td>
      <td><input type='date' value= '$row[tarikh_mula_kursus]'> Hingga <input type='date' value= '$row[tarikh_tamat_kursus]'></td>
    </tr>
    <tr>
      <td>Program Tajaan : </td>
      <td><input type='text' value= '$row[penaja_program]'></td>
    </tr>
    <tr>
      <td>Peruntukan : </td>
      <td><input type='text' value= '$row[peruntukan]'></td>
    </tr>
    <tr>
      <td>Tarikh Kajiselidik : </td>
      <td ><input type='date' value= '$row[tarikh_kajiselidik]'></td>
    </tr>
    <tr>
      <td>Cara Kajiselidik : </td>
      <td ><input type='text' value= '$row[cara_kajiselidik]'></td>
    </tr>
 </table>

  <br>";

  echo "<table class='table2'>
  <tr>
    <th rowspan='2'>Bil</th>
    <th colspan='4'>Nama dan Butiran Pelatih</th>
    <th colspan='6'>Maklumat Tracer Study</th>       
  </tr>
  <tr>        
    <th>Nama Pelatih</th>
    <th>No. K/P</th>
    <th>No. Telefon</th>
    <th>Status Semasa</th>
    <th>Nama Syarikat</th>
    <th>Alamat Syarikat</th>
    <th>No. Telefon</th>
    <th>Nama Jawatan</th>
    <th>Tangga Gaji</th>
    <th>Catatan</th>
 </tr>";

                 echo '<tr>
                          <td>'.$no.'</td>
                          <td>'.$row['nama_pelatih'].'</td>
                          <td>'.$row['ic_pelatih'].'</td>
                          <td>'.$row['tel_pelatih'].'</td>
                          <td>'.$row['status_semasa_pelatih'].'</td>
                          <td>'.$row['nama_syarikat'].'</td>
                          <td>'.$row['alamat_syarikat'].'</td>
                          <td>'.$row['tel_syarikat']. '</td>
                          <td>'.$row['jawatan'].'</td>
                          <td> RM'.$row['tangga_gaji'].'</td>
                          <td>'.$row['catatan'].'</td>
                      </tr>';
                $no++;
      }
      echo "</table>";

      mysqli_close($con);

      ?>

      </body>
      </html>

0 个答案:

没有答案