Post data to mySQL using PHP

I am working on an app and I need to send information from the client to the server.  I am connecting to the server, but I’m not having any of the information passed on to my MySQL database.  I’m not sure if the problem is with my lua or php file, could be both.  but all assistance will be greatly appreciated.  Here is my code:


LUA


local json = require “json”

print (“test”)

sendInfo = {[“asked”] = 5, [“correct”] = 3, [“wrong”] = 2}

print(sendInfo)

local function networkListener( event )
if ( event.isError ) then
print( “Network error!” )
else
print( "RESPONSE: "…event.response )
end
end

local headers = {
[“Content-Tpe”] = “application/x-www-form-urlencoded”,
[“Accept-Language”] = “en-US”,
}

local params = {}
params.headers = headers
params.body = json.encode( sendInfo )

print( "params.body: "…params.body )

network.request( httpAddr, “POST”, networkListener, params)


PHP


$data = file_get_contents(‘php://input’);
 

$json = json_decode($data, true);
var_dump($json);

echo “<br />” . $json[“asked”];
$a = $json[“asked”];
$c = $json[“correct”];
$w = $json[“wrong”];
$d = date(“Y-m-d”);
$t = date(“H:i:s”);

echo "<br />Total Questions Asked: " . $a;
echo "<br />Total Questions Correct: " . $c;
echo "<br />Total Questions Wrong: " . $w;
echo "<br />Today’s Date is : " . $d . " and the time is: " . $t . “<br />”;
try
{
$pdo = new
PDO(‘mysql:host=;dbname=test’,$username,$password);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec(‘SET NAMES “utf8”’);

}
catch (PDOException $e)
{
$output = ‘Unable to connect to the database server.’ . $e->getMessage();
include ‘output.html.php’;
}

echo ‘Successful Connection to Server’;

$pdo->exec(“INSERT INTO results (date, time, asked, correct, wrong) VALUES (’$d’, ‘$t’, ‘$a’, ‘$c’, ‘$w’)”);

?>

Your headers look incorrect.

Lua headers should be:

local headers = { ["Content-Type"] = "application/json", -- you had a typo here too ["Accept-Language"] = "en-US", }

You should also probably set your PHP header too:

header( "Content-Type: application/json" );

After that, you should verify that you are receiving the data correctly on the PHP side, before worrying about it getting passed on to the database, as getting the data sent correctly is the first step.

Hope this helps

Take a look at http://coronium.io/

Can make the mysql interaction truly a non-issue. Does so much more than mysql of course so you might find other uses for it too.

Totally free and amazing! 

agree with ksan.

Thanks guys for the coronium plug, I’ll have to check that out. However, I’m still not seeing the information being sent from the app to the server.  I’m printing the params.body just before posting params to the server. When I echo  $data  I get**: string(0) “”**. 

Also, I’m not trying to pass the variables yet.  I know that section already works because I worked backwards from an html form.  So once I receive the json string, I just need to parse the data to the variables.   Thank you for your help.

Your headers look incorrect.

Lua headers should be:

local headers = { ["Content-Type"] = "application/json", -- you had a typo here too ["Accept-Language"] = "en-US", }

You should also probably set your PHP header too:

header( "Content-Type: application/json" );

After that, you should verify that you are receiving the data correctly on the PHP side, before worrying about it getting passed on to the database, as getting the data sent correctly is the first step.

Hope this helps

Take a look at http://coronium.io/

Can make the mysql interaction truly a non-issue. Does so much more than mysql of course so you might find other uses for it too.

Totally free and amazing! 

agree with ksan.

Thanks guys for the coronium plug, I’ll have to check that out. However, I’m still not seeing the information being sent from the app to the server.  I’m printing the params.body just before posting params to the server. When I echo  $data  I get**: string(0) “”**. 

Also, I’m not trying to pass the variables yet.  I know that section already works because I worked backwards from an html form.  So once I receive the json string, I just need to parse the data to the variables.   Thank you for your help.

Hey, ii am also trying ti work on something similar to you… i made a mySQL db and i want to integrate it to work with corona sdk but i have to make a php file and echo the table onto a website to read it with corona by using the network.request function… i dont know how this works but i figure it will be much safer to connect directly to the sql db… im clueless

I’ll recommend the same as I did for OP… 

Take a look at http://coronium.io/

 

Can make the mysql interaction truly a non-issue. Does so much more than mysql of course so you might find other uses for it too.

 

Totally free and amazing! 

Thanks for the great advice, it seems really really helpful! Unfortunately… I dont know what coronium is and how it works…Nis it a part of corona? Plugin parhaps? May i get any resourse where i can educate myself about this? Or can you parhaps tell me a thing or two to get myself started up?

This is a good place to start : http://www.develephant.net/up-and-running-with-coronium-cloud-platform/

Basically, Coronium is a highly Corona SDK & Lua specific middleware allowing you to run Lua code on a server and your mobile app to get the mobile app to do the following things : 

Read & Write to MySQL databases

Read & Write to MongoDB databases

Call PHP code

Make network calls

Send emails (from server)

Upload files

Manage user accounts

Send push messages 

Setup recurring jobs on the server

And a lot more that I’m sure I’m forgetting. Start with the above document and watch the excellent intro videos from @Develephant. All your questions will be answered. Good luck.

Hey, ii am also trying ti work on something similar to you… i made a mySQL db and i want to integrate it to work with corona sdk but i have to make a php file and echo the table onto a website to read it with corona by using the network.request function… i dont know how this works but i figure it will be much safer to connect directly to the sql db… im clueless

I’ll recommend the same as I did for OP… 

Take a look at http://coronium.io/

 

Can make the mysql interaction truly a non-issue. Does so much more than mysql of course so you might find other uses for it too.

 

Totally free and amazing! 

Thanks for the great advice, it seems really really helpful! Unfortunately… I dont know what coronium is and how it works…Nis it a part of corona? Plugin parhaps? May i get any resourse where i can educate myself about this? Or can you parhaps tell me a thing or two to get myself started up?

This is a good place to start : http://www.develephant.net/up-and-running-with-coronium-cloud-platform/

Basically, Coronium is a highly Corona SDK & Lua specific middleware allowing you to run Lua code on a server and your mobile app to get the mobile app to do the following things : 

Read & Write to MySQL databases

Read & Write to MongoDB databases

Call PHP code

Make network calls

Send emails (from server)

Upload files

Manage user accounts

Send push messages 

Setup recurring jobs on the server

And a lot more that I’m sure I’m forgetting. Start with the above document and watch the excellent intro videos from @Develephant. All your questions will be answered. Good luck.