I want to update single row base on id, i have questions table and there is column question_id, which has unique date for each row.
Now i want to update that row column based on that question_id.
I have code written below, but not getting success. It created New Record not update old record. I want to update old record.
I have tried both ParseObject and ParseQuery but not getting success.
$query = new ParseObject("question");
$query->equalTo("question_id", "2");
$query->set("correct_percentage", "55.5");
$query->save();
You should load the object in question first, manipulate the data and then save it. Your current code, as you already noticed, simply creates new objects each time it is run.
// Fetch the question object where question_id == 2
$query = new ParseQuery("question");
$query->equalTo("question_id", "2");
$question = $query->first();
// .. optionally verify that $question has a value ...
// Manipulate the object and save it
$question->set("correct_percentage", "55.5");
$question->save();
Related
I have a problem with my Laravel crud application for Registrations.
There are these tables: Registration, ExtraRegistration (with a registration_id, extra_id and extraoptions_id),
Extra and ExtraOptions (with the Extra_id).
In the RegistrationController when i add a Registration it makes a new record in the ExtraRegistration with the extraoptions_id and the extra_id. the extra_id is the name of the option and the extraoptions_id is the id of the option you selected.
But now, when you click on edit a record, it shows all the information. the problem is that when you change the extraoption, it makes another record, and not change the select.
And when you have edited something and you look at it again, it still shows the option before you edited it.
RegistrationController
$options = Extra::where("exa_form_id", $distance->asd_form_id)->get();
foreach($options as $option){
$input_name = "option_" . $option->exa_id;
$input_option = $request->$input_name;
if(!is_null($input_option)){
$input_name_extra = "extraoptions_" . $option->exa_id;
$input_option_extra = $request->$input_name_extra;
$registrationextra = new ExtraRegistration();
$registrationextra->iea_registration_id = $registration->isg_id;
$registrationextra->iea_extra_id = $input_option;
$registrationextra->iea_extraoption_id = $input_option_extra;
$registrationextra->iea_price = $option->exa_price;
$registrationextra->save();
}
}
$registration->isg_options = $input_option;
$registration->isg_option_extra_id = $input_option_extra;
Edit:
I want a check before it makes a new ExtraRegistration. that if the price that is in the registration already exists in that registration_id(in ExtraRegistration) it doesnt make another record in the ExtraRegistration.
I want to use an if to fix the problem, but not sure where to begin..
Thanks in advance!
It will probably be easier for you if you just use firstOrCreate method that Laravel provides out of the box.
You can check it here:
https://laravel.com/docs/5.8/eloquent#other-creation-methods
That way, you will be able to retrieve the record that you are looking for, if there is any and then perform all the changes on it. If that record doesn't exist, Laravel will create the new record for you which you'll be able to update.
I hope this helps!
You are creating new object of ExtraRegistration() so its inserting/creating new record every time.
Just changes this line
$registrationextra = ExtraRegistration::find($id);//any id to fetch record
I'm developping a website where there are ajax request to get data from a mySQL database.
To simplify the problem, let's say I have the following database structure :
Table OBJECT
ID_OBJECT (int)
NAME_OBJECT (varchar)
Table IMAGE
ID_OBJECT (int) -> foreign key from OBJECT
URL (varchar)
So every object can have between 0 and n images attached to it.
I'm stuck at writing the php script that request the data.
To simplify again, let's say I want to get all objects with all the images they have (but in the future I'll want to get any specific object with all its picture too)
I have written this PHP script :
$sql = "select
ID_OBJECT,
NAME_OBJECT,
URL
from
OBJECT,
IMAGE
where
OBJECT.ID_OBJECT = IMAGE.ID_OBJECT";
$statement=$db->prepare($sql);
$statement->execute();
$result=$statement->fetchAll(PDO::FETCH_ASSOC);
header('Content-Type: application/json; charset=utf-8');
echo json_encode($result);
The problem I have is as soon as I add "URL", "IMAGE" and the where statement in the sql query, it doen"t echo anything.(I have several objects and images in my databases with the propers ids). I don't understand where I'm making a mistake here.
ID_OBJECT is in both tables, so MySQl doesn't know which one to show. Try specifying like this:
select
OBJECT.ID_OBJECT,
OBJECT.NAME_OBJECT,
IMAGE.URL
from
OBJECT,
IMAGE
where
OBJECT.ID_OBJECT = IMAGE.ID_OBJECT
Obviously you have an error in your query. You should set what ID_OBJECT field you want - from first or second table.
Rewrite your query like:
select
o.ID_OBJECT,
o,NAME_OBJECT,
i.URL
from
OBJECT o,
IMAGE i
where
o.ID_OBJECT = i.ID_OBJECT
I need to read all records from table agreements, make changes in filed payments, and update all records, save to table. So, my problem is, that save() only create empty record. Do not update exists record. I show you how:
Reading from table:
$agreements = $this->Agreement->find('all');
$payments = $this->Payment->find('all');
Manipulation on fields (part of)(example):
$id=0;
foreach ($agreements as $agreement):
for ($i=$first_agreement; $i<=$last_agreement; $i++){
if ( $agreement['Agreement']['agreement_number']==$i){
$agreements[$id]['Agreement']['payment']=$payd[$i];
}
}
$id++;
endforeach;
Writting to table:
$this->Agreement->save();
A echo debug($agreements) shows correct array, i have tryed also :
$this->Agreement->save($agreements);
or
$this->Agreement->save($this->request->data);
Can you help/explain me how to write all record?
Cake 2.5.2 PHP : 5.4.4-14
Model::save() only saves a single record. If you want to save multiple records you need to use Model::saveAll() or Model::saveMany().
I'm trying to use the Joomla framework to make a create in the main content table.[http://docs.joomla.org/How_to_use_the_JTable_class] This works fine except that some data comes from posted variables and some from logic that happens when a file is uploaded moments before (store the random image name of a jpg)
$data=&JRequest::get('post');
this takes a ref to the posted values and I want to add to this Array or Object my field. The code I have makes the new record but the column images, doesnt get my string inserted.
I am trying to do something like$data=&JRequest::get('post');
$newdata=(array)$data;
array_push($newdata,"images"=>"Dog");
i make newdata as data is a ref to the posted variables and i suspect wont there fore allow me to add values to $data.
I'm a flash guy normally not a php and my knowledge is letting me down here.
Thanks for any help
Right, first thing:
$data=&JRequest::get('post');
$data is an array, you do not have to cast it. To add another element to the array as described in the comments do this:
$data['images'] = 'cats';
If you are using normal SQL to do the insert then you would do something like this to get the last inserted id e.g. the id of the row you just inserted:
$db = $this->getDBO();
$query = 'Some sql';
$db->setQuery($query);
if (!$db->query()) {
JError::raiseWarning(100, 'Insert failed - '.$db->getErrorMsg());
}
$id = $db->insertid();
If you are developing in Joomla I suggest you use the db functions provided to you rather than mysql_insert_id();
[EDIT]
If you want to use store then you can get the last inserted id like so:
$row->bind($data);
$row->check();
$row->store();
$lastId = $row->id;
I'm using RedBean PHP to dump some data from a web scrape into a database, and I need to retain the legacy IDs, preferably as the primary key field. Is it possible to do this with RedBean?
When I try to set the id as so:
$bean->id = 56;
The row doesn't get inserted - the query that ends up being created instead becomes an "UPDATE WHERE id=56", which does nothing since the record doesn't exist yet.
I solved this by inserting a placeholder with an SQL exec first:
$id = 60000;
$name = 'bob';
R::exec('insert into person(id) values('.$id.')');
$person = R::dispense('person');
$person->id = $id;
$person->name = $name;
R::store($person);
Forcing a specific value on an auto-increment primary index is just asking for race-condition problems. I'd suggest you create a new column legacy_id instead where you save the alternative id.
I have started an redbean github project for this, first version supports to have custom id fields.
You have to tell redbean what the name of the id field is (if not "id")
R::getRedBean()->idFieldMap= array('adres'=>'id_adres','contact'=>'contact_id'); // settings for current database
Future improvements: support custom Foreign keys.
https://github.com/freelanceniek/redbeanx