Update in Codeigniter for Beginner - codeigniter

Can anyone help me to update in Codeigniter?
This is my model code:
function update($data,$userid)
{
$data = array(
'dob' => $dob,
'sex' => $sex,
);
$this->db->where('user_id', $userid);
$this->db->update('profile', $data);
}

Assuming you are passing dob and sex via the data variable in the function you're just trying to access those incorrectly.
function update($data,$userid)
{
$data = array(
'dob' => $data['dob'],
'sex' => $data['sex'],
);
$this->db->where('user_id', $userid);
$this->db->update('profile', $data);
}
And for that matter if that is all that's in that data variable you can lose the first part entirely since it is really just replicating what you already have.
function update($data,$userid)
{
$this->db->where('user_id', $userid);
$this->db->update('profile', $data);
}

$where = array('id'=>'10','email'=>'dude#dude.com');
$data = array('username'=>'lol');
function update($data,$where){
foreach($where as $key=>$value){
$this->db->where($key,$value);
}
$this->db->update('users',$data);
}

Related

laravel excel import data does not properly working

I’m working on laravel excel import. The data can be loaded using
$data = Excel::load($path)->get(); command. But, when i try to loop through $data object and put it in $insert[], some fields remaining empty.
my import function look like
public function import(request $request) {
$path = $request->file('select_file')->getRealPath();
$data = Excel::load($path)->get();
if(!empty($data) && $data->count()){
foreach ($data as $key => $value) {
$insert[] = [
'Item_name' => $value->Item_name,
'Manufacturer' => $value->Manufacturer,
'Serial_no' => $value->Serial_no,
'Model_no' => $value->Model_no,
'status' => $value->status,
'Price' => $value->Price,
'photo' => $value->photo,
'user_id' => $value->user_id,
'deletedBy' => $value->deletedBy,
'created_at' => $value->created_at,
'updated_at' => $value->updated_at,
];
}
if(!empty($insert)){
$insertData = DB::table('inventories')->insert($insert);
if ($insertData) {
Session::flash('success', 'Your Data has successfully imported');
}else {
Session::flash('error', 'Error inserting the data..');
return redirect()->back();
}
}
}
return redirect()->back();
}
when I dd($data); the result looks as
and the result of dd($insert); looks as
if any friend can help me that why some fields like Item_name, Manufacturer, and Serial_no remain null, would be appreciated.
solved!
all value->item_names must write in lowercase; I thought that it should match with the database table column names.

How to update multiple rows of a table in Laravel?

Helper,
I wanna update multiple addresses for a particular user_id, with out changing its existing order. Also I'm using relation here.
public function update(Request $request, $id){
$data['id'] = $id;
$user = User::with('addresses')->find($id);
$j = $user['addresses'][0]['id'];
foreach($request->address_id as $key => $i){
$i++;
}
foreach($request->address1 as $key => $v){
if($j<$i){
$user->addresses()->whereUserId($data['id'])->update([
'address1' => $v,
'address2' => $request->address2[$key],
'state' => $request->state[$key],
'country' => $request->country[$key]
]);
}
$j++;}
}
I'm getting input somewhat like this,
{"_method":"PATCH","address_id":["92","93"],"address1":["aaa","xxx"],"address2":["bbb","yyy"],"state":["ccc","zzz"],"country":["India","India"]}
When I do my update the last input, which means, xxx,yyy,zzz,India is getting update for both rows92,93

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);
}
}
}
}

Codeigniter "No tables used", get_where()

So, i'm trying to create a function that removes a post, and inserts the values into a table named "removed_post".
Now, i get the error:
No tables used
SELECT *
Line Number: 330
What causes this? I have google'd it ALOT, and tried different "answers", without any success.
Here is my code:
class Remove extends CI_Controller {
function post($id = null) {
$this->db->get_where('comments', array('id' => $id));
$query = $this->db->get();
$data = array(
'amne_id' => $query->row()->amne_id,
'user_id' => $query->row()->user_id,
'date_created' => $query->row()->date,
);
$this->db->insert('removed_post', $data);
$this->db->delete('comments', array('id' => $id));
redirect('/');
}
EDIT: And yes, the code is not optimized.
$this->db->get_where('comments', array('id' => $id));
$query = $this->db->get();
should be:
$query = $this->db->get_where('comments', array('id' => $id));
In your code the first query is run but you are not capturing the result. The second line tries to capture results for a blank query. get_where() calls get() for you so you don't have to call it explicitly.
new error
$row = $query->row();
$data = array(
'amne_id' => $row->amne_id,
'user_id' => $row->user_id,
'date_created' => $row->date,
);

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