[SOLVED] Update database in Custom Code

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?
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
Gatsman 14 Jan, 2013
Thank you Bob, worked like a charm... 😀
This topic is locked and no more replies can be posted.