codeigniter transaction is not working - codeigniter

I am using codeigniter transaction. i wrote a function but it is not working.It was supposed to complete the transaction while submitting my form. Now its not saving with this code.with out transition code it is working. how can i fix this:
public function twotable_insertData() {
$this->db->trans_start();
$data = array(
'brand_name' => $this->input->post('f_name'),
'brand_user_name' => $this->input->post('l_name'),
);
$brand_id = $this->m_common->insert_row('brands', $data);
echo '$brand_id';
$data1 = array(
'brand_id' => $brand_id,
'stadium_id' => $this->input->post('stadium'),
'concession_stand_no' => $this->input->post('con_std_no'),
);
$this->m_common->insert_row('concession_stands', $data1);
redirect('backend/brand/view_brand');
if ($this->db->trans_status() === FALSE) {
$this->db->trans_rollback();
} else {
echo $this->db->trans_complete();
}
}

I have updated your query...
$this->db->trans_start();
$data = array(
'brand_name' => $this->input->post('f_name'),
'brand_user_name' => $this->input->post('l_name')
);
$brand_id = $this->m_common->insert_row('brands', $data);
// echo '$brand_id';
$data1 = array(
'brand_id' => $brand_id,
'stadium_id' => $this->input->post('stadium'),
'concession_stand_no' => $this->input->post('con_std_no')
);
$this->m_common->insert_row('concession_stands', $data1);
$this->db->trans_complete();
if($this->db->trans_status() === FALSE){
// Check if transaction result successful
$this->db->trans_rollback();
$this->session->set_flashdata('failure', 'Transaction Fails.');
}else{
$this->db->trans_complete();
$this->session->set_flashdata('success', 'Transaction Success.');
}
redirect('backend/brand/view_brand');

Related

Laravel - How to Update Dynamic Input Fields

In my Laravel-8, I have this code:
public function add(Request $request){
if( $request->ajax() ){
$rules = array(
'first_name.*' => 'required',
'country.*' => 'required'
);
$error = Validator::make($request->all(),$rules);
if($error->fails()){
return response()->json([
'error' => $error->errors()->all(),
]);
}
$first_name = $request->first_name;
$country = $request->country;
for( $count = 0; $count < count($first_name); $count++ ){
$data = array(
'first_name' => $first_name[$count],
'country' => $country[$count],
);
$insert_data[] = $data;
}
DynamicField::insert($insert_data);
return response()->json([
'success' => 'Data added Successfully',
]);
}
}
This successfully Inserts record.
How do I do the update to this same code?
I mean public function update ...

CI - getting no error but even database not update

Please help me, i just messed up. I just checked the code again and again , manipulate code every time but database not updating.
controller
function updt_ctrl($id="",$userData=""){
$userData = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$res=$this->user_model->updt_row($id,$userData);
if($res){
$this->load->view('profile_view');
}
else{
echo "something is wrong";
}
}
Model
function updt_row($id,$userData){
//$this->db->set($userData);
$this->db->where('id',$id);
return $this->db->update('user',$userData);
}
try this.
CONTROLLER
function updt_ctrl(){
$this->load->helper('user_model');
$userData['fname'] = $this->input->post('fname');
$userData['lname'] = $this->input->post('lname');
$userData['email'] = $this->input->post('email');
$userData['password'] = $this->input->post('password');
$this->user_model->updt_row($id,$userData);
}
MODEL
function updt_row($id,$userData){
$this->db->where('id', $id);
$this->db->update('user',$userData);
//after done update, load this view
$this->load->view('profile_view');
}
If your embed a correct Form action on your view. this should be working 100%
Replace your contoller with this
controller
function updt_ctrl($id){
$userData = array(
'fname' => $this->input->post('fname'),
'lname' => $this->input->post('lname'),
'email' => $this->input->post('email'),
'password' => $this->input->post('password')
);
$res=$this->user_model->updt_row($id,$userData);
if($res){
$this->load->view('profile_view');
}
else{
echo "something is wrong";
}
}

file_get_contents() expects parameter 1 to be a valid path

My image is uploading to images/news successfully. I want to now save that image in the database. but it keeps giving me the above error. My model query is correct because i use it to insert other data and it works.
How can i save the image into the database once the user clicks submit?
My controller:
function news()
{
$config = array(
'upload_path' => "./images/news",
'allowed_types' => "gif|jpg|png|jpeg|JPG",
'overwrite' =>False,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
$this->load->helper(array('form', 'url'));
if($this->upload->do_upload())
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('manage_news',$data);
}
else
{
$message2 = "Please choose another file type. gif,jpg,png,jpeg,pdf ";
echo "<script type='text/javascript'>alert('$message2'); </script>";
redirect('resetPasswordController/add_news', 'refresh');
}
$this->db->trans_start();
$data = array('upload_data' => $this->upload->data());
$data = array(
'image' => file_get_contents( $data )
);
$this->load->model('users_model');
$this->users_model->insert($data);
$this->db->trans_complete();
$this->db->trans_complete();
redirect('resetPasswordController/manage_news', 'refresh');
}
my view:
<?php echo form_open_multipart('resetPasswordController/news');?>
<label>Image</label>
<input name = "userfile" type="file" />
my model:
function insert($data){
$this->db->insert('news',$data);
return $this->db->insert_id();
}
$data contain array values, you have to use upload_data
for ex :
$data = array('upload_data' => $this->upload->data());
$data = array(
'image' => file_get_contents( $data )
);
change this to
$data = array('upload_data' => $this->upload->data());
$data = array(
'image' => file_get_contents( $data['upload_data'] )
);
or
you can directly use upload data function
$data = array(
'image' => file_get_contents( $this->upload->data())
);

How to use the same insert_id() in CodeIgniter from one function to another function [duplicate]

I have a problem with insert_id. I'm using CodeIgniter. I have a method in which I insert some values and in it I return
$insert_id=$this->db->insert_id();
return $insert_id;`
I want to use this value in another method in the same model. I tried that: $insert_id=$this->add_teacher_subject(); but in this way first method runs again and I doesn't receive last_insert_id , but this value +1. Please, tell me how could I solve this problem?
My model is:
public function add_teacher_subject() {
$date = new DateTime("now");
$data=array(
'teacher_id'=>$this->input->post('teacher'),
'user_id'=>$this->session->userdata['user_id'],
'created_at'=>$date->format('Y-m-d H:i:s')
);
$this->db->insert('student_surveys',$data);
$insert_id=$this->db->insert_id();
return $insert_id;
}
public function survey_fill()
{
$insert_id=$this->add_teacher_subject();
if ($this->input->post('answer')) {
$answers= $this->input->post('answer');
if (null !==($this->input->post('submit'))) {
$date = new DateTime("now");
foreach($answers as $question_id=>$answer)
{
$data = array(
'user_id'=>$this->session->userdata['user_id'],
'question_id'=>$question_id,
'answer'=>$answer,
'student_survey_id' => $insert_id
);
$this->db->insert('survey_answers', $data);
}
}
You can use SESSION[] for keeping last insert id:
public function add_teacher_subject() {
$date = new DateTime("now");
$data = array(
'teacher_id' => $this->input->post('teacher'),
'user_id' => $this->session->userdata['user_id'],
'created_at' => $date->format('Y-m-d H:i:s')
);
$this->db->insert('student_surveys', $data);
$insert_id = $this->db->insert_id();
$newdata = array('insert_id' => $insert_id);
$this->session->set_userdata($newdata);
return $insert_id;
}
public function survey_fill() {
$insert_id = $this->session->userdata('insert_id');
if ($this->input->post('answer')) {
$answers = $this->input->post('answer');
if (null !== ($this->input->post('submit'))) {
$date = new DateTime("now");
foreach ($answers as $question_id => $answer) {
$data = array(
'user_id' => $this->session->userdata['user_id'],
'question_id' => $question_id,
'answer' => $answer,
'student_survey_id' => $insert_id
);
$this->db->insert('survey_answers', $data);
}
}
}
}

How to set the logged in user ID on a session (CodeIgniter)?

I am having trouble, getting the logged in user ID from the database...
This is my controller code :
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if ($query){
$this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
This is my model function where I return the user id from the database:
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users');
foreach ($query->result() as $row)
{
$user_id = $row->id;
}
return $user_id;
}
Everything works perfect, except that $user_id returns empty... What am I doing wrong?
Try:
$user_id= $this->users_model->get_userID($this->input->post('email'));
try modifying the model function to this -
function get_userID($email){
$this->db->where('email',$email);
$query = $this->db->get('users')->row_array(); // retrieves only first-row.
return $query['id'];
}
In controller the function needs just a bit of modification as follows -
function validation(){
$this->load->model('users_model');
$query = $this->users_model->validate();
if($query){
//$user_id was not assigned before :)
$user_id = $this->users_model->get_userID($this->input->post('email'));
$data = array(
'email' => $this->input->post('email'),
'is_logged_in' => true,
'user_id' => $user_id
);
$this->session->set_userdata($data);
redirect('home');
}
else {
$this->index();
}
}
hope it helps,
faisal ahmed
blog / site

Resources