I have a scenario where i have 2 tables i.e 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 [email protected]
2 demo_user [email protected]
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(); ?>