Update record

rosedum 20 Nov, 2010
Hello,

I have been trying a whole day to make it possible to update records in the database.
I have been searching the forum and found very valuable information and code.
However, I do not succeed in making it work by using this code.

Currently, I succeed in making a settings form and I let it fill up with the information that is already stored in a database called jos_settingen. However, when a user changes information and clicks on submit, the record is not being updated but a new record is added.

As you can see in the code below (It contains the php code that I paste in the FORM CODE HTML), I have been trying lots of stuff. I'm currently using loadObject instead of loadResult() because that didn't work out. However, loadobject() does not work either.

If I fill in cf_id=2 for example, he perfectly updates it, but as soon as I refer to $result, he fails in updating and justs inserts.

I can't figure out what's going wrong here, could somebody help me please?

<?php 
$Name = $_POST["name"];
$Position = $_POST["position"];
$department = $_POST["department"];
$account_nbr = $_POST["account_nbr"];
$password = $_POST["password"];
$ver_password = $_POST["ver_password"];
$email = $_POST["email"];
$cf_id = $_POST["cf_id"];

if ( !$mainframe->isSite() ) { return; }
$db =& JFactory::getDBO();
$user = JFactory::getUser();
$query = "
  SELECT 'cf_id', 'cf_user_id'
    FROM '#__settingen' 
    WHERE 'cf_user_id' = ".$user->id.";
";
$db->setQuery($query);
$result=NULL;
$db->loadObject($result);
if ( $result->cf_id ) {
  echo "<input type='hidden' name='cf_id' id='cf_id' value='.$result->cf_id.' />";
}
?>

<input type='hidden' name='cf_id' id='cf_id' value='<?php echo $result->cf_id?>' />
GreyHead 20 Nov, 2010
Hi rosedum,

The ChronoForms DB Connection will do most of this for you.

If your form data includes a value for the table primary key (often cf_id) then if the value already exists the record will be updated, otherwise a new record will be created.

Bob
rosedum 20 Nov, 2010
Hello Bob,

Well, that's exactly what he's not doing. If I log in with a user that has already a record in the jos_settings table and that user makes some changes through my form, a new record adds instead of the old existing record being updated.

He succesfully finds the user_id from the user table, but probably something goes wrong in querying for the cf_id which is later on being used.

So do you think or see a problem is situated in my query or in the call of 'cf_id' afterwards? Or somewhere else?

I'm quite desperate after 12H of searching for a solution...
GreyHead 21 Nov, 2010
Hi rosedum,

There's a little mistake in the syntax here
$db->setQuery($query);
$result=NULL;
$db->loadObject($result);
this needs to be
$db->setQuery($query);
$result = $db->loadObject();

Bob
rosedum 23 Nov, 2010
Hi Bob,

Thanks a lot for your answer! I've changed the mistake into the code you proposed, but unfortunately, it's still not working.😟

If I fill in cf_id='2', he's updating perfectly, but once I refer to the cf_id that I get from the jos_settingen table through the query, he adds a new record.

I still have got no clue about what's going wrong here...I suppose it's a query problem, but I have no idea what's wrong...

Is there another way to update records?

Could you help me again please?

Thanks,

Rosedum
GreyHead 23 Nov, 2010
Hi rosedum,

Not sure if this is your current query but the quotes here are incorrect. You need to use backticks `` (or no quotes) for column and table names. So this
$query = "
  SELECT 'cf_id', 'cf_user_id'
    FROM '#__settingen'
    WHERE 'cf_user_id' = ".$user->id.";
";
should be
$query = "
  SELECT `cf_id`, `cf_user_id`
    FROM `#__settingen`
    WHERE `cf_user_id` = ".$user->id.";
";

Bob
rosedum 23 Nov, 2010
Thanks a lot Bob!

It finally works!

Thank you!
This topic is locked and no more replies can be posted.