2016-12-04 58 views
1

我有一個tbl_employee表和tbl_time表,我想插入多個數據插入考勤,但是當我點擊提交按鈕它只插入單個數據。但問題出在哪裏,幫我找出this..this是插入代碼爲什麼多個數據不插入此代碼

require './db_connect.php'; 
class Time extends Db_connect { 

    protected $link; 

    public function __construct() { 
     $this->link = $this->database_connection(); 
    } 

    public function attendance_insert($data) { 
     extract($data); 
     $cur_date = date('Y-m-d'); 

     foreach ($time_attendance as $attn_key => $attn_value) { 
      if ($attn_value == 'P') { 
       $SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance)VALUES('$attn_key','$cur_date','P')"; 
       $atten_date=mysqli_query($this->link, $SQL); 
      } else if ($attn_value == 'A') { 
       $SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance)VALUES('$attn_key','$cur_date','A')"; 
       $atten_date=mysqli_query($this->link, $SQL); 

      } 
      if ($atten_date) { 
       $massage = "<div class='alert alert-success text-center'><h5>Attendance insert successfully</h5></div>"; 
       return $massage; 
      } else { 
       die('Attendance insert query problem' . mysqli_error($this->link)); 
      } 
     } 
    } 
} 

這是HTML代碼

<?php 

    require_once './time.php'; 
    $obj_time = new Time() 
    $massage = ''; 
    if (isset($_POST['btn'])) { 
     $massage = $obj_time->attendance_insert($_POST); 
    } 

    $employee_view = $obj_employee->employee_all_view(); 
    ?> 
    <div class="container-fluid"> 
     <div class="row"> 
      <center> 
       <a href="time.php" class="btn btn-sm btn-default glyphicon glyphicon-backward pull-left" id="show_form"></a> 
       <span style="font-size:1.8em;">Attendance form</span> 
      </center> 
     </div> 
    </div> 
    <hr/> 


    <?php echo $massage; ?> 
    <div class="container-fluid"> 
     <div class="row"> 
      <div class="col-lg-12"> 

       <div class="panel panel-body panel-default"> 
        <div class="well text-center" style="font-size:15px;"> 
         <strong>Date :</strong> 
         <?php $current_date = date('Y-M-d'); 
         echo $current_date; ?> 
        </div> 
        <form class="form-horizontal" method="post"> 
         <table class="table table-striped table-responsive text-center"> 
          <tr> 
           <td><b>Serial</b></td> 
           <td><b>Name</b></td> 
           <td><b>ID</b></td> 
           <td><b>Attendance</b></td> 
          </tr> 
          <?php 
          $i = 0; 
          while ($employee_info = mysqli_fetch_assoc($employee_view)) { 
           extract($employee_info); 
           $i++; 
           ?> 
           <tr> 
            <td><?php echo $i; ?></td> 
            <td><?php echo $employee_first_name . ' ' . $employee_last_name; ?></td> 
            <td><?php echo $employee_id; ?></td> 
            <td> 
             <input type="radio" name="time_attendance[<?php echo $employee_id; ?>]" value="P">P &nbsp; 
             <input type="radio" name="time_attendance[<?php echo $employee_id; ?>]" value="A">A 
            </td> 
           </tr> 
          <?php } ?> 

          <tr> 
           <td colspan="4"> 
            <input type="submit" class="btn btn-primary btn-block" name="btn" value="submit"/> 
           </td> 
          </tr> 

         </table> 
        </form> 
       </div> 
      </div> 
     </div> 
    </div> 
+0

爲什麼只插入單排.........請給我解決方案............. –

+0

我的代碼有沒有問題........ ... –

+1

鏈接回來,所以meta https://laurent22.github.io/so-injections/ 你的代碼容易受到非常討厭的攻擊 – SoonDead

回答

-1

此:

foreach($time_attendance as $attn_key => $attn_value){ 
    $SQL = "INSERT INTO tbl_time(employee_id,time_date,time_attendance) 
     VALUES('$attn_key','$cur_date','A')"; 
    $atten_date = mysqli_query($this->link, $SQL); 

    if ($atten_date) { 
     $massage = "<div class=''><h5>Attendance insert successfully</h5></div>"; 
     return $massage; 
    } 
} 

字面上要求打破與return $massage; foreach,如果你查詢成功......所以是的,你只會有一個記錄。

把你的回報放在foreach之外。

+0

非常感謝您的回放BRO「Blag」........... –

+0

現在的工作流暢...... Blag –

+0

@ Md.ShapanHossain沒問題;如果你想要一些改進,請看看['INSERT INTO'在這裏是如何安全使用的](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php);併爲你的測試成功,測試另一種方式:如果有錯誤($ atten_date == FALSE)添加一條消息,如果沒有在foreach後的var按摩,然後顯示「everything ok」 – Blag