我有一個表格輸入:笨插入表用條件
<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
「register」操作有什麼用? –
我正在使用'register'庫來添加驅動程序作爲新用戶。 @RameshKumar – may
如果條件,然後嘗試之前刪除行。 '$ this-> login_library-> register($ user_email,$ user_password);'希望它能爲你工作。還有一件事。您可以在循環外加載其他影響加載時間的庫。 –