0

I'm building a simple sign up form with AngularJS and sending the data to a PHP page using JQuery's $.post(). When I send the data, it correctly gets inserted into the database. However, the returned json that I am logging is showing my data fields as null.

Console:

{"status":"success","email":null,"id":null,"sessionId":null}

Javascript:

$.post("admin/addUser.php", {
    email: form.email,
    password: form.password
}).done(function(data){
    console.log(data);
});

PHP:

$email = mysql_real_escape_string($_POST["email"]);
$password = md5(mysql_real_escape_string($_POST["password"]));
$sessionId = md5(uniqid(microtime()) . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']);

//Add this user to the database
$sql = mysql_query("INSERT INTO users (email, password, sessionId) VALUES ('".$email."', '".$password."', '".$sessionId."')");

if ($sql){
    //Now find the user we just added
    $getUser = mysql_query("SELECT * FROM users WHERE email = '".$email."' AND sessionId = '".$sessionId."'");
    if ($getUser){
        $row = mysql_fetch_array($getUser);
        $user = array(
            'status' => 'success',
            'email' => $row['email'],
            'id' => $row['id'],
            'sessionId' => $row['sessionId']
            );
            echo json_encode($user);
    }else{
        $user = array(
            'error' => mysql_error()
        );
        echo json_encode($user);
    }
}else{
    $user = array(
        'error' => mysql_error()
    );
    echo json_encode($user);
}

2 Answers 2

1

Are you sure that you have only one record in here

$getUser = mysql_query("SELECT * FROM users WHERE email = '".$email."' AND sessionId = '".$sessionId."'");

Try to dump $row and see the response. BTW I would suggest you to add limit

$getUser = mysql_query("SELECT * FROM users WHERE email = '".$email."' AND sessionId = '".$sessionId."' LIMIT 1");
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the help. I added if (mysql_num_rows($getUser) == 1) and it doesn't evaluate. Odd, because I don't have any other data in this table. I'm trying to insert the data, and if successful, retrieve it and return it as json.
BTW I cant understand why do you select the data after insert?
@user3796431 Once the user has signed up, I want to get their sessionId and store it locally so they don't have to sign in again. It's also a bit of confirmation for me that this has worked. I don't understand why (mysql_num_rows($getUser) == 1) returns false when there's no one else in the DB. Additionally, the email column requires unique values, as does the sessionId column.
Last guess :) $getUser = mysql_query("SELECT COUNT(*) AS num FROM users WHERE email = '".$email."' AND sessionId = '".$sessionId."'"); $num = mysql_fetch_array($getUser); if($num['num'] == 1) echo json_encode(array( 'status' => 'success', 'email' => $email, 'id' => mysql_insert_id(), 'sessionId' => $sessionId ));
0

Ok, I dug around and found the answer to this. It was a mistake on my end. I was only storing the password and sessionId as varchar(30). When I was generating the sessionId and checking it against the DB, it was being cut off when it was stored, since I was only allowing 30 chars. I update to 255 and works as expected :-P.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.