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);
Related
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.
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();
}
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);
}
}
Hey guys I'm having trouble running an update query through a form in codeigniter. There is no error returned, but the query does not update in the database. It should be noted there's another form on the same page running an insert query that works fine.
View:
<?php
$modify = array(
'id' => 'modify-form',
'class' => ''
);
echo form_open('main/modify_idea', $modify);
$ta_modify = array(
'id' => $row->id,
'name' => 'ta_modify',
'label' => 'ta_modify',
'placeholder' => $row->idea,
'method' => 'Post'
);
echo form_textarea($ta_modify);
echo form_hidden('facebook_id', $session['id']);
echo "<div id='button'>";
echo form_submit('submit', 'Post');
echo "</div>";
echo form_close();
?>
Controller:
public function modify_idea() {
$this->load->model('idea');
$this->idea->modify_idea();
redirect('main/members');
}
Model:
public function modify_idea() {
$data = array(
'id' => $this->input->post('id'),
'idea' => $this->input->post('ta_modify'),
'facebook_id' => $this->input->post('facebook_id')
);
$query = $this->db->query("UPDATE idea SET `idea` = ? WHERE `id` =?", array($data['idea'], $data['id']));
return $query;
}
I've utilized the following resources but still no luck:
CodeIgniter MySQL query not working
CodeIgniter MySQL Query not returning any data, even though there definitely is data to be returned!
http://www.tutorialspoint.com/mysql/mysql-update-query.htm
https://www.codeigniter.com/userguide3/database/query_builder.html#deleting-data
As always, thanks for the help!
Pass your post data from controller to model
Change your controller code:
public function modify_idea() {
$this->load->model('idea');
$data = array(
'id' => $this->input->post('id'),
'idea' => $this->input->post('ta_modify'),
'facebook_id' => $this->input->post('facebook_id')
);
$this->idea->modify_idea($data);
redirect('main/members');
}
in model do this
public function modify_idea($data) {
$query = $this->db->query("UPDATE idea SET `idea` = ? WHERE `id` =?",
array($data['idea'], $data['id']));
return $query;
}
Please pass your post values to model from controller. Then it will work.
I currently have a model that creates an array and returns it using the following:
$data = array(
'title' => $this->input->post('title'),
'slug' => $slug,
'text' => $this->input->post('text')
);
$this->db->insert('table-name', $data);
$id = $this->db->insert_id();
return $data;
Unfortunately the variable $data is empty when I get it back in my controller. How do I go about getting hold of this data to pass to my view.
The code seems fine.
Controller:
$data['mydata'] = $this->my_model->my_method();
$this->load->view('my_view', $data);
View
<?= $mydata ?>