简体   繁体   English

我不知道这段代码SQL有什么问题

[英]I don't know what's wrong with this code SQL

I'm new to PHP and Mysql, for some reason it only checks for statement if($email == $result2 ) wether the input is username or email. 我是PHP和Mysql的新手,由于某种原因,它仅检查是否输入用户名或电子邮件的语句if($ email == $ result2)。 I don't know why? 不知道为什么 can someone explain it logically, i'm stuck for hours figuring it out. 有人可以从逻辑上解释它,我被困了几个小时才弄清楚。 :( Thanks Please be kind. :(谢谢,请客气。

<?php 
session_start();
include_once("connect.php");

$email = $_POST['email'];
$username = $_POST['username'];
//echo $_POST['email'];



if(isset($_POST['email']) )
{
$extract= mysql_query("SELECT username, email FROM users");
$resultq = mysql_num_rows($extract);
while($row= mysql_fetch_array($extract))
{

    $result = $row['username'];
    $result2 = $row['email'];


    //$pass = $_POST['pass'];

    if($email == $result2 ) 
    { //check if there is already an entry for that username
        echo "Email Address is already used!";
        exit(); //break;
    }
    if ($username == $result )
    {
        echo " Username is already Taken!";
            //mysql_query("INSERT INTO users (Username, Password) VALUES ('$user', '$pass')");
            //header("location:index.php");
        exit(); //break;
    }
    else
    {

    }
}

} }

It's behaving as written. 它的行为如所写。 If either if() test succeeds, you tell the script to exit() . 如果任何一个if()测试都成功,则告诉脚本exit()

Remove the exit() calls... 删除exit()调用...

You also really REALLY need to learn about WHERE clauses in queries. 您还真的需要了解查询中的WHERE子句。 You are sucking across your entire user table and comparing the records one at a time. 您正在遍历整个用户表并一次比较一条记录。 This is the equivalent of driving to the grocery store, buying up the ENTIRE store's inventory, driving it home... then throwing it all in the garbage because all you really wanted was one candy bar. 这相当于开车去杂货店,买下整个商店的存货,开车回家……然后把它们全部扔进垃圾桶,因为您真正想要的只是一个糖果吧。

我认为您最好在电子邮件和用户名列中使用唯一性,然后您不再需要检查它,mysql将为您做到这一点!

try this 尝试这个

  if($email == $result2 ) 
{ //check if there is already an entry for that username
    echo "Email Address is already used!";
    //---------removed that line
}
else if ($username == $result )   //add else if instead of if
{
    echo " Username is already Taken!";
        //mysql_query("INSERT INTO users (Username, Password) VALUES ('$user', '$pass')");
        //header("location:index.php");
    //----------removed that line
}
else
{

}

EDIT: 编辑:

change this 改变这个

    if(isset($_POST['email']) )

to

 if(isset($_POST['email']) or isset($_POST['username']))

this to check them both. 这要检查他们两个。 you are checking just email thats why you dont get the second if. 您正在检查的只是电子邮件,这就是为什么您没有第二个if的原因。

Does it go into the second if statement ( if ($username == $result ) ) after you comment out the first one ( if ($username == $result )) ? 在注释掉第一个if语句(if($ username == $ result))之后,它是否进入第二个if语句(if($ username == $ result))?

If so, then it keeps hitting that exit() function. 如果是这样,那么它将继续命中该exit()函数。

Guys i kinda guessed the answer from combining some of your comments. 伙计们,我有点通过合并您的一些评论来猜测答案。 for some reason i need to include isset($_POST['username']) along with isset($_POST['email']) in order for my if Statements to be all executed... perhaps it was the isset checking if there is a value for username. 由于某种原因,我需要将isset($ _ POST ['username'])和isset($ _ POST ['email'])包括在内,以便我的if语句全部执行...也许是isset检查是否存在是用户名的值。

change this line the following line: 将此行更改为以下行:

 $extract= mysql_query("SELECT username, email FROM users");

Then use where clause as follows: 然后使用where子句,如下所示:

$extract= mysql_query("SELECT username, email FROM users where username='$username' and email='$email'");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM