简体   繁体   中英

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. 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() .

Remove the exit() calls...

You also really REALLY need to learn about WHERE clauses in queries. 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.

Does it go into the second if statement ( if ($username == $result ) ) after you comment out the first one ( if ($username == $result )) ?

If so, then it keeps hitting that exit() function.

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.

change this line the following line:

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

Then use where clause as follows:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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