【问题标题】:Won't this mysql line call the result?这个mysql行不会调用结果吗?
【发布时间】:2019-02-17 05:02:55
【问题描述】:

我创建了一个数据库,例如,您已经使用了代码,它将代码添加到其中,列是“promocodex”和“idused”。当我运行我的 PHP 代码时,它不会返回我想要的结果,不,我需要。

我已经尝试获取结果,我尝试计算受影响的行数,但它就是没有让步。我在想我用错了 SELECT 函数,但是当我看到它时,并没有太大的问题。

<?php
$userid = $_SESSION["userid"];

if(isset($_POST['Submit'])) {    
    $ckrs=mysql_query("SELECT 'promocodex' FROM `oto_usedpromocodes` WHERE 'idused' ='".$userid."'") or die(mysql_error());
    $recordcode = mysqli_fetch_array($ckrs);

    if (mysqli_num_rows($recordcode) < 0) {
        $msg = "You've already used this promo code!"; 
    }
    else {
        $msg = "It worked!"; 
    } 
}
else {
    $msg = "Something's wrong!"; 
}
?>

表格是这样的:

"<table width="30%" align="center" cellspacing="0" cellpadding="0" style=" border: solid !important; border-radius:5px; background-color:rgb(70, 72, 76); ">
  <tr>
    <td><p>&nbsp;</p><center><font color="red"><strong><?=$clean;?></strong></center></font><p>&nbsp;</p></td>
  </tr>
  <tr>
    <td>
    <form action="promocode.php" method="post" name="promocode" id="promocode">
    <table width="500" align="center" cellpadding="4" cellspacing="0" style="margin-top:-50px;">
      <?php
      if(isset($_POST['Submit']))
      { ?>
        <tr>
          <td  colspan="4" align="center">
            <font color="red"><strong><?=$msg;?></strong></font></td>
        </tr>
    <?
      }    
    ?>

    <tr>
        <td colspan="7" nowrap="nowrap" class="admintd" align="center"><font face="Verdana, Arial, Helvetica, sans-serif"><span><strong>Promo Code<br><br></strong></span></font> </td>
        </tr>

    <tr>
                <td colspan="1" class="formfield" align="center">
                    <input name="promocode" type="text" class="footer-text" value="" width="25" padding="0" />
                </td>
    </tr>
    <tr>
        <td colspan="4" align="center" nowrap="nowrap"><input name="Submit" type="submit" class="footer-text" value="Create" /></td>
        </tr>
        </table>
    </form>
    </td>
  </tr>

</table>"

我已经在数据库中放入了一个代码,所以应该出现的结果是“你已经使用了这个促销代码!”但出现的是“它成功了!”

我真正想要发生的是相同的用户 ID 不能一次又一次地使用相同的代码。

【问题讨论】:

  • 假设查询有效(意味着 $userid 不为空或无效)...尝试输出以下值:$recordcode
  • 我尝试为 $userid 设置一个实际值,它仍然输出相同的结果..
  • @JanCabrera 检查我更新的代码。

标签: php html mysql sql forms


【解决方案1】:

首先,您没有在对mysql_querymysql_error 的调用中包含数据库连接,实际上应该是mysqli_querymysqli_error。将该行更改为此(假设您的连接称为$conn):

$ckrs=mysqli_query($conn, "SELECT 'promocodex' FROM `oto_usedpromocodes` WHERE 'idused' ='".$userid."'") or die(mysqli_error($conn));

其次,您在数组 ($recordcode) 上调用 mysqli_num_rows,而不是在查询的结果集 ($ckrs) 上调用。您还比较应该大于的小于 0。您需要更改此行:

if (mysqli_num_rows($recordcode) < 0) {

到任一

if (mysqli_num_rows($ckrs) > 0) {

if (!empty($recordcode)) {

【讨论】:

    【解决方案2】:

    不要同时使用mysql和mysqli。

    mysqli_query($con,$sql)。它需要两个参数。

    $sql = "SELECT promocodex FROM oto_usedpromocodes WHERE idused ='$userid'";
    $ckrs = mysqli_query($con,$sql); // where $con is your connection string. 
    $recordcode = mysqli_fetch_array($ckrs);
    

    试试这个应该没问题。

    【讨论】:

      【解决方案3】:

      首先是这个:

      $ckrs=mysql_query("S ... 
      

      会更好:

      $ckrs=mysqli_query("S ...
      

      这是因为 mysql_* 已被弃用。

      下一步:mysqli_fetch_array($ckrs); 将下一行作为数组返回, 所以mysqli_num_rows($recordcode) 在这种情况下可能会返回零。

      在这种情况下,计数通常是可行的方法,但您可能会遇到一个被捕获的错误。您可以通过以下方式显示错误:mysqli_error($conn)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-17
        • 2015-06-20
        • 2016-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多