I have a table of values that uses a Where clause to find the row and then updates the values in the row. However it is deleting the value it is searching for after it is already found. So it finds the row using the stockid (not a PK) and updates the row, but leaves the stockid blank afterwards
$data = array(
'towSet' => $towSet,
'transWare' => $transWare,
'oceanFreightBooked' => $oceanFreightBooked,
'BOLrec' => $BOLrec,
'BOLsent' => $BOLsent,
);
$this->db
->where('stockid', $stockHold)
->update('logistics_tracking', $data);
The Strange part is that using this code doing the same thing it works perfectly fine
$data = array(
'recTitle' => $recTitle,
'recPOA' => $recPOA,
'recTitleState' => $recTitleState,
'titleSent' => $titleSent,
);
$this->db
->where('stockid', $stockHold)
->update('title_tracking', $data);
So as a reminder all the other values (towSet, transWare, oceanFreightBooked, ect.. ) all insert and it just blanks out the stockid after updating
Any ideas would be greatly appreciated
Yeah the code was fine, apparently I was just doing too much with one function. Created an external function and passed through an array of the data. Weird.
Thanks for looking!
Related
I insert multiple values into my database using saveField function. Here is my code.
$this->Fuel->saveField(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
);
It's not a form value. Value automatically insert when the page is refreshed.
It's only inserts first value 'device_id', $int_fuel_func[0],. Other two values are not insert into my database. Any one suggest me how to insert these values.
Thanks in advance
Cake version 2.7.5
You can simply do this as well.
$data = array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
);
$this->Fuel->save($data);
Also try this blog tutorial (cakephp blog tutorial) if you haven't been there before to be more familiar with these things.
My id is not auto Increment it is defined as varchar. Time is not include there also. I got my answer. I use it and it works.
$this->Fuel->set(
array(
'device_id' => $int_fuel_value[0],
'func_code' => $int_fuel_value[1],
'device_data' => $int_fuel_value[2]
)
);
$this->Fuel->save();
saveField function is only for updating one field! use updateAll and pass an array to it :
$this->Fuel->updateAll(
array(
'device_id', $int_fuel_func[0],
'func_code', $int_fuel_func[1],
'device_data', $int_fuel_func[2]
),
array('Fuel.id' => 234) // conditions for the Fuel you want to update
);
I have an event table with this fields:
I use this code to insert data in this table :
$oneEvent = array(
'trainer_id' => $event['trainer_id'],
'formation_id' => $event['formation_id'],
'title' => $event['title'],
'start' => $event['start'][$i],
'end' => $event['end'][$i],
);
$success = $this->Event->save($oneEvent);
$events_id= $this->Event->inserted_ids;
when I run this code I get true and ID of insert element (showed using debug)
but in database i can't see this field never !!!!.
and when I insert data in phpmyadmin when this request INSERT INTO events(title,start, end, trainer_id, formation_id) VALUES ('Départ B','2016-11-18 10:00:00','2016-11-18 11:00:00','13','1') it worked
I didn't know what happen here !!??
I solved the problem by using $dataSource->commit(); after calling save() function
I am having an issue with updating a MySql table using codeigniter.
Basically, its inserting 'img' the characters, into the table rather than the value of the variable.
This is so strange!
Here is my model:
public function update_course_progress($progress_data) {
$course_id = $progress_data['course_id'];
$user_id = $progress_data['user_id'];
$progress = $progress_data['progress'];
$status = $progress_data['status'];
$update_data = array (
'progress' => $progress,
'status' => $status,
);
// perform update on the matching row
$this->db->update('training_stats', $update_data, array('course_id' => $course_id, 'user_id' => $user_id));
}
So, the issue is with 'progress' instead of inserting the value of this variable it is inserting 'img'???
So, if i var_dump $update_data i get this:
array(2) {
["progress"]=> string(2) "1a"
["status"]=> string(1) "i"
}
Which is correct:
And if i use the profiler in CI to get the db queries, this is what I get:
UPDATE `training_stats`
SET `progress` = '1a', `status` = 'i'
WHERE `course_id` = '8'
AND `user_id` = '2'
Which is correct.
So WHY ON EARTH is it inserting null into the db instead of 1a.
The table structure for this column is VARCHAR(4).
progress varchar(4) NOT NULL DEFAULT '0',
What the hell is going on? why on earth is it img input???
What can be wrong?
UPDATE:
As i was debugging, i tried an insert instead of an update, and 2 rows were inserted. The first row was the expected data, and the second row was the data with 'img' in it. Both rows were the same except for the 'progress' column, which had img inserted in the second row.
So obviously it had been updating the row with the correct data and then overwriting it with the incorrect data.
But now why are there 2 rows being inserted? There is no loop? and why is the CI profiler not logging the second query, if that is indeed what is happening
As of per documentation of CI I will use the more standard method of updating data.
$updateArray = array (
'progress' => $progress_data['progress'],
'status' => $progress_data['status'],
);
$whereArray = array(
'course_id' => $progress_data['course_id'],
'user_id' => $progress_data['user_id']
)
$this->db->set($updateArray);
$this->db->where($whereArray);
$this->db->update('training_stats');
This should do, I also think you shouldn't put extra variables for the data as you did. With such short function you really are not having any benefits sinds all data is only accessed once and seem like unnecessary to me, though opinions could vary.
Here is my code -
$updatecompany = DB::table('Companies')
->where('ID', (int)$companyid)
->update(array(
'CompanyName' => $companyname,
'CompanyAddress' => $companyaddress,
'CompanyEmail' => $companyemail,
'ContactName' => $contactname,
'CompanyCity' => $companycity,
'CompanyState' => $companystate,
'CompanyZip' => $companyzipcode,
'CompanyPhone' => $companyphone,
));
$updatecompany is always 0. What might be the problem?
One of most possible reasons is that you are updating with the same data in the database.
There needs one out of the box solution, of course if you can do it.
So, no rows are updating, even if the SQL is correct.
Here is my suggestion:
Add a new column updatedOn in DB Table Companies.
The type should be TIMESTAMP and add attribute ON UPDATE CURRENT_TIMESTAMP.
This way you will always get row affected and hence you get return value other than 0.
You don't need to cast $companyId to an integer there. It does not help Laravel's query builder.
Use dd($companyId) and dump the variable before you run the query and find out what it is.
i use Active record for inserting values to DB.
All other type of queries i do with custom query as it's much easier but the active record insert is quite nice.
So i have this code :
$comment = array (
'point_id' => $object->id,
'title' => $object->title,
'comment' => $object->comment,
'author_name' => $object->author_name,
'is_temp' => 0,
'parent_id' => $object->parent_id
);
return $this->db->insert('comments', $comment);
Now i want to be able to set is_temp as a subquery result, which is :
(SELECT allow_user_comments from subjects where id='somevalue')
How would one achive that?
I was hoping to avoid using third party libraries.
Well, i doubt the fact that that's how you're supposed to do it, but ain't CI all about that?
This is how i got it to work (removing is_temp from the $comment array ofcourse):
$this->db->set($comment);
$this->db->set('is_temp',
'(SELECT allow_user_comments from subjects where id='.$subject_id.')',FALSE);
$this->db->insert('comments');
Feel free to use https://github.com/NTICompass/CodeIgniter-Subqueries. I have used it and it works! Hope it would be useful. :-)