0

I have a database table (furcodes) with fields A, B, C, D, E i use this code to create a redeem code system in a Joomla site:

    <?php
$redeem_code = JRequest::getString('redeem_code', '', 'post');
$db =& JFactory::getDBO();
$query = "
    SELECT COUNT(`B`)
        FROM `furcodes`
        WHERE `B` = '$redeem_code' ;
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count ) {

  $mainframe =& JFactory::getApplication();
  $mainframe->enqueuemessage('You have succesfully redeemed this fur code');
  return false;
} else {
    $mainframe =& JFactory::getApplication();
  $mainframe->enqueueMessage(JText::_('Invalid code. Please check and try again.'), 'error');
  return false;
}
?>

I use the count to check if this code exists in the database but i also need to get the field from column "E" that states if this code has been used before or not (values 0=not used or 1=used , one use for every code) and create an if like this:

    if $used == 1
 echo "This code has been used"

Any help?


Final code i used, it might help someone else.

<?php
$your_fur_code = JRequest::getString('fur_code', '', 'post');
$db =& JFactory::getDBO();
$query = "SELECT `E` FROM `furcodes` WHERE `B` = '$your_fur_code'";
$db->setQuery($query);
$code_used = $db->loadResult();
if ( $code_used == "1" ) {
  $mainframe =& JFactory::getApplication();
  $mainframe->enqueueMessage(JText::_('The code has already been used.'), 'error');
  return false;
} if ( $code_used == "0" ) {
  $mainframe =& JFactory::getApplication();
  $mainframe->enqueuemessage('You have succesfully redeemed this code');
  return false;
} else {
    $mainframe =& JFactory::getApplication();
  $mainframe->enqueueMessage(JText::_('Invalid code. Please check and try again.'), 'error');
  return false;
}
?>

2 Answers 2

3

There is no need to check for the count at all, just do a regular select for column E

SELECT `E` FROM `furcodes` WHERE `B` = '$redeem_code'

If no rows are returned, you know the code didn't exist in table. If a row is returned, you can then check the returned value of E to see the status.

Sign up to request clarification or add additional context in comments.

Comments

0

Change

$query = "SELECT COUNT(`B`) FROM `furcodes` WHERE `B` = '$redeem_code' ;";

TO

$query = "SELECT COUNT(`B`) FROM `furcodes` WHERE `B` = '$redeem_code'";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.