2016-09-26 64 views
3

我有一個表格輸入:笨插入表用條件

<div class="row" id="form_pesan"> 
 
       
 
        <input type="hidden" name="id" id="id" /> 
 

 
        <div class="col-sm-6"> 
 
        <input type="text" class="form-control input-lg typeahead typeahead_user_email" autocomplete="off" id="user_email" name="user_email" placeholder="Email" required> 
 
        </div> 
 

 
        <div class="col-sm-6"> 
 
        <input type="text" class="form-control input-lg" id="phone" name="phone" placeholder="Phone" required> 
 
        </div> 
 

 
        <div class="col-sm-6"> 
 
        <input type="text" class="form-control input-lg" id="plate_number" name="plate_number" placeholder="Plate Number" required> 
 
        </div> 
 

 
        <div class="col-sm-6"> 
 
         <select class="form-control" id="location_name" name="location_name"> 
 
         <?php foreach ($vendor as $v) { 
 
          ?> 
 
          <option id="<?php echo $v['location_id']; ?>" value="<?php echo $v['location_id']?>" ><?php echo $v['name'] ;?></option> 
 
         <?php }; ?> 
 
         </select> 
 
        </div> 
 
       
 
       </div> 
 
<button id="add" type="button" class="btn btn-primary btn-md" onclick=add()>Add</button>

Javascript函數的add():

function add(){ 
 
    var formIsValid = true; 
 
     $(matchFormFields).each(function() { 
 
     $(this).css('border-color', ''); 
 
     if(!$.trim($(this).val())) { 
 
      $(this).css('border-color', errorColor); 
 
      formIsValid = false; 
 
     } 
 
    }); 
 
    if (formIsValid) { 
 
    user_email = $("#user_email").val(); 
 
    phone = $("#phone").val(); 
 
    plate_number = $("#plate_number").val(); 
 
    location_id = $('#location_name option:selected').attr('id'); 
 

 
    $.ajax 
 
     ({ 
 
      url : "<?php echo site_url('admin/add_driver')?>/", 
 
      type: "POST", 
 
      dataType: "json", 
 
      data:{user_email: user_email, plate_number: plate_number, location_id: location_id}, 
 
      success: function(data) 
 
      {  
 
       $("#add_driver_modal").modal("hide"); 
 
       $("#alert").show(); 
 
       console.log(user_email); 
 
       console.log(phone); 
 
       console.log(plate_number); 
 
       console.log(location_name); 
 
       //location.reload(); 
 
      }, 
 
      error: function (jqXHR, textStatus, errorThrown) 
 
      { 
 
       alert('Error data'); 
 
      } 
 

 
     }); 
 
    }; 
 
}

控制器:

public function add_driver() 
{ 
    if(!$this->user_permission->check_permission())return; 

     $phone      = $this->input->post('phone'); 
     $user_email     = $this->input->post('user_email'); 
     $user_password    = "tiki1234"; 
     $plate_number    = $this->input->post('plate_number'); 
     $location_id    = $this->input->post('location_id'); 

     $this->db->select("*"); 
     $this->db->from('user'); 
     $query = $this->db->get(); 
     $data['user'] = $query->result_array(); 
     foreach ($data['user'] as $u) { 
      if($user_email == $u['user_email']){ 
       $data_user_driver = array(
        'user_id'     => $u['user_id'], 
        'plate_number'    => $plate_number, 
        'location_id'    => $location_id, 
        'current_lat'    => 0, 
        'current_lon'    => 0 
       ); 
       $this->db->insert('user_driver', $data_user_driver); 
      } 
      else{ 
       $this->load->library('login_library'); 
       $this->load->library('form_validation'); 

       $this->form_validation->set_message('is_unique', 'Email is already in use'); 
       $this->form_validation->set_rules('user_email', 'Email', 'trim|required|valid_email|is_unique[user.user_email]|max_length[255]'); 
       $this->form_validation->set_rules('user_password', 'Password', 'trim|required|min_length[6]|max_length[32]|callback__password_valid'); 
       $this->form_validation->run(); 
       $this->login_library->register($user_email, $user_password); 

       if ($this->form_validation->run() && $data = $this->login_library->register($user_email, $user_password)) { 
        if (isset($data['success']) && $data['success'] == TRUE) { 
         echo json_encode((array('success' => 1, "msg" => $data['message']))); 
        } else 
         echo json_encode(array('success' => 0, "msg" => $data["message"])); 
       } else 
        echo json_encode(array('success' => 0, "msg" => validation_errors())); 
       } 
      }    
} 

釷條件是,如果input->post->('user_email')user表存在,則插入user表第一然後user_driver表,如果它存在,則插入到只user_driver

值被插入到數據庫中,但它拋出一個錯誤duplicate entry ****@gmail.com for key user_email_unique

+0

「register」操作有什麼用? –

+0

我正在使用'register'庫來添加驅動程序作爲新用戶。 @RameshKumar – may

+1

如果條件,然後嘗試之前刪除行。 '$ this-> login_library-> register($ user_email,$ user_password);'希望它能爲你工作。還有一件事。您可以在循環外加載其他影響加載時間的庫。 –

回答

0

[解決]我錯過了SQL中的where語法,我可以用它來檢查變量是否匹配表中的值:)

0

您可以通過電子郵件ID從表中首先選擇數據如下做到這一點:

$user_array = $this->db->where('user_email',$this->input->post('user_email')->get('users')->row_array(); 
if(count($user_array)>0){ 
$this->db->update('users',$update_array,array('user_email',$this->input->post('user_email'))); //update driver by email 
}else{ 
$this->db->insert('users',$data_user_driver); //insert the data 
} 
+0

萬一你可能會誤解我的問題,如果input-> post - >('user_email')'與'user'表中的值不匹配,我想插入新的id,email等等到'user'表中,**在插入到**'user'後,我希望它也插入'user_driver',但是如果在user表中存在'input-> post - >('user_email)',那麼它會* *只能插入**'user_driver'表中。據我瞭解你的答案,爲什麼我需要'this-> db-> update('users')'?請幫我理解它:) – may

+0

好的,你可以插入新的記錄,並使用更新查詢全局。 –