Note that there are some explanatory texts on larger screens.

plurals
  1. POWrong errorcode when using JSON parser
    text
    copied!<p>I'm having trouble finding the error in my .php script.</p> <p>I use an android app sending post requests in order to register a user including a unique device id and email.</p> <p>When sending the data my api checks if the email or device id already exist so you can't register twice.</p> <p>When registering the first time, everything works. When i try to register again with the same email it works aswell (getting the correct error). But if I use a different email (but the same device id) I get a wrong error code.</p> <p>Here is the PHP-Code:</p> <pre><code>else if ($tag == 'register') { // Request type is Register new user $name = $_POST['name']; $email = $_POST['email']; $password = $_POST['password']; $devid = $_POST['devid']; // check if user already exists if ($db-&gt;CheckUser($email)) { // user already exists $response["error"] = 2; $response["error_msg"] = "User already exists"; echo json_encode($response);} else if ($db-&gt;CheckDevice($devid)) { // Device already exists $response["error"] = 3; $response["error_msg"] = "Device already exists"; echo json_encode($response);} else { // store user $user = $db-&gt;storeUser($name, $email, $password, $devid); if ($user) { // user stored successfully $response["success"] = 1; $response["uid"] = $user["unique_id"]; $response["user"]["name"] = $user["name"]; $response["user"]["email"] = $user["email"]; $response["user"]["devid"] = $user["devid"]; $response["user"]["created_at"] = $user["created_at"]; $response["user"]["updated_at"] = $user["updated_at"]; echo json_encode($response);} else { // user failed to store $response["error"] = 1; $response["error_msg"] = "Error occured in Registration"; echo json_encode($response);} } } </code></pre> <p>Check Functions:</p> <pre><code>public function CheckUser($email) { $result = mysql_query("SELECT email from users WHERE email = '$email'"); $no_of_rows = mysql_num_rows($result); if ($no_of_rows &gt; 0) { // user existed return true;} else { // user not existed return false;} } public function CheckDevice($devid) { $result = mysql_query("SELECT devid from users WHERE devid = '$devid'"); $no_of_rows = mysql_num_rows($result); if ($no_of_rows &gt; 0) { // user existed return true; } else { // user not existed return false; } } </code></pre> <p>Store Function:</p> <pre><code>public function storeUser($name, $email, $password, $devid) { $uuid = uniqid('', true); $hash = $this-&gt;hashSSHA($password); $encrypted_password = $hash["encrypted"]; // encrypted password $salt = $hash["salt"]; // salt $result = mysql_query("INSERT INTO users(unique_id, name, email, encrypted_password, salt, devid, created_at) VALUES('$uuid', '$name', '$email', '$encrypted_password', '$salt', '$devid', NOW())"); // check for successful store if ($result) { // get user details $uid = mysql_insert_id(); // last inserted id $result = mysql_query("SELECT * FROM users WHERE uid = $uid"); // return user details return mysql_fetch_array($result); } else { return false; } } </code></pre> <p>I'm sending </p> <p>I should get this error:</p> <pre><code>$response["error"] = 3; $response["error_msg"] = "Device already exists"; </code></pre> <p>But i am getting this one:</p> <pre><code>$response["error"] = 1; $response["error_msg"] = "Error occured in Registration"; </code></pre> <p>Somehow when calling the <code>CheckDevice</code> Function it seems to return <code>false</code> although when I manually use <code>SELECT devid from users WHERE devid = '$devid'</code> in phpMyAdmin I get a <code>true</code>.</p> <p>Then he fails to store because the device id must be unique and gives me the error (thats the only plausible explanation).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload