query();$qry="UPDATE `jos_vodes_campaign_balance` SET credits_used=credits_used+1,balance=balance-1 WHERE userid='{$user->id}' and campaign_id in(select id from `jos_vodes_campaigns` WHERE published=1);";$database->setQuery($qry);$database->query();?>The thing is it doesn't update either of the tables that it should. Am I missing something that will make the queries happen, maybe a setting somewhere else in Chronoforms, or have I just got the code wrong?Thanks"> Updating tables onSubmit - Forums

Forums

Updating tables onSubmit

bigcrags 30 Jul, 2009
Hi
I'm using Chronoforms to email me competition entries but I require it to update another 2 tables in the database and subtract a credit from a users account when the form is submitted.
I've put this code in the onSubmit form code box:
<?php
echo ("Thank You For Your Submission");
$database =& JFactory::getDBO();
$user =& JFactory::getUser();
$database->setQuery( "UPDATE jos_vodes_credits SET credit=credit-1 WHERE userid='{$user->id}'" );
$database->query();
$qry="UPDATE `jos_vodes_campaign_balance` SET credits_used=credits_used+1,balance=balance-1 WHERE userid='{$user->id}' and campaign_id in(select id from `jos_vodes_campaigns` WHERE published=1);";
$database->setQuery($qry);
$database->query();
?>

The thing is it doesn't update either of the tables that it should. Am I missing something that will make the queries happen, maybe a setting somewhere else in Chronoforms, or have I just got the code wrong?
Thanks
GreyHead 30 Jul, 2009
Hi bigcrags,

It ought to work OK, I'm not sure about the {} syntax here and I suspect that the SQL may need quoting.

Try
$query = "UPDATE `#__vodes_credits` SET `credit` = `credit`-1 WHERE `userid` = '".$user->id."'; ";
echo '<div>$query</div';
$database->setQuery($query);
. . .
The echo statement should output the SQL so that you can see exactly what the generated query is and, if necessary, cut and paste it into PHPMyAdmin for testing.

Bob
bigcrags 30 Jul, 2009
Thanks for your advice Bob - that echoes a good query which works when I paste it into phpmyadmin and I change the tablename #__vodes_credits back to jos_vodes_credits. However it doesn't seem to update the table when Chronoforms runs it.
GreyHead 30 Jul, 2009
Hi bigcrags,

Plese turn Site Debug on in Global Configuration and submit the form. You should get a long list of SQL queries at the foot of the page please look for these queries and see if there are any clues there.

Usually SQL queries run OK from ChronoForms if the SQL is well-formed.

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