2013-09-26 39 views
0
提前

感謝我在做什麼是我創建了一個表中的MySQL與記錄的細節(ID,姓名,日期)和另一個表個人信息插入表值像(ID,姓名,地址,批號,出生日期,性別,血型等) 我想要做的是,我想的形式來從個人詳細一些細節,如名批次表現在我想要另一個字段與複選框它應該在每一行 現在選擇幾個複選框和提交按鈕它應該能夠在不選擇以從記錄表個人細節表和複選框的作爲1上選擇的值或0插入取出的數據。我已經嘗試了代碼,但無法找出解決方案。無法使用循環

這是我的完整代碼的一部分

require_once('connect.php'); // contains my connection 


    $select_query="SELECT * FROM personal WHERE batch='161'"; 

     $result=mysql_query($select_query); 

if(!$result) 
{ 
$error="there was some error try again later"; 
} 
?> 
<div class="module-table-body" > 

        <form method="post"> 
        <table id="myTable" class="tablesorter" align="center"> 
         <thead> 
          <tr> 
           <th style="">#</th> 
           <th style="">Full name</th> 
           <th style="">Batch</th> 
           <th style=""> Present/Absent</th> 

          </tr> 
         </thead> 
        <tbody> 
         <?php 
       while($row=mysql_fetch_array($result)) 
       { 

      ?> 
      <tr> 
       <td ><?php echo $row['id']; ?></td> 
       <td ><?php echo $row['full_name']; ?></td> 
       <td><?php echo $row['batch']; ?></td> 
      <td> 
     <input name="checkbox[]" type="checkbox" value="<?php echo $row['id']; ?>">  
        </td> 
        </tr> 
     <?php 

} 

       ?> 
       <tr> 
       <td> 
      <input name="save" type="submit" value="Save" /></td></tr> 

      </tbody> 
      </table> 

      <?php 

      if(isset($_POST['save'])) 
      { 
      $box=$_POST['checkbox']; 
      foreach($box as $id) 
       { 

     $box= isset($_POST['checkbox']) && $_POST['checkbox'] ? "1" : "0" ; 


      $insert="INSERT INTO record VALUES('','$fname','$date','$box')"; 

      $res=mysql_query($insert); 
      if(!$res) 
      { 
     echo mysql_error(); 
     } 
     else 
    { 
     echo "updated successfully"; 
    } 

    } 
     } 


    ?> 

代碼工作,只有插入,只有零複選框值

回答

1

你被寫代碼

$box= isset($_POST['checkbox']) && $_POST['checkbox'] ? "1" : "0" ; 
覆蓋 $box的最後一個條目

在循環中,通過使用另一個變量編輯....

+0

如何請詳細說明 –

+0

循環中的$ box變量覆蓋:foreach($ box as $ id)。每次循環執行$ box值都會因$ box = isset()而改變..... – Thomas