Forums

Updating a value in database: custom code or DB Save?

boyjah 03 Mar, 2016
When a user submits a form I want, in addition to saving the contents to a database table (which it does), I also want to update a value in another table. Is this best done with a Custom Code or a DB Save?

I have tried both but cannot seem to get it right.

The sql for what I want to do is:
UPDATE `y2vfp_comprofiler` SET `cb_usertype` = 'w' WHERE `y2vfp_comprofiler`.`id` = (the user id);

I have tried variations on the following, both as custom code and as DB save, but always get an error.
<?php
// get current user
$user = JFactory::getUser();
UPDATE `y2vfp_comprofiler` SET cb_usertype = w WHERE `y2vfp_comprofiler.id` = $user;
?>
The error I get is always something along the lines of:
Parse error: syntax error, unexpected '`' in /home/wwoofjap/www/www/home/administrator/components/com_chronoforms5/chronoforms/actions/db_save/db_save.php(86) : eval()'d code on line 4

(it changes somewhat depending on what I have tried modifying in the php code)

Can someone advise me as to proper syntax and whether this is best suited to custom code or db save?
GreyHead 03 Mar, 2016
Hi boyjah,

You can't just stick a line of MySQL into your PHP code and expect it to work. You need to use something to send the MySQL to the database. There are hundreds of examples here.

In this case you could just use a DB Save action and set the Conditions box to set the WHERE clause. There are FAQs on getting the User ID and on setting the Conditions box.

Bob
boyjah 03 Mar, 2016
What is the advantage of using DB Save for this versus the Custom Code option?
GreyHead 04 Mar, 2016
Hi boyjah,

The DB Save action is simple to use and fine for most database saves. However - if you need to do something more complex than a simple save it can be simpler to write and run the MySQL query directly,

Bob
This topic is locked and no more replies can be posted.

VPS & Email Hosting 20% discount
hostinger