help needed with update_record

help needed with update_record

av Frederic Nevers -
Antal svar: 13
Hi,

I am trying to use the 'update_record()' function but have been unsuccessful so far. Looking at the documentation I understand (I think leende )that I need to create an object so that I can call it in the function, but that is the part I am struggling with. I have looked at examples of code with update_record but I still cannot get my head around it.

Here is what I have so far:

$uniqueattendance = new object();
$uniqueattendance ->id = $anabsentee->number; //This is where the unique ID is stored
update_record(mytable, $uniqueattendance);

I only want to update 2 fields in my record. How do I add that to my object?

I'd really appreciate any help that might send me in the right direction.

Cheers,
Fred

Genomsnitt av betyg: -
Som svar till Frederic Nevers

Re: help needed with update_record

av Tim Hunt -
Bild på Core developers Bild på Documentation writers Bild på Particularly helpful Moodlers Bild på Peer reviewers Bild på Plugin developers
Search the code for other places where update_record is called. They will show you what to do.
Som svar till Frederic Nevers

Re: help needed with update_record

av Paul Holden -
Bild på Core developers Bild på Moodle HQ Bild på Moodle Workplace team Bild på Particularly helpful Moodlers Bild på Peer reviewers Bild på Plugin developers Bild på Testers
The code you've posted doesn't look like it will do anything. The id field should just be an auto-incremented primary key, so shouldn't ever need to be updated.

Generally, it would look something like the following, assuming $record has been populated somewhere:

$record->field1 = 'new value';
$record->field2 = 22;
update_record('mytable', $record);
Som svar till Frederic Nevers

Re: help needed with update_record

av Shane Elliott -
Bild på Core developers Bild på Plugin developers
Hi Fred,

Something like the following should do the trick:

$uniqueattendance = new stdclass;
$uniqueattendance->id = $anabsentee->number;
$uniqueattendance->field1 = 'some data';
$uniqueattendance->field2 = 'something else';
if (update_record('mytable', $uniqueattendance)) {
 /// Success!
} else {
 /// Fail!
}

Paul is correct that id is usually the auto-indexing primary key on moodle tables, but update_recrod expects it so it can match up the record to be updated.

Generally data pulled using one of the get_record functions is in a format that can be submitted directly back into update_record so you could always do something like:

if ($uniqueattendance = get_record('mytable', 'id', $anabsentee->number)) {
 $uniqueattendance->field1 = 'new data';
 update_record('mytable', $uniqueattendance);
}

Hope that helps.

Cheers,
Shane.
Som svar till Shane Elliott

Re: help needed with update_record

av Tim Hunt -
Bild på Core developers Bild på Documentation writers Bild på Particularly helpful Moodlers Bild på Peer reviewers Bild på Plugin developers
"Generally data pulled using one of the get_record functions is in a format that can be submitted directly back into update_record"

This is dangerously wrong. Suppose one of the fields contains a single quote character.

It is always safer to create a new object with just the id and the fields you want to change.

If you want to do a get_records ... update record, you must do an addslashes in-between. (Or just wait until Moodle 2.0 when the whole addslashes nightmare is behind us.)
Som svar till Shane Elliott

Re: help needed with update_record

av Frederic Nevers -
Thank you all very much for your help. I have managed to get it to work, finally leende

Cheers,
Fred

Som svar till Frederic Nevers

Re: help needed with update_record

av Sathya Dinesh -

Hi all,

I have tried all these...but nothing works....

Kindly help me out to update a particular record of the user table...

Thanks in advance...

 global $DB; $uniquelevel = new stdclass;

$lcSelectedid = $_GET['stuid'];

$unilevel = intVal($_POST['level_select']);

 if ($uniquelevel =$DB->get_record('{user}', 'id', $lcSelectedid ))

{ $uniquelevel->level = $unilevel;

$DB->update_record('mdl_user',$uniquelevel);

}

2.

$data = new stdClass;

$lcSelectedValue = intVal($_POST['level_select']);

$lid=$_GET['stuid'];

$data->level = $lcSelectedValue;

//$ulevel=$DB->update_record_sql('UPDATE mdl_user SET level = $lcSelectedValue WHERE id=?',$lid);

$res = $this->datalib->update_record('mdl_user', $data);

 

3.$record = new object();

$record->level = $lcSelectedValue;

if($record->id == $lid)

{ echo $id = $DB->insert_record('mdl_user', $record); }

echo "Level Updated Successfully!!!";

 

4.

$record = new stdClass();

$record->id=$stuval->id;

$ulevel=$DB->update_record(mdl_user, $record, $bulk=false);

5.$ulevel=$DB->update_record('UPDATE {user} SET level = $lcSelectedValue WHERE id=?',array($stuval->id));

 The passing and getting of the value from other part of the code is successful..I have checked that...but cant update the record....

Please help me at the earliest...

Thanks,

Sathya

Som svar till Sathya Dinesh

Re: help needed with update_record

av Davo Smith -
Bild på Core developers Bild på Particularly helpful Moodlers Bild på Peer reviewers Bild på Plugin developers
Assuming Moodle 2.x:
$DB->update_record('user', $userdetails);

Don't include the table prefix. The format of insert_record is the same. (Please note there are hundreds of working examples in the Moodle source code, any of which would show the right way of doing this).
Som svar till Davo Smith

Re: help needed with update_record

av Sathya Dinesh -

Hi  Davo Smith,

Thanks for your reply....

It worked well with the following code....

global $DB;
$uniquelevel = new stdclass;
$lcSelectedid = $_GET['stuid'];

$levl = intVal($_POST['level_select']);

$sql=$DB->get_record_sql("Update {user} set level=$levl where id= $lcSelectedid");

Now im going to try to update multiple values from multiple selection box to a field in table.

Thanks,

Sathya.

Som svar till Sathya Dinesh

Re: help needed with update_record

av Davo Smith -
Bild på Core developers Bild på Particularly helpful Moodlers Bild på Peer reviewers Bild på Plugin developers

I think you need to have another look at the code, it might work, but is wide open to a number of security flaws that Moodle tries its best to protect you from, try this instead:

global $DB;
$upduser = new stdClass();
$upduser->id = required_param('stuid', PARAM_INT);
$upduser->level = required_param('level_select', PARAM_INT);
$DB->update_record('user', $uniquelevel); 

Even better would be to add a check, just before the last line, that the student exists:

if ($DB->record_exists('user', array('id' => $upduser->id))) {

You should never be calling 'get_record_sql' to update the database ('execute' can be used in the rare occassions where there really is no alternative). Even if you are going to use 'get_record_sql' for some purpose (i.e. to get records, not update them), then remember to use the following syntax:

$result = $DB->get_record_sql("SELECT * FROM {user} WHERE level = ?", array($userid));

As this format will clean all the paramaters for you (although in this example, you would actually use '$DB->get_record('user', array('id' => $userid));')

 

Som svar till Davo Smith

Re: help needed with update_record

av Anne Krijger -

Hi Dave,

I think there are two typoos in your example;

1) $uniquelevel is never defined. I assume this should be $upduser;
$DB->update_record('user', $upduser);

2) You use $userid as level, so this line should be;
$result
= $DB->get_record_sql("SELECT * FROM {user} WHERE level = ?", array($upduser->level));

BTW personally I prefer named parameters;
$result = $DB->get_record_sql("SELECT * FROM {user} WHERE level = :level", array('level'=>$upduser->level));

Anne.

Note to other readers; the way to handle DB has changed since 2.x, so the top of this thread my no longer apply.

Som svar till Davo Smith

Re: help needed with update_record

av Sathya Dinesh -

Hi Dave & Anne,

Thank You for your reply....

Reply is more understandable and make to learn better...Thanks again...

<code> global $DB;
$upduser = new stdClass();
$lcSelectedid = $_GET['stuid'];
$levls = $_POST['level_select'];
$upduser->id = required_param('lcSelectedid', PARAM_INT);
$upduser->level = required_param('levls', PARAM_INT);
if ($DB->record_exists('user', array('id' => $upduser->id))) {
$DB->update_record('user', $upduser);
}  </code>

Here im passing the value stuid & level_select from one file to other,so i have altered the code like this,here it is not taking lcSelectedid value....

"A required parameter (lcSelectedid) was missing"

And now im trying to insert multiple values into the table....when only the first value is inserted....Is there an option for it?

<codeglobal $DB;
$upduser = new stdClass();
$lcSelectedid = $_GET['stuid'];
$levls = $_POST['level_select'];

foreach($levls as $levl)
{
$upduser->teacherid = required_param("lcSelectedid", PARAM_INT);
$upduser->level = required_param('levls', PARAM_INT);
if ($DB->record_exists('teacher_level', array('teacherid' => $upduser->teacherid)))
{

//Delete Query should be written later once this works fine here

}
else{
$DB->insert_record('teacher_level', $upduser);
}

 "A required parameter (lcSelectedid) was missing"--Its not taking the value passed....

 

Thanks in advance.....

Sathya

Som svar till Sathya Dinesh

Re: help needed with update_record

av Davo Smith -
Bild på Core developers Bild på Particularly helpful Moodlers Bild på Peer reviewers Bild på Plugin developers

global $DB;
$upduser = new stdClass();
$lcSelectedid = $_GET['stuid'];
$lcSelectedid = required_param('studid', PARAM_INT);
$levls = $_POST['level_select'];
$levls = required_param('level_select', PARAM_INT);
$upduser->id = $lcSelectedid;
$upduser->level = $levls;
if ($DB->record_exists('user', array('id' => $upduser->id))) {
$DB->update_record('user', $upduser); 

Although, there is really no reason to get the data via required_param and then copy it into another variable, the code I originally suggested would work just as well:

$upduser->id = required_param('studid', PARAM_INT);