insert and update at the same time with codeigniter - php

I have another problem again on querying in CodeIgniter,
I'll try to insert and update a "comment" on my website, but it's not working yet.
Here's my code :
on the Models (news_model.php)
public function simpan_komentar() {
$data = array(
'noid' => $_POST['noid'],
'kategori' => $_POST['kategori'],
'nama' => $_POST['nama'],
'pekerjaan' => $_POST['pekerjaan'],
'detail' => $_POST['detail'],
'created_at' => date('Y-m-d h:i:s'),
);
$data1 = array(
'komentar' => 'komentar + 1',
);
$this->db->insert('komentar',$data);
$this->db->update('news',$data1);
}
The insert query is working, but the update query didn't work
Can you tell me where's my fault?
Thanks

Try this
$this->db->set('komentar', 'komentar+1', FALSE);
//add your where condition if any
$this->db->update('news');

Related

How to insert new data into exists row without replace exists data in laravel?

Here I used updateOrCreate method to post data but when I use this method old data replaced by new data but I want to add new data without updating or replacing exists data.
here is the code for insert data
$booking = Bookings::updateOrCreate(
['schedules_id' => $schedules_id], // match the row based on this array
[ // update this columns
'buses_id' => $buses_id,
'routes_id' => $routes_id,
'seat' => json_encode($seat),
'price' => $request->price,
'profile' => 'pending',
]
);
I solved myself, I stored old data into $extSeat then merge with new data, I don't know the logic is correct or wrong but works
$extSeat = DB::table('bookings')->select('seat')->first();
$extSeat = explode(",", $extSeat->seat);
$booking = Bookings::updateOrCreate(
['schedules_id' => $schedules_id],// row to test if schedule id matches existing schedule id
[ // update this columns
'buses_id' => $buses_id,
'routes_id' => $routes_id,
'seat' => implode(",", array_merge($seat,$extSeat )),
'price' => $request->price,
'profile' => 'pending',
]);

Cakephp 2 update multiple records

i'm newbie in cakephp and i trying to update multiple rows in one transaction like:
$Model->saveMany($data, array('deep' => true));
... And the structure of the $data array is:
$data = array(
(int) 1 => array( 'Item' => array('id' => 2, 'name' => 'Name 1') ),
(int) 2 => array( 'Item' => array('id' => 3, 'name' => 'Name 2') ),
);
I Already tried with saveAll instruction and without deep parameter but nothing :( .... what's wrong?
Thanks for the help :)
The problem was that it had a required field in the validation that, although it was not compromised in the update, anyway it had to be passed in command
Thank you all!!
You can use this following code to insert data in Cake Php,
$this->request->data = Hash::insert($this->request->data);

wordpress wpdb->update with post/request method NOT WORKING

I have to update records in database, but it's not working. Any ideas why?
$wpdb->update($table_name, array( 'value' => $request['promopunk_name']),"WHERE = 'name'");
change your query like.
$wpdb->update($table_name, array( 'value' => $request['promopunk_name']),array("name" => 'value'));
i hop this is used-full for you.

How to insert data from a single form to multiple tables in codeigniter framework ? - below method is not working

Anyone know how to insert data from a single form to multi tables in codeigniter
i tried below method but it is not working
Model
function add_models(){
$data1 = array(
'companykeyid' => $this->input->post('ckeyid'),
'name' => $this->input->post('name'),
'age' => $this->input->post('age'),
);
$data2 = array(
'companykeyid' => $this->input->post('ckeyid'),
'phrase' => $this->input->post('phrase'),
'medialength' => $this->input->post('medialength'),
);
$data3 = array(
'companykeyid' => $this->input->post('ckeyid'),
'phrase' => $this->input->post('phrase')
'medialength' => $this->input->post('medialength'),
);
$this->db->insert('girls', $data1);
$this->db->insert('movies',$data2);
$this->db->insert('keywords',$data3);
}
Method you made is, actually working, i think that your problem is syntax error you have (missing comma in data3 array).
So, this should work:
function add_models(){
$data1 = array(
'companykeyid' => $this->input->post('ckeyid'),
'name' => $this->input->post('name'),
'age' => $this->input->post('age')
);
$data2 = array(
'companykeyid' => $this->input->post('ckeyid'),
'phrase' => $this->input->post('phrase'),
'medialength' => $this->input->post('medialength')
);
$data3 = array(
'companykeyid' => $this->input->post('ckeyid'),
'phrase' => $this->input->post('phrase'),
'medialength' => $this->input->post('medialength')
);
$this->db->insert('girls', $data1);
$this->db->insert('movies',$data2);
$this->db->insert('keywords',$data3);
}
Important - you have to be sure that field names in tables are right... If this doesn't work, your problem is somewhere else in code (check your controller, view...)
In the array, you call the last value don't have "," get read of it and your model working correctly.
$data1 = array(
'companykeyid' => $this->input->post('ckeyid'),
'name' => $this->input->post('name'),
'age' => $this->input->post('age') <-----
);
In sinisake's answer shouldn't:
function add_models()
be chaged to:
function add_models($data)
Seems like there will be data to pass from the controller.

Renaming fields in every document in a MongoDB collection using PHP

Following this question I gather that upsert: false and multi: true fields need to be set for this to work.
However, when I try to code this in PHP, I have a problem:
$conn = new Mongo("mongodb://foo:bar#localhost:27017");
$db = $conn->selectDB("someDB");
$data = array('$rename' => array(
'nmae' => 'name'
));
$db->command(array(
'findAndModify' => 'foo',
'update' => $data,
'upsert' => 'false',
'multi' => 'true'
));
After running this script, only the first document with the nmae typo is changed to name; the rest still say nmae. The same as if I had run it without the upsert and multi options.
I also tried this:
$data = array('$rename' => array(
'nmae' => 'name'
),
'upsert' => 'false',
'multi' => 'true'
);
$db->command(array(
'findAndModify' => 'foo',
'update' => $data
));
But that does the same thing.
Any way to get this working?
The findAndModify query doesn't have a "multi" option:
http://www.php.net/manual/en/mongocollection.findandmodify.php
What you probably want to use is update instead:
http://www.php.net/manual/en/mongocollection.update.php

Categories