AJAX Login PHP [FIXED]

  • I've been using a PHP file made from the Login tutorial and it works fine but I've been trying to force an error by creating an account on the login system with an email that already exists, I'm using this code

    <?php header('Access-Control-Allow-Origin: *');
    
    ini_set('display_errors', 'On');
    error_reporting(E_ALL | E_STRICT);
    
    $email = $_GET['femail'];
    $password = $_GET['fpass'];
    $con=mysqli_connect("mysql2.clusterdb.net","PSDB-9qw","bravo190","PSDB-9qw","3306","/var/lib/mysql/cluster2.sock");
    
    // Check connection
    if (mysqli_connect_errno()){
      echo "-1";
     } else {
    	
    	$qz = "SELECT IsMember FROM UserAuthData where email='".$email."' and password='".$password."'"; 
    	$qz = str_replace("\'","",$qz);
    	$result = mysqli_query($con,$qz);
    
    	if (!$result) {
    		echo "-2";
    	} else {
    		while($row = mysqli_fetch_array($result))
    		{
    			echo $row['IsMember'];
    		}
    	}	 
    }
    
    mysqli_close($con);
    ?>[/code:1q9v6mso]
    
    The problem is with my limited knowledge of php and mysql, I expected this to return a -2 value to ajax like it does when there are no errors due to duplicate unique keys in the database but it doesn't, ajax never returns a value.
    
    Would anyone know why it will return IsMember fine but not -2?
    
    Thank you.
  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • If no mysql error happens, result is set. if ($result) //->true

    No entry found is no error, its a 0 row result. if (mysqli_num_rows($result)) //->false

    This is not best and shortest version but easy to understand:

    $qz = "SELECT IsMember FROM UserAuthData WHERE email='".$email."' AND password='".$password."'"; 
    $result = mysqli_query($con,$qz);
    
    if ($result) {
          if (mysqli_num_rows($result)) {
                while($row = mysqli_fetch_array($result))
                {
                     echo $row['IsMember'];        //success
                }
          }
          else
          {
                 echo "-2";       //no entry found
          }
    }
    else
    {
            echo "";      //mysql error
    }
    [/code:zd8jxqii]
    
    Write mysql condition uppercase!
    If you do more with this futher, look for "or die()" and "sprintf()".
  • Thank you, it's working perfectly.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)