0

我有一个包含字段 A、B、C、D、E 的数据库表(furcodes),我使用此代码在 Joomla 站点中创建兑换代码系统:

    <?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;
}
?>

我使用计数来检查数据库中是否存在此代码,但我还需要从“E”列中获取字段,该字段说明此代码之前是否已使用(值 0=未使用或 1=已使用,一次使用对于每个代码)并创建一个这样的 if:

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

有什么帮助吗?


我使用的最终代码,它可能对其他人有帮助。

<?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;
}
?>
4

2 回答 2

3

根本不需要检查计数,只需对列 E 进行常规选择

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

如果没有返回任何行,您就知道该代码在表中不存在。如果返回一行,则可以检查 E 的返回值以查看状态。

于 2013-01-11T18:30:20.907 回答
0

改变

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

$query = "SELECT COUNT(`B`) FROM `furcodes` WHERE `B` = '$redeem_code'";
于 2013-01-11T18:27:44.310 回答