2014-12-29 41 views
-1

查詢中的行可以是一個或多個(最多100行),如果查詢輸出只有一行數據,但是當輸出有許多行數據,只有最後選定的行纔會更新,我錯過了什麼?爲什麼我的查詢只更新一個字段

一些背景:員工需要從一個位置移動到另一個位置,插入查詢將數據寫入數據庫,但將RECEIPT日期留空。許多員工可以從一個地方被釋放到另一個地方......這發生在一個不同的表格上,並且完美地工作。

現在,收件人必須接受這些員工,更新查詢在提交表單之後在後臺運行,並且只需在$ move_receipt_date字段中輸入今日日期($ d)即可。但正如我前面提到的,只有最後一行是使我的更新查詢...

這裏是選擇查詢獲取數據和更新查詢,相當直截了當:

<?php 
    include("../../xxx.xxx"); 
     $cxn = mysqli_connect($host,$user,$password,$dbname) 
      or die ("Couldn't connect to server."); 
     $query = "SELECT `move_id`,`empl_no`,`empl_jc_code` AS old_jc_code,`new_jc_code`,`move_date`,`move_receipt_date`,`move_reason` 
      FROM `empl_movement` 
      WHERE `new_jc_code` = '$empl_jc_code' AND `move_receipt_date` = 0"; 
     $result = mysqli_query($cxn,$query) 
      or die ("Couldn't execute query. " 
       .mysqli_error($cxn)); 

       echo "<table><br> 
     <tr> 
     <th>Move ID</th> 
     <th>Employee No</th> 
     <th>Old JC Code</th> 
     <th>New JC Code</th> 
     <th>Release Date</th> 
     <th>Receipt Date</th> 
     <th>Reason for Move</th> 
     </tr>"; 
     while($row = mysqli_fetch_assoc($result)) 
     { 
      extract($row); 
      echo "<tr>\n 
       <td>$move_id</td>\n 
       <td>$empl_idno</td>\n 
       <td>$old_jc_code</td>\n 
       <td>$new_jc_code</td>\n 
       <td>$move_date</td>\n 
       <td>$move_receipt_date</td>\n //this is the field that will be updated 
       <td>$move_reason</td>\n 
       </tr>\n"; 
     } 
     echo "</table><br>";  ?> 

    <?php 
    include("../../xxx.xxx"); 
     $cxn = mysqli_connect($host,$user,$password,$dbname) 
      or die ("Couldn't connect to server."); 
     $query = "UPDATE `empl_movement` SET `move_receipt_date` = '$d' 
        WHERE `move_id` = '$move_id'"; 
     $result = mysqli_query($cxn,$query) 
      or die ("Couldn't execute query. " 
       .mysqli_error($cxn)); 
    ?> 
+1

該UPDATE查詢有一個WHERE子句,它看起來像是在標識符上過濾。如果只有一個匹配記錄,那麼它只會更新一條記錄。多少條記錄具有相同的'move_id'? – David

+0

據我所知,我的qyery只選擇了move_id的最後一個,但我的選擇查詢可以彈出1到100行,每一行都有不同的(唯一的)move_id。 –

+0

我知道我的問題在這裏,但我不知道如何解決這個問題,這是我第一次必須在查詢中使用select查詢進行多個更新 –

回答

0

因爲在你的查詢中,你只更新一個字段,例如empl_movement。

相關問題