【问题标题】:Check the value in db and if it is already set to 1 it will trigger the error message检查 db 中的值,如果它已经设置为 1,它将触发错误消息
【发布时间】:2019-10-02 15:47:44
【问题描述】:

我的数据库

DBName:用户,表名:引用 列:

taken, email, inviteCode

我不知道什么是正确的代码来检查所取数据的值是否是“1”表示已使用“0”表示未使用如果我输入已占用列中已为“1”的邀请码将触发“已使用代码”消息。

注册.php

if(isset($_POST['Register'])){
    $inviteCode = ($_POST['inviteCode']);
    if (empty($_POST['inviteCode'])) {
        header("Location: index.php/0");
        exit();
    } 
    $rs_check = mysqli_query("SELECT * from referrals 
                            WHERE inviteCode='$invite_code' 
                            AND `taken` = '0'"); 
    $num = mysqli_num_rows($rs_check);
    // Match row found with more than 0 results  - the code is invalid. 
    if ( $num <= 0) { 
        $err7 = "Invite code is invalid";
    } else {
        $inviteCode = $_POST['inviteCode'];
    }
    if(empty($err7)) {
        $rs_check_update = mysqli_query($conn, "UPDATE referrals SET taken='1' WHERE inviteCode='$inviteCode'");
        header("Location: index.php/1");
        exit;
    }
}

index.php

<?php
include ('register.php');
?>
    <form action="register.php" method="POST">  
       <div class="form-email">
       <p>   
<?php if(isset($_GET["result"]) && $_GET["result"]=="1") { ?>
        <p>Code Success</p>
<?php } ?>
        </p>
        <p>    
<?php if(isset($_GET["result"]) && $_GET["result"]=="2") { ?>
        <p>Code Already Used</p>
<?php } ?>
        </p>
        <p>    
<?php if(isset($_GET["result"]) && $_GET["result"]=="0") { ?>
        <p>Please Input A Code</p>
<?php } ?>
        </p>
        <input type="text" placeholder="Invite Code" name="inviteCode" />
        </div>
        <div class="submit3">
           <input type="submit" value="Invite" name="Register" />
        </div>
    </form>

如果代码已经更改为“1”,如果我提交两次,我仍然会收到消息“代码成功”

【问题讨论】:

  • 你在哪里定义$err7
  • @alex55132 我不知道 xD 我只是将这段代码复制到这里它仍然可以工作:D 我只是 php 中的新手
  • @RiggsFolly 它来自这里 stackoverflow.com/questions/14087281/… 我仍然不明白他的 $err7 是如何工作的 :(
  • 我的猜测是它不起作用。看起来您没有在您向我们展示的这段代码中的任何地方创建一个名为 $err7 的变量
  • @RiggsFolly 是对的,告诉我们你在哪里定义$err7。它必须在某个地方,因为它不会给出任何错误,不是吗?

标签: php html mysql mysqli


【解决方案1】:

如果我要更正 register.php,我会这样做:

if(isset($_POST['Register'])){
    if(!isset($_POST['inviteCode']) || $_POST['inviteCode'] == ''){
      // If inviteCode is not set or empty
      header('Location: index.php/0');
    } else {
      // Use mysqli_real_escape_string to prevent SQL injection.
      // Sending raw user input to your database is very dangerous.
      // Also, you don't need to assign a variable to the same thing multiple times.
      $invite_code = mysqli_real_escape_string($_POST['inviteCode']);
      $rs_query = 'SELECT * FROM `referrals` WHERE `inviteCode` = "'.$invite_code.'"';
      $res = mysqli_query($conn, $rs_query);
      $num = mysqli_num_rows($res);

      if($num <= 0){
        $err7 = 'Invite code is invalid.';
        // You probably want to do something -like printing it- with this error.
        // Just attaching it to a variable will display nothing to the user.
      } else if($num == 1){
        if($res->taken == 0){
          $rs_check_update = 'UPDATE `referrals` SET `taken` = 1 WHERE `inviteCode` = "'.$inviteCode.'"';
          if(mysqli_query($conn,$rs_check_update)){
            // If the update query is successful.
            header('Location: index.php/1');
          } else {
            // Error handling if the update query fails.
          }
        } else {
          // taken column in db is not 0
        }
      }
    }
}

【讨论】:

  • 感谢帮助我有一个问题,如果已获取列中的邀请码数据已设置为 1,我应该在哪里输入“已使用的代码”消息
  • @EllsowrthSanders 我已经相应地编辑了代码,如果您遇到错误或其他情况,请告诉我,我还没有在服务器上测试过代码。
  • 它没有将取值更新为 1 但我重定向到 index.php/1
  • 如果查询失败,它不应该重定向到 index.php/1。这就是我们将 mysqli_query 放在 if 语句中的原因。确保查询对于您的数据库是正确的。
猜你喜欢
  • 1970-01-01
  • 2020-09-04
  • 2020-01-21
  • 2011-07-14
  • 2017-04-14
  • 1970-01-01
  • 2019-10-07
  • 1970-01-01
  • 2017-06-26
相关资源
最近更新 更多