[SOLVED] Update database in Custom Code

How to update a database record in ChronoForms custom code.

Overview

The issue occurred because the UPDATE query was defined but not executed.
To resolve this, ensure you call the setQuery and query methods on the database object after defining the UPDATE statement.

Answered
Ga Gatsman 14 Jan, 2013
Good evening,
I am building a code redemption system with chronoforms.
I have a table (redeemcodes) with fields A=ID, B=CODES, C=DATE, D, E=USED OR NOT (0 or 1)
User will insert code, code will be checked if exists in the DB and then marked as used (1).
I use this Custom Code for the first 2 steps but can't get the last one 😟

<?php
$your_code = JRequest::getString('_code', '', 'post');
$db =& JFactory::getDBO();
$query = "SELECT `E` FROM `redeemcodes` WHERE `B` = '$your_code'";
$db->setQuery($query);
$code_used = $db->loadResult();
// if value on E=1 the redeem code has been found in DB but it's already used
if ( $code_used == "1" ) {
	$mainframe =& JFactory::getApplication();
	$mainframe->enqueueMessage(JText::_('The code has already been used.'), 'error');
	return false;
// if value on E=0 the redeem code has been found in DB, will be redeemed and must change E=1
} if ( $code_used == "0" ) {

// START change the reedem's code field E to "1"
$used = "1";
$query = "UPDATE `redeemcodes` SET `E` = '$used' WHERE `B` = '$your_code'"; 
// END change the reedem's code field E to "1"

	$mainframe =& JFactory::getApplication();
	$mainframe->enqueuemessage('You have succesfully redeemed this code.');
	return false;
// if no value on E has been returned this code does not exist in DB
} else {
	$mainframe =& JFactory::getApplication();
	$mainframe->enqueueMessage(JText::_('Invalid code. Please check and try again.'), 'error');
	return false;
}
?>


Any help on how to make this UPDATE work?
Gr GreyHead 14 Jan, 2013
Answer
Hi gatsman,

It looks like you aren't executing the second query:
. . .
$query = "UPDATE `redeemcodes` SET `E` = '$used' WHERE `B` = '$your_code'"; 
$db->setQuery($query); // add this line
$db->query();          // and this line
. .  .

Bob
Ga Gatsman 14 Jan, 2013
Thank you Bob, worked like a charm... 😀
This topic is locked and no more replies can be posted.