PHP / MySQL会员系统总是给我电子邮件已经注册

时间:2014-12-26 10:49:03

标签: php mysql system member

会员系统代码总是将我重定向到"电子邮件已经注册的页面"
这是我在处理过程中使用的代码,用于检查电子邮件是否为用户名 已被采取或不采取 请帮忙!!!!

<?php
    $db_host = "localhost";
    $db_user = "ms_admin";
    $db_pass = "secretpassword";
    $db_name = "member_system";

     $connection = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

    if(mysqli_connect_errno())
        die("Databse connection failed." . mysqli_connect_error()
            . " (" . mysqli_connect_errno() . ")" );
?>

<?php

    function check_email($e_mail)
    {
        $query  = "SELECT email FROM members ";
        $query .= "WHERE email='$e_mail'";

        $result = mysqli_query($connection, $query);
        $num_rows = mysqli_num_rows($result);

        if($num_rows>0) header("Location: registration_successful.php");
         else header("Location: registration_unsuccessful.php");
    }
?>

<?php
    $full_name = ucwords($_POST["full_name"]);
    $email = strtolower($_POST["email"]);
    $password = md5($_POST["password"]);

    check_email($email);
?>

我改变了那些:

 if($num_rows>0) header("Location: registration_successful.php");
  else header("Location: registration_unsuccessful.php");

为:

 if($num_rows>0) header("Location: registration_unsuccessful.php");
  else header("Location: registration_successful.php");


似乎($ num_rows&gt; 0)总是返回FALSE :(

请帮忙,, 谢谢....

4 个答案:

答案 0 :(得分:2)

check_email()中缺少

$ connection变量请设置global $connection;以进行数据库连接,如

function check_email($e_mail){
        global $connection;
        $query  = "SELECT email FROM members ";
        $query .= "WHERE email='$e_mail'";

        $result = mysqli_query($connection, $query);
        $num_rows = mysqli_num_rows($result);

        if($num_rows>0) header("Location: registration_successful.php");
         else header("Location: registration_unsuccessful.php");
     }


     check_email($email);

OR

 function check_email($e_mail, $connection){
    $query  = "SELECT email FROM members ";
    $query .= "WHERE email='$e_mail'";

    $result = mysqli_query($connection, $query);
    $num_rows = mysqli_num_rows($result);

    if($num_rows>0) header("Location: registration_successful.php");
     else header("Location: registration_unsuccessful.php");
 }


 check_email($email, $connection);

虽然我喜欢第一个:)

答案 1 :(得分:0)

问题在于您重定向用户之前的测试:

// if the rows are > 0 then it means the email already exists 
// which means we should redirect to the unsuccessful registration page
if($num_rows>0) header("Location: registration_unsuccessful.php");
     else echo header("Location: registration_successful.php");
// else, we redirect to successful registration page (meaning the email does not exists in DB)

正如@Mihai所提到的,你忘了连接你的查询字符串了。

答案 2 :(得分:0)

  

尝试以下代码

<?php

function check_email($e_mail)
{
    $query = "SELECT email FROM members";
    $query .= "WHERE email='$e_mail'";

    $result = mysqli_query($connection, $query);
    $num_rows = mysqli_num_rows($result);

    if($num_rows>0) header("Location: registration_unsuccessful.php");
     else echo header("Location: registration_successful.php")
}
?>

<?php
$full_name = ucwords($_POST["full_name"]);
$email = strtolower($_POST["email"]);
$password = md5($_POST["password"]);

check_email($email);
?>

答案 3 :(得分:0)

使用

    $query  = "SELECT email FROM members ";
    $query .= "WHERE email='".$e_mail."'";

您在会员后错过了Space,将其复制粘贴到您的代码中。

或使用简单的解决方案

    $query  = "SELECT email FROM members WHERE email='".$e_mail."'";

更新

mysqli_store_result($connection);
$num_rows= mysqli_num_rows($user_query);
if($num_rows>0) 

原因 - &gt; 使用store_result缓冲结果。