2016-11-17 65 views
-1

我希望插入員工考勤。當我檢查更多,然後一個複選框它需要兩個複選框值1和其他複選框值0。我怎麼能解決這個請幫助我。這是我的表單代碼當我選擇多個選中的複選框時,不會保留所有值

<form action="coll.php" method="post" name="create_grading" id="create_grading"> 
     <table width="30%" border="0" cellpadding="2" cellspacing="3" class="mainTable"> 
      <tr> 
       <th><input type="checkbox" id="selectall" /></th> 
       <th>name</th> 
      </tr> 
      <?php 
      $sql = "select * from employee"; 
      $query = mysqli_query($con, $sql); 
      while ($row = mysqli_fetch_array($query)) { 
       ?> 
       <tr> 
        <td><input type="hidden" name="eid[]" value="<?php echo $row['eid']; ?>"/> 
         <input name="status[]" class="case" type="checkbox" value="1" /><input name="status[]" class="case" type="hidden" value="0" /></td> 
        <td align="center"><?php echo $row['employee_name'] ?></td> 
       </tr> 
      <?php }; ?> 

      <tr> 
       <td></td> 
       <td><input type="submit" name="Submit" id="Submit" value="Submit" /></td>  
      </tr> 
     </table> 
    </form> 

這是我的插入代碼

$host = "localhost"; 
$user = "root"; 
$pass = ""; 
$db = "multiple_row_insert"; 
$con = mysqli_connect($host, $user, $pass, $db); 


if (isset($_POST['Submit'])) { 
    $eid = $_POST['eid']; 
    $count = count($eid); 
    for ($i = 0; $i < $count; $i++) { 
     $status = $_POST['status'][$i]; 
     $eid2 = $_POST['eid'][$i]; 
     $query = "INSERT INTO time(eid,status) VALUES ('$eid2','$status')"; 
     $query = mysqli_query($con, $query); 
    } 
} 
+0

你只需要知道1複選框是否被選中,這就是全部,在這種情況下你不需要兩個複選框。 – Asfo

+1

[Little Bobby](http://bobby-tables.com/)說*** [你的腳本存在SQL注入攻擊風險。](http://stackoverflow.com/questions/60174/how-can- ***)瞭解[MySQLi](http://php.net/manual)[準備](http://en.wikipedia.org/wiki/Prepared_statement)聲明/en/mysqli.quickstart.prepared-statements.php)。即使[轉義字符串](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string)是不安全的! [不相信嗎?](http://stackoverflow.com/q/38297105/1011527) –

回答

0
if(isset($_POST['checkbox'])) 
//then is checked... 
else 
//is not checked 

所以基本上你只需要1個複選框,你可以改變你的輸入名稱。然後在你的後端使用類似於:

$_POST['status_'.$i] 

在每種情況下,並檢測是否檢查其$ _POST的「isset」。

編輯:

你的代碼看起來應該是這樣的(未測試,對不起,如果有任何語法錯誤,你可以反正解決這些問題):

$host = "localhost"; 
$user = "root"; 
$pass = ""; 
$db = "multiple_row_insert"; 
$con = mysqli_connect($host, $user, $pass, $db); 


if (isset($_POST['Submit'])) { 
    $eid = $_POST['eid']; 
    $count = count($eid); 
    for ($i = 0; $i < $count; $i++) { 
     $status = 0; 
     if(isset($_POST['status_'.$i])) //check if checkbox is setted (value 1/checked) 
      $status = 1; 
     $eid2 = $_POST['eid'][$i]; 
     $query = "INSERT INTO time(eid,status) VALUES ('$eid2','$status')"; 
     $query = mysqli_query($con, $query); 
    } 
} 

而且你的前端代碼:

<form action="coll.php" method="post" name="create_grading" id="create_grading"> 
     <table width="30%" border="0" cellpadding="2" cellspacing="3" class="mainTable"> 
      <tr> 
       <th><input type="checkbox" id="selectall" /></th> 
       <th>name</th> 
      </tr> 
      <?php 
      $sql = "select * from employee"; 
      $query = mysqli_query($con, $sql); 
      $i = 0; 
      while ($row = mysqli_fetch_array($query)) { 
       ?> 
       <tr> 
        <td><input type="hidden" name="eid[]" value="<?php echo $row['eid']; ?>"/> 
         <input name="status_<?php echo $i; ?>" class="case" type="checkbox" value="0" /></td> 
        <td align="center"><?php echo $row['employee_name']; ?></td> 
       </tr> 
      <?php $i++; } ?> 

      <tr> 
       <td></td> 
       <td><input type="submit" name="Submit" id="Submit" value="Submit" /></td>  
      </tr> 
     </table> 
    </form>    
+0

我不能解決這個問題,所以如果給我完整的代碼.....它會幫助我很多 –

+0

請幫我導致我不能忍受什麼必須要解決這個問題 –

+0

只要用你的邏輯夥計,我不知道爲什麼AbraCadaver刪除了他的評論,但基本上在你的內部,$ status = $ _POST ['status 「] [$ i]於; < - 在這一行,只需將其更改爲if(isset($ _ POST ['status _'。$ i])){--- code ---}並在您的內部更改您的行 to 」class =「case」type =「checkbox」value =「0」/> ...終於別忘了包含$ i = 0;和$ i ++;爲你的時間。 – Asfo