i have tried to update data of my database, but there is no error
this my method
public function update_pj_si(request $request)
{
$id = $request->id;
DB::table('tbl_profil_penyedia')
->where('id_profil_penyedia', $id)
->update(array('status' => 1));
return redirect('/verif/pj_si');
}
if i run this method, it run correctly and no error but the database is not updated.
how can i fix this?
The $id variable doesn't contain a value that exists in your table, tbl_profil_penyedia, for the field id_profil_penyedia. It is as simple as that.
You are trying to update a profil_penyedia that doesn't exist.
The update call returning 0, means it didn't update any rows, which means your where condition didn't yield any results to be updated.
Related
the code bellow should be updating and saving the weekly_item using the same button. The update works fine. I have manually added a record in database and its getting updated. But when it comes to saving the function not saving the data.
public function store(){
$WeeklyItems = weekly_item::firstOrNew(['item_id'=>request('item_id')],
['restaurant_id'=>request('restaurant_id')],
['avil_date'=>request('avile_date')],
['start_time'=>request('start_time')]
);
$WeeklyItems->item_id = request('item_id');
$WeeklyItems->restaurant_id = request('restaurant_id');
$WeeklyItems->start_time=request('start_time');
$WeeklyItems->end_time=request('end_time');
$WeeklyItems->tiffin=request('tiffin-switch');
$WeeklyItems->lunch=request('lunch-switch');
$WeeklyItems->snacks=request('snacks-switch');
$WeeklyItems->dinner=request('dinner-switch');
$WeeklyItems->special=request('special-switch');
$WeeklyItems->extend=request('extend-avil');
$WeeklyItems->unit=request('unit');
$WeeklyItems->quantity=request('quantity');
$WeeklyItems->avil_date=request('avil_date');
$WeeklyItems->save();
editWeeklyItem(request('item_id'),request('restaurant_id'));
}
I am fed up. Tried all the inbuilt functions(firstOrNew,firstOrCreate...etc) and failed. Now I am using two different methods for create and update operations. The save operations works fine. But the update method not working. Someone please
public function updateWeeklyItem(){
$item_id=request('item_id');
$restaurant_id=request('restaurant_id');
$avil_date=request('avil_date');
weekly_item::where(function($query) use ($item_id, $restaurant_id,$avil_date){
$query->where('item_id', $item_id)
->where('restaurant_id', $restaurant_id)
>where('avil_date', $avil_date);
})->update ([
'start_time'=>request('start_time'),
'end_time'=>request('end_time'),
'tiffin-switch'=>request('tiffin-switch'),
'lunch-switch'=>request('lunch-switch'),
'snacks-switch'=>request('snacks-switch'),
'dinner-switch'=>request('dinner-switch'),
'special-switch'=>request('special-switch'),
'extend-avil'=>request('extend-avil'),
'unit'=>request('unit'),
'quantity'=>request('quantity')
]);
editWeeklyItem(request('item_id'),request('restaurant_id'));
}
My function works with any value of the parameter $champ except with the value "utilisateur" where the function returns false, even though the update works.
I've tried to use transactions to avoid the issue, but it's still here.
public function updateChamp($champ, $newVal, $sim)
{
$this->db->trans_start();
$this->db->set($champ,$newVal)
->where("numero_sim",$sim)
->update("forfait");
$this->db->trans_complete();
return($this->db->trans_status());
}
Here, an old version of my function :
public function updateChamp($champ, $newVal, $sim)
{
return $this->db->set($champ,$newVal)
->where("numero_sim",$sim)
->update("forfait");
}
So, I found the problem.
I forgot that I made a trigger which insert data in another table and the problem was that this table had a column which can not be null but it didn't have a default value. And as i wasn't giving a value for this column in my trigger, $this->db->update() was returning false.
I have a function that is taking an id as a param, and updates the db using the DB method. however, when i run the code, the variable is not being passed to the method. to test, i replaced $id with an integer and it worked, so i think the DB method can not access the variable from the parameter
public function disable($id)
{
// Update the user status to 0 from 1
DB::table('employees')->where('id', $id)->update(['status' => 0]);
return redirect('/employee')->with('error', 'User is disabled, all related accounts are now shutdown!...');
}
Update:
Forgot to mention that i have already checked, and the param comes inside the function OK, i can return the id outside of the method
SOLUTION
As shown in the comment, the varDump was returning "id=9" where i need "9", i noticed in the form piece of my code, there was an extra "id=" before the id which caused a malfunction.
Use function as disable(Request $request) and get id as $request->id
public function disable(Request $request)
{
// Update the user status to 0 from 1
DB::table('employees')->where('id', $request->id)->update(['status' => 0]);
return redirect('/employee')->with('error', 'User is disabled, all related accounts are now shutdown!...');
}
I have a function which allow me to delete a row on a table. But the problem is that, for database integrity, I have to delete another row on another table that is in relation with the record I want to delete. This is my function :
public function delete($tab)
{
$this->db->where('id', $tab['id']);
$this->db->delete('agent', $tab);
// Here the query is "DELETE from agent WHERE id = $tab['id']"
$this->db->where('id_agent', $tab['id']);
$this->db->delete('agent_vendeuse', $tab);
// Here the query gives "Delete from agent_vendeuse WHERE id_agent= $tab['id'] AND id=$tab['id'];" Which is where the error comes from
}
I have an error database error on my function :
DELETE FROM `agent_vendeuse` WHERE `id_agent` = '2' AND `id` = '2'
Which means that after I affected the new index 'id_agent' for the where clause, the previous one 'id' is still in cache.
The $tab variable is coming from $_POST of my form. I just changed its name in function.
I guess I have to clean the cache after the first deletion but how to write it ?
The second parameter of the delete function is a where clause, so I'm guessing that probably what happens is that the line $this->db->where('id', $tab['id']); gets ignored on the first delete() call, and for some reason on the second delete() call the 'where clause' you're defining in the parameter as $tab gets ignored and the two where() calls gets used instead.
Just remove the $tab from the parameters passed to delete() and you should be ok:
public function delete($tab)
{
$this->db->where('id', $tab['id']);
$this->db->delete('agent');
$this->db->where('id_agent', $tab['id']);
$this->db->delete('agent_vendeuse');
}
im using codeigniter 2.0.2 and this is from its userguide
$data = array(
'title' => $title,
'name' => $name,
'date' => $date
);
$this->db->where('id', $id);
$this->db->update('mytable', $data);
my question is once this executed how do you find its executed correctly or not?
The update function returns a value:
$result = $this->db->update('mytable', $data);
Check that value for either being TRUE (success) or FALSE (failure). update runs query internally and then returns the return value of query (Ref):
The query() function returns a database result object when "read" type queries are run, which you can use to show your results. When "write" type queries are run it simply returns TRUE or FALSE depending on success or failure.
Use
$this->db->affected_rows()
to see how many rows have been affected on write type queries (update, insert, etc...)
http://codeigniter.com/user_guide/database/helpers.html
Both answers was valid. You just have to use each one depending on the case. If you are just checking if the query was executed use this method:
$result = $this->db->update('mytable', $data);
if you really want the number of rows affected the second method is better:
$this->db->affected_rows()
however I always use the second method. The update query is a good example why. A query can be successful and still there was nothing updated on the database because the value that you were trying to update was actually equal to the value you are sending in the query.
This would be a false positive. And the affected rows would be 0.
I hope it helped =)
When developing CodeIgniter model methods, I find that I consistently return desirable values depending on the type of database write that is executed. It is often important to differentiate between a query that has run successfully versus a query that has actually changed a record.
For an update or delete query, I'll return the number of affected rows -- this will be most helpful to controller methods that call it. If you are performing logging (to keep track of change history), then ONLY log something if there is a change to the row; otherwise you are unnecessarily bloating your change history logs.
public function update(int $id, array $newData) :int
{
$oldData = $this->db->get_where('mytable', ['id' => $id])->row_array();
if ($this->db->update('mytable', $newData, ['id' => $id])) {
$affectedRows = $this->db->affected_rows();
if ($affectedRows) {
$this->Log->mytableUpdate($id, $newData, $oldData);
}
return $affectedRows;
}
return 0;
}
For insert queries, I always return the auto-incremented id of the newly inserted row via insert_id().
If using the PDO driver with PostgreSQL, or using the Interbase driver, this function requires a $name parameter, which specifies the appropriate sequence to check for the insert id.
public function insert(array $newData) :int
{
if ($this->db->insert('mytable', $newData)) {
$newId = $this->db->insert_id(); // or insert_id('mytable')
$this->Log->mytableInsert($newId, $newData);
return $newId;
}
return 0;
}
Having consistent return types in your model methods will make your project easier to develop and maintain. The script that calls these model methods will be able to quickly assess the outcome by making a "falsey" check.