AJAX Login PHP [FIXED]

Just started using Construct 2? Post your questions here

Post » Thu Jun 11, 2015 1:02 am

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

Code: Select all
<?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);
?>


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.
Last edited by Kettlewell on Thu Jun 11, 2015 10:18 am, edited 1 time in total.
B
4
Posts: 19
Reputation: 240

Post » Thu Jun 11, 2015 5:57 am

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:

Code: Select all
$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
}


Write mysql condition uppercase!
If you do more with this futher, look for "or die()" and "sprintf()".
Sorry for my bad english, but I really do my best! And use and check with Google Translator.
B
10
S
4
G
1
Posts: 158
Reputation: 1,303

Post » Thu Jun 11, 2015 10:18 am

Thank you, it's working perfectly.
B
4
Posts: 19
Reputation: 240


Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 0 guests