the problem that i have is in this model. The query runs correctly if i remove the where statement, but i need the where statement in order to change one row depending on the id. when i run the query with the where statement i have no errors but not any affected row in the database.
public function update_goods()
{
$id=$this->input->post('ID');
$data=array(
'ID'=>$this->input->post('ID'),
'Title'=>$this->input->post('Title'),
'Value'=>$this->input->post('Value'),
'Description'=> $this->input->post('Description'),
);
$this->db->where('ID', $id);
$this->db->update('goods',$data);
}
That should work fine based on the docs.
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
// Produces:
// UPDATE mytable
// SET title = '{$title}', name = '{$name}', date = '{$date}'
// WHERE id = $id
I think you should take the ID from your array, though it doesn't need to be updated.
public function update_goods()
{
$data = array(
'Title' => $this->input->post('Title'),
'Value' => $this->input->post('Value'),
'Description' => $this->input->post('Description'),
);
$this->db->where('ID', $this->input->post('ID'));
$this->db->update('goods', $data);
}
Also, verify $this->input->post('ID') refers to a record in the goods table.
Related
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);
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.
public function register() {
if (isset($_POST['register'])) {
$u = $_POST['uname'];
$this->load->database();
$this->load->database();
$this->db->select('uname');
$this->db->from('login');
$this->db->where(array('uname' => $u));
$query1 = $this->db->get();
if (!$query1->num_rows() == 1) {
$data = array(
'fname' => $_POST['fname'],
'lname' => $_POST['lname'],
'dob' => $_POST['dob'],
'gender' => $_POST['gender'],
'email' => $_POST['email']
);
$this->load->database();
$this->db->insert('user', $data);
$data1 = array(
//'User_idUser'=>$_POST[$query1],
'uname' => $_POST['uname'],
'upass' => $_POST['upass']
);
$this->db->insert('login', $data1);
} else {
$_SESSION["ex"] = "User All Ready Exists";
}
}
$this->load->view('register');
}
I want to get auto increment id of user table to save in login table as a foreign key to identify which user is currently logging. the code is running perfectly.
the user table is a parent table and
login table is a child table.
User following code to get ID of last insert operation.
$insert_id = $this->db->insert_id();
And use $inser_id to add it to any table you want.
So if I open the commented line of your code,
$data1 = array(
'User_idUser' => $insert_id, // Assuming that UseridUser is your columne to store the Autoincremented user id from previous insert query.
'uname' => $_POST['uname'],
'upass' => $_POST['upass']
);
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 have this code, what I want to do is to get the primary key lastly inserted into the user table and then put that into the foreign key of the library table. I don't know how to do it with $request or what to do here?
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$data1 = array(
'user_id' => '5',// this is where I have to put the primary key from last table
'library_name' => $request['lib_name']
);
$this->model->insert('user', $data);
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');
}
Use mysql_insert_id()
-- Returns the value generated for an AUTO_INCREMENT column by the previous INSERT or UPDATE statement.
You can do like this
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$this->model->insert('user', $data);
$user_id = $this->model->select('user', $data); //select user_id from db
$data1 = array(
'user_id' => $user_id ,
'library_name' => $request['lib_name']
);
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');
}
you can do it like this
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$data1 = array(
'library_name' => $request['lib_name']
);
$this->model->insert('user', $data);
$user_id = mysql_insert_id();
$data1['userid'] = $user_id;
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');
}
If you are using any frameworks. then they have the helper functions to get last insert id, you should use them
Thanks guys, it worked all i needed to do was just to change the logic, I was using it stupidly and didn't see where to place the insert query so Its working now all i had to do was just to change the place of the insert query as given
public function register($request = array()) {
$data = array(
'user_name' => $request['username'],
'password' => $request['password'],
'email' => ($request['email'])
);
$this->model->insert('user', $data);
$data1 = array(
'user_id' => mysql_insert_id(),// now it works as insert has been done above ....
'library_name' => $request['lib_name']
);
$this->model->insert('library', $data1);
$this->redirect('uploadSongs.php');