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’)”);
?>