I have a scenario where i have 2 tables ie users and request and i am asking the user to fill a form, in which along with other data he has to fill email also. Now i want that if the user enters an email it should simply add the data in users table and pick the last insert id and then go ahead and save the data+last inserted id in request table and display the message
Your account is created..
Till here i have done the coding, but the part where i am stuck is
I want that if the user enters an email that is already present in users table then the code should pick the id that is present against that email and store it along with form data in the request table and display the message
"Request is submitted but you already have an account, please login to check furthur details "
users table
id name email
1 sam sam@gmail.com
2 demo_user demo@gmail.com
request table
id email userid
1 demo@gmail 2
Controller
public function submit()
{
$this->form_validation->set_rules('email','Email','trim|required');
if($this->form_validation->run() == FALSE)
{
$erdata = array
(
'error' => validation_errors()
);
$this->session->set_flashdata($erdata);
redirect('home/index');
}
else
{
if($this->user_model->instant_submit())
{
$this->session->set_flashdata('msg','Your account is created');
}
else
{
echo "failed";
}
redirect('home/index');
}
}
Model
public function instant_submit()
{
$userdata = array(
'email' => $this->input->post('email')
);
$insert_data = $this->db->insert('users', $userdata);
$lastid = $this->db->insert_id();
$reqdata = array(
'email' => $this->input->post('email'),
'userid' => $lastid,
'status'=>'pending'
);
$insert_request = $this->db->insert('request', $reqdata);
return $insert_data;
}
View
<?php if($this->session->flashdata('msg')): ?>
<?php echo $this->session->flashdata('msg'); ?>
<?php endif; ?>
<?php
$reg_attributes = array('id'=>'form','role'=>"form");
echo form_open('home/submit', $reg_attributes);
?>
<?php
$data = array(
'type'=>'text',
'name'=>'email',
'placeholder'=>'Email',
'class'=>'form-control',
'id'=>'form-email'
);
echo form_input($data);
?>
<?php
$data = array(
'type'=>'submit',
'class'=>'btn-primary',
'name'=>'submit',
'content'=>'Submit!'
);
echo form_button($data);
?>
<?php echo form_close(); ?>
is_unique
Returns FALSE
if the form element is not unique to the table and field name in the parameter.
Syntax :
is_unique[table.field]
Example :
$this->form_validation->set_rules('email', 'Email', 'required|valid_email|is_unique[users.email]');
This will solve your problem
In your controller before you call this $this->user_model->instant_submit()
add one more condition to check if email id already exist or not like below.
Controller
public function submit()
{
$this->form_validation->set_rules('email','Email','trim|required');
if($this->form_validation->run() == FALSE)
{
$erdata = array
(
'error' => validation_errors()
);
$this->session->set_flashdata($erdata);
redirect('home/index');
}
else
{
if(!$this->user_model->check_user_exist($this->input->post('email'))) {
if($this->user_model->instant_submit())
{
$this->session->set_flashdata('msg','Your account is created');
}
else
{
echo "failed";
}
} else {
//do whatever operation you want to do if user is already exist;
$this->session->set_flashdata('msg','Request is submitted but you already have an account, please login to check furthur details ');
}
redirect('home/index');
}
}
Now in your model create a function which can check the data of user
public function check_user_exist($email_id) {
$this->db->select('id');
$this->db->where('email',$email_id);
$query = $this->db->get('users');
$data = $query->row();
if($query->num_rows() == 1) {
return $data->id;
} else {
return false;
}
}
Note : Remember it will send false if user not found so it will create a new entry of that user.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.