我有一个表单输入:
<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 :(得分: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
}
答案 1 :(得分:0)
[求助]我错过了SQL中的where
语法,我可以用它来检查变量是否与表中的值匹配:)