Registration error

I am creating an app with PHP as the back end code . When I click the register button all of the sign up info goes into the database but I get the else statement in lua that says " local alert = native.showAlert( “Error Signing Up”, “There was an error signing up.”, { “Try again” }  ) " . Can someone help me ? 

register.lua:

local function emptyFields( event ) if ( username.text == "" or pw.text == "" or pw2.text == "" or email.text == "" ) then local alert = native.showAlert( "Error", "Fill in all fields .", { "Try again" } ) return true else return false end end local function urlencode(str) if (str) then str = string.gsub (str, "\n", "\r\n") str = string.gsub (str, "([^%w])", function (c) return string.format ("%%%02X", string.byte(c)) end) str = string.gsub (str, " ", "+") end return str end local function passwordMatch( event ) if ( pw.text ~= pw2.text ) then local alert = native.showAlert( "Error", "Passwords do not match .", { "Try again" } ) return true else return false end end local function networkListener( event ) if ( event.isError ) then print( "Network error.") else if event.response == "success" then -- put the code here to go to where the user needs to be -- after a successful registration composer.gotoScene("login") else -- put code here to notify the user of the problem, perhaps -- a native.alert() dialog that shows them the value of event.response -- and take them back to the registration screen to let them try again local json = require("json") json.prettify( event ) local alert = native.showAlert( "Error Signing Up", "There was an error signing up.", { "Try again" } ) end end end local function userRegister( event ) if ( "ended" == event.phase ) then if emptyFields() == true then else if passwordMatch() == true then else local parameters = {} parameters.body = "Register=1&username=" .. username.text .. "&pw=" .. pw.text .. "&pw2=" .. pw2.text .. "&email=" .. urlencode( email.text ) local URL = "http://hash.net16.net/register.php" network.request(URL, "POST", networkListener, parameters) end end end end

register.php:

\<?php $servername = "localhost"; $username = "id1662780\_hashh"; $password = "bigman23"; $database = "id1662780\_hash"; $con = new mysqli($servername, $username, $password, $database); // Check connection if ($con-\>connect\_error) { die("Connection failed: " . $con-\>connect\_error); } echo "Connected successfully"; if(isset($\_POST['Register'])) { if (empty($\_POST["username"])) { echo"fill in username to sign up"; } else { if (empty($\_POST["pw"])) { echo"fill in password to sign up"; } else { if (empty($\_POST["pw2"])) { echo"confirm password to sign up"; } else { if (empty($\_POST["email"])) { echo"fill in email to sign up"; } else { if ($\_POST['pw'] == $\_POST['pw2']) { $username = mysqli\_real\_escape\_string($con, $\_POST["username"]); $pw= mysqli\_real\_escape\_string($con, $\_POST["pw"]); $email = mysqli\_real\_escape\_string($con, $\_POST["email"]); $result = mysqli\_query($con ,"SELECT \* FROM users WHERE username='" . $username . "'"); if(mysqli\_num\_rows($result) \> 0) { echo "Username exists"; } else { $result2 = mysqli\_query($con ,"SELECT \* FROM users WHERE email='" . $email. "'"); if(mysqli\_num\_rows($result2) \> 0) { echo "email exist"; } else { $sql = "INSERT INTO users (username, pw, email) VALUES('" . $username . "', '" . $pw . "', '" . $email . "')"; if(mysqli\_query($con, $sql)){ // if insert checked as successful echo username and password saved successfully echo"success"; }else{ echo mysqli\_error($con); } } } } else{ echo "The passwords do not match."; // and send them back to registration page }}}}} } ?\>

First change this:

json.prettify( event )

to:

print( json.prettify( event ) )

then get the results from your console log where that event table is printed back and post the contents here:

Rob

This is what it says :

{ 13:48:58.884 "bytesEstimated":-1, 13:48:58.884 "bytesTransferred":29, 13:48:58.884 "name":"networkRequest", 13:48:58.884 "phase":"ended", 13:48:58.884 "requestId":"\<type 'userdata' is not supported by JSON.\>", 13:48:58.884 "response":"Connected successfullysuccess", 13:48:58.884 "responseHeaders":{ 13:48:58.884 "Connection":"keep-alive", 13:48:58.884 "Content-Type":"text/html; charset=UTF-8", 13:48:58.884 "Date":"Mon, 29 May 2017 17:49:31 GMT", 13:48:58.884 "Transfer-Encoding":"chunked", 13:48:58.884 "X-Request-ID":"c361784dc1c8fc2befefc5060f7cce84", 13:48:58.884 "X-Content-Type-Options":"nosniff", 13:48:58.884 "X-Xss-Protection":"1; mode=block", 13:48:58.884 "HTTP-STATUS-LINE":"HTTP/1.1 200 OK", 13:48:58.884 "Server":"awex" 13:48:58.884 }, 13:48:58.884 "responseType":"text", 13:48:58.884 "status":200, 13:48:58.884 "url":"http://hash.net16.net/register.php", 13:48:58.884 "isError":false 13:48:58.884 }

I tried taking a look I don’t know what it means 

In that data, since you did json.prettify is turning your “event” table into a printable string so you can easily see the contents of the table. Each line (for the most part) is a key-value pair with the Key on the left of the colon ( : ) and the value on the right hand side.

Your Lua code looks for the key in the event table called “response”. If you look through there you will see a line that starts with “response”:  Now find the value on the right side of the colon. That’s what your PHP script is returning to you.

It’s pretty obvious what’s going on.

Let’s see if you can find the error armed with the information on how to read the return values.

Rob

is this the problem ?

"responseType":"text"

No, that’s the “responseType” key. You’re looking for “response”.

Rob

I see this response line :

"response":"Connected successfullysuccess",

It doesn’t look like anything is wrong with it . Does that mean the problem is with corona ?

Can you tell me what this means ?

In your Lua code, you have an if statement that is comparing event.response to the string “success”. Are you getting a string “success” back from your PHP script?

No, you’re not. You’re getting the string “Connected successfullysuccess”.  Why is your script outputting that instead of outputting “success”? 

This is the basic debugging that you need to do to solve these problems. If you look through your PHP script, it should be very obvious what is causing the problem…  Every bit of text your PHP writes out (with the echo statement) ends up being put into event.response.

Rob

Thanks. It works

How do I show the PHP errors to the user ?

Just to make sure the fix is correct, all you needed to do was was simply remove this line:

echo "Connected successfully";

from your PHP script. That extra text was being output with your success message which caused the string to not match.

As for notifying your user, are basically doing that correctly. Your other error conditions return something other than success and that will cause your dialog box to show up.  Now a change I would suggest would be to change:

local alert = native.showAlert( "Error Signing Up", "There was an error signing up.", { "Try again" } )

to:

local alert = native.showAlert( "Error Signing Up", event.response, { "Try again" } )

That way you show the user what the PHP script is complaining about. Then you can go and 

Rob

wow thanks

Do you think you could help with my other question on here ?

 local parameters = {} parameters.body = "Register=1&username=" .. username.text .. "&pw=" .. pw.text .. "&pw2=" .. pw2.text .. "&email=" .. urlencode( email.text ) local URL = "http://hash.net16.net/register.php" network.request(URL, "POST", networkListener, parameters)

Sending user names and passwords as plain text is criminal!  Encrypt your data and decrypt it in PHP.

First change this:

json.prettify( event )

to:

print( json.prettify( event ) )

then get the results from your console log where that event table is printed back and post the contents here:

Rob

This is what it says :

{ 13:48:58.884 "bytesEstimated":-1, 13:48:58.884 "bytesTransferred":29, 13:48:58.884 "name":"networkRequest", 13:48:58.884 "phase":"ended", 13:48:58.884 "requestId":"\<type 'userdata' is not supported by JSON.\>", 13:48:58.884 "response":"Connected successfullysuccess", 13:48:58.884 "responseHeaders":{ 13:48:58.884 "Connection":"keep-alive", 13:48:58.884 "Content-Type":"text/html; charset=UTF-8", 13:48:58.884 "Date":"Mon, 29 May 2017 17:49:31 GMT", 13:48:58.884 "Transfer-Encoding":"chunked", 13:48:58.884 "X-Request-ID":"c361784dc1c8fc2befefc5060f7cce84", 13:48:58.884 "X-Content-Type-Options":"nosniff", 13:48:58.884 "X-Xss-Protection":"1; mode=block", 13:48:58.884 "HTTP-STATUS-LINE":"HTTP/1.1 200 OK", 13:48:58.884 "Server":"awex" 13:48:58.884 }, 13:48:58.884 "responseType":"text", 13:48:58.884 "status":200, 13:48:58.884 "url":"http://hash.net16.net/register.php", 13:48:58.884 "isError":false 13:48:58.884 }

I tried taking a look I don’t know what it means 

In that data, since you did json.prettify is turning your “event” table into a printable string so you can easily see the contents of the table. Each line (for the most part) is a key-value pair with the Key on the left of the colon ( : ) and the value on the right hand side.

Your Lua code looks for the key in the event table called “response”. If you look through there you will see a line that starts with “response”:  Now find the value on the right side of the colon. That’s what your PHP script is returning to you.

It’s pretty obvious what’s going on.

Let’s see if you can find the error armed with the information on how to read the return values.

Rob