I can't update data in a database Codeigniter - php

I can't update data in a database.i don't know what happend.please help me. Display data Press the edit button.But i can not edit data on the database.
I have posted the code below:
Controller.php
public function ajax_update()
{
$data = array(
'placename' => $this->input->post('placename'),
'description' => $this->input->post('description'),
'tel' => $this->input->post('tel'),
'latitude' => $this->input->post('latitude'),
'longtitude' => $this->input->post('longtitude'),
'placetype_id' => $this->input->post('placetype_id'),
'province_id' => $this->input->post('province_id'),
);
$this->tblplace->update(array('placeid' => $this->input->post('placeid')), $data);
echo json_encode(array("status" => TRUE));
}
Modal.php
public function save($data)
{
$this->db->insert('tblplace', $data);
return $this->db->insert_id();
}
public function update($where, $data)
{
$this->db->update('tblplace', $data, $where);
return $this->db->affected_rows();
}
I can get data from the database to show in the textbox. But can't update data.I need to help.Thank you.

Your Controller:
public function ajax_update()
{
//You should also check the post array.
$ajax_post_data = $this->input->post();
log_message('debug', 'Ajax Post Data Array:' . print_r($ajax_post_data, TRUE));
$placeid = $this->input->post('placeid');
$data = array(
'placename' => $this->input->post('placename'),
'description' => $this->input->post('description'),
'tel' => $this->input->post('tel'),
'latitude' => $this->input->post('latitude'),
'longtitude' => $this->input->post('longtitude'),
'placetype_id' => $this->input->post('placetype_id'),
'province_id' => $this->input->post('province_id'),
);
$this->load->model('tblplace');
$updte_status = $this->tblplace->update($placeid, $data);
if($updte_status == TRUE){
echo json_encode(array("status" => true));
} else {
echo json_encode(array("status" => false));
}
}
In Model:
public function update($where, $data)
{
$this->db->where('id', $where)
->update('tblplace', $data);
log_message('debug', 'Last Update Query:' . print_r($this->db->last_query(), TRUE));
log_message('debug', 'Affected row count:' . print_r(count($this->db->affected_rows()), TRUE));
if ($this->db->affected_rows() === 1) {
return TRUE;
}else{
return FALSE;
}
}
Hope this is clear and it will help you to update the data.

There is no where condition in your update function
public function update($where, $data)
{
$this->db->where($where);
$this->db->update('tblplace', $data);
return $this->db->affected_rows();
}

Related

Not working update using ajax with codeigniter

When I update one column, that changes all the columns.
How can I fix it?
Model
public function update($where, $data)
{
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
Controller:
public function ajax_update()
{
$data = array(
'book_title' => $this->input->post('book_title'),
'book_isbn' => $this->input->post('book_isbn'),
'book_yop' => $this->input->post('book_yop'),
'book_active' => $this->input->post('book_active'),
'publisher_name' => $this->input->post('publisher_name'),
'author_name' => $this->input->post('author_name'),
);
$this->user_model->update( $this->input->post('book_id'), $data);
echo json_encode(array("status" => TRUE));
}
Make sure all of your data are ok and especially your id then update like this:
$this->db->where('book_id', $this->input->post('book_id'));
$this->db->update($this->table, $data);
Based on CodeIgniter documentation I would suggest changing the code that defines the 'where' part:
$this->user_model->update( array('book_id' => $this->input->post('book_id')), $data);

Codeigniter not adding the Items to Cart

I'm trying to add items to the cart when i tried first time its worked but when i did the update function, the insert function not working, Can Anyone Help? thanks in advance
This is the code
public function addCart($id,$qty,$price,$name,$color,$cat)
{
$size=$this->input->post('size');
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $price,
'name' => $name,
'options' => array('Size' => $size, 'Color' => $color)
);
$res=$this->cart->insert($data);
if($res == true){
$this->session->set_flashdata('success',urldecode($name).' Added in Cart.');
redirect('product/view_all/'.$cat);
}
else{
$this->session->set_flashdata('success','Product Adding Failed :(');
redirect('product/view_all/'.$cat);
}
}
public function viewCart()
{
$data['title']="Products in Cart | DreamShopie.in";
$this->load->view('templates/header',$data);
$this->load->view('templates/menu');
$this->load->view('main_page/cartView',$data);
$this->load->view('templates/footer');
}
public function removeCart($id)
{
$data = array(
'rowid' => $id,
'qty' => 0
);
$this->cart->update($data);
$this->session->set_flashdata('success', 'Item Removed');
redirect('cart/viewCart');
}
Assuming $this->cart is your db , didnt you miss db name?
$this->cart->insert('db_name', $data);
insert() and update() function in the cart model should be like this
public function insert($data){
return $this->db->insert('table', $data);
}
public function update($data){
$this->db->where('column', $value);
$this->db->set($data);
return $this->db->update('table');
}
I own find the answer , the errorr occurred because the name contains illegal characters from the URL , so I use urldecode function for name
Problem solved.

last inserted id in Php codeIgniter

In my model I have return below function for get record id
function getLastInserted()
{
$query = $this->db->select("MAX(`UserID`)+1 as userid")->get("registeration");
return $query->result();
}
Now I want to pass that ID to mycontroller for record insertion
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
Now I want to access model function in controller and pass record id in data function How I do ??
set return in model and in controller write
$insert_id=$this->db->insert_id();
In Controller load your model first using
$this->load->model('model_name');
Then call to model's getLastInserted() function.
$ID = $this->model_name->getLastInserted();//you will get id here
In model return $query->row()->userid; insead return of $query->result().
Then modify the controller:
public function newregistration()
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $this->model_name->getLastInserted(),
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}
hi i worked out the solution on last inserted id dear here the code:
controller:
function sample() {
$sid=$this->adminmodel->getLastInserted();
$this->adminmodel->newregistration( $sid);
}
model:
function getLastInserted()
{
$query = $query = $this->db->select('id')->order_by('id','desc')->limit(1)->get('employee_outs')->row('id');
return $query;
}
model:
public function newregistration($sid)
{
if ($this->form_validation->run())
{
$data = array(
'UserID'=> $sid,
'Name' => $this->input->post('Name'),
'MobileNo' => $this->input->post('MobileNo'),
'IMEINumber' => $this->input->post('IMEINumber'),
'City' => $this->input->post('City')
);
$this->adminmodel->insertregistration($data);
}
}

Converting Array to String Codeigniter 3.0

I am trying to insert an element into my 'category' table and use the inserted element's id to insert another element into my 'subcategory' table.
This is my code in my controller
public function insertCategory(){
$category = $this->input->post('category');
$subcategory = $this->input->post('subcategory');
$this->form_validation->set_rules('category', 'Category', 'required');
$this->form_validation->set_rules('subcategory', 'Subcategory', 'required');
if($this->form_validation->run() == FALSE){
$this->load->view('ADMIN_ADDCategory');
}
else{
$category_id = $this->admin_model->insertCategory($category);
$this->admin_model->insertSubcategory($category_id, $subcategory);
}
}
And this is my code in my model
function insertCategory($category){
$data = array(
'category' => $category,
'status' => 1
);
$this->db->insert('category', $data);
$this->db->select('id');
$this->db->from('category');
$this->db->where('category', $category);
$this->db->limit(1);
$query = $this->db->get();
return $query->result();
}
function insertSubcategory($category_id, $subcategory){
$data = array(
'category_id' => $category_id,
'subcategory' => $subcategory,
'status' => 1
);
$this->db->insert('subcategory', $data);
}
However I am getting this error
I already tried using $category_id = (array) $this->admin_model->insertCategory($category); but it still doesn't work
How do I overcome this error? Thank you for the help.
$this->admin_model->insertSubcategory($category_id, $subcategory);
$category_id is an array , so , you must do it like this:
$this->admin_model->insertSubcategory($category_id[0]->field, $subcategory);
You should try this
$this->admin_model->insertSubcategory($category_id[0]->id;, $subcategory);
try this code
Model
public function insert($table, $data)
{
if($this->db->insert($table,array $data))
{
return $this->db->insert_id();
}else
return false
}
controller
public function insertCategory(){
$this->form_validation->set_rules('category', 'Category', 'required');
$this->form_validation->set_rules('subcategory', 'Subcategory', 'required');
if($this->form_validation->run() == FALSE){
$this->load->view('ADMIN_ADDCategory');
}
else{
$category = $this->input->post('category');
$subcategory = $this->input->post('subcategory');
//data must be an array
$data = [
'category' => $category,
'status' => '1'
];
$category_id = $this->admin_model->insert('tablename',$data)
if($category_id != false)
{
$data = [
'category_id' => $category_id,
'subcategory' => $subcategory,
'status' => 1
];
if($this->admin_model->insert('table name', $data) != false)
{
echo 'success';
//or load your success page
}else{
echo 'failed';
//or load your success page
}
}else{
echo 'failed';
//or load your success page
}
}
}

Using $insert_id to update two tables - codeigniter

Is there a way that I can update a form input using something like insert_id?
I have a form for adding factories to a table and I use the insert_id to store the factories id in another table too after submit.
this all works great, but is there such a way for $this->db->update?
My model for updating:
function updatebedrijf($id, $data)
{
$this->db->where('idbedrijven', $id);
$this->db->update('bedrijven', $data);
}
My controller function for updating:
function updatebedrijven()
{
$dbres = $this->db->get('categorieen');
$ddmenu = array();
foreach ($dbres->result_array() as $tablerow) {
$ddmenu[$tablerow['idcategorieen']] = $tablerow['Categorie'];
}
$data['opties'] = $ddmenu;
$data['info'] = $this->members_model->getbedrijf($id);
$data['id'] = $this->uri->segment(3);
$this->load->view('members/header');
$this->load->view('members/editform', $data);
$this->load->view('members/footer');
}
for adding factories i use this with insert_id:
controller:
function addbedrijven()
{
$this->members_model->addbedrijf();
redirect('members/index');
}
model:
function addbedrijf()
{
$data1 = array(
'Bedrijfsnaam' => $this->input->post('Bedrijfsnaam'),
'Postcode' => $this->input->post('Postcode'),
'Plaats' => $this->input->post('Plaats'),
'Telefoonnummer' => $this->input->post('Telefoonnummer'),
'Email' => $this->input->post('Email'),
'Website' => $this->input->post('Website'),
'Profiel' => $this->input->post('Profiel'),
'Adres' => $this->input->post('Adres'),
'logo' => $this->input->post('logo')
);
$this->db->insert('bedrijven',$data1);
if($this->db->affected_rows() >= 1)
{
$to_bedrijfcategorieen['idbedrijven'] = $this->db->insert_id();
$to_bedrijfcategorieen['idcategorieen'] = $this->input->post('categorieen');
$this->insert_bedrijfcat($to_bedrijfcategorieen);
}else{
return FALSE;
}
}
function insert_bedrijfcat($data1)
{
$this->db->insert('bedrijfcategorieen', $data1);
return $this->db->affected_rows() >= 1 ? TRUE : FALSE;
}
Hope it's clear enough.

Categories