duanqun7761 2016-11-07 10:25
浏览 26
已采纳

检查表是否包含值将无效(SELECT COUNT)[重复]

This question already has an answer here:

I want to prevent user from registering an email address that is already set in my table. I am doing it like this:

$emailcheck = $bdd->prepare('SELECT COUNT(*) FROM ' . DB_TABLE . ' WHERE MATCH(email) AGAINST '.$_POST['email'].' ');
$emailcheck->execute();
$emailcheckrows = $emailcheck->fetch();

if ($emailcheckrows > 0) {
    $_SESSION['err_msg']="This email address is already registered";
    $error=true;
    $emailcheck->closeCursor();
}

But this doesn't work. I have already tried almost everything (also with LIKE, = and in-array). The "if" is not executed when I enter an already submitted email.

Any idea ? Thank you

</div>
  • 写回答

2条回答 默认 最新

  • dre26973 2016-11-07 10:48
    关注

    you can use it as an simple function like:

    class Validation {
        public static function emailUnique($conn, $email)
            {
                $sql = "SELECT email FROM formular WHERE email = '".$email."'";
                $emailUnique = $conn->query($sql);
                return (boolean) $emailUnique->num_rows;
            }
    }
    

    this returns a true if an entry has been found and false if not and then you can call your function in your script like this. i've used this together with bootstrap-alerts:

    $errorField = "";
    $labelClass = array(
     "emailUnique"=>"",
    );
    $email = mysqli_real_escape_string($conn, $_POST["email"]);
    $errorMessages["emailUnique"] = Validation::emailUnique($conn ,$email);
    
    $DisplayErrorForm = array();
    $hasErrors = false;
    $formErrorMessage = "";
    foreach ($labelClass as $key=>$value) {
              if($errorMessages[$key]){
              $labelClass[$key] = "has-error";
              $hasErrors = true;
    
              $DisplayErrorForm["emailUnique"] = array("style" => "red", "text" => "Email is already taken");
    
              if($key == "emailUnique"){
                   $formErrorMessage .= "<li style='" . $DisplayErrorForm["emailUnique"]["style"] . "'>" . $DisplayErrorForm["emailUnique"]["text"] . "</li>";
                 }
              }
           }
    
    if(count($DisplayErrorForm)) {
      $errorField = "<div class=\"alert alert-danger\">".
                                "<strong>Whoops!</strong> There were some problems with your input.<br><br>".
                                "<ul>".$formErrorMessage."</ul>".
                            "</div>";
            }
            if (!$hasErrors) {
            //Do the database input
    

    and then down in your html part call the $errorField

    <div>
    <?php echo $errorField; ?>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法