Forums

Update second table using custom code

rmesman 10 Feb, 2012
Hi,

I have a form where the data is being saved to a table using a `DB Save` element in the `On Submit` part of the form-wizard. Some data is also used to update a second table (a Community Builder table). I use a `Custom code` element with the following code:
<?php 
$fase = $_POST['fase'];
$id = $_POST['UserID'];
echo "Fase: ".$fase." ID: ".$id;
$db =& JFactory::getDBO();
$query = "UPDATE jpwjh_comprofiler SET cb_fase=".$fase." WHERE user_id=".$id."; ";
$db->setQuery($query);
?>

The $fase and $id variables are displayed fine but the table `jpwjh_comprofiler` is not being updated.

I've also tried using ` in the code (see below) but this also didn't help.
<?php 
$fase = $_POST['fase'];
$id = $_POST['UserID'];
echo "Fase: ".$fase." ID: ".$id;
$db =& JFactory::getDBO();
$query = "UPDATE `jpwjh_comprofiler` SET `cb_fase`=".$fase." WHERE `user_id`=".$id."; ";
$db->setQuery($query);
?>


What am I doing wrong or is there another way to update a second table?
Robin
GreyHead 10 Feb, 2012
Hi rmesman,

You aen't actually executing the query; and the values will need quoting unless they are integers:
. . .
$query = "
  UPDATE `#__comprofiler` 
    SET `cb_fase` = '{$fase}' 
    WHERE `user_id` = {$id}; 
";
$db->setQuery($query);
$db->query();
?>

Bob
rmesman 11 Feb, 2012
Thanks Bob,

This works!

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