Updating online Database

H​ello , I am a new learner of Corona.

​I wanted to know how to update any field in a database table accessed via a remote server using Corona.

​I am using PHP as a mediator between the remote server and mobile device(Corona).

Any help or code regarding the above is appreciated.

Thanks in advance

Shivani 

In your Corona app, you will have to capture input. Most likely you will use a native.newTextField() to input text and numbers. But you can use widget.newSwitch()'s to collect yes/no type values or widget.newSlider()'s to collect a range of values or a native.newPickerWheel() to select fixed values from a list.

Once you have your data, you will use network.request() to call your PHP script. You can use HTTP GET and pass the values as key-value pairs on the URL:

https://yoursite.com/yourscript.php?key1=value1&key2=value2&key3=value3

or as a HTTP POST request where all the stuff after the ? is put into a table structure (see the documentation for an example). You have to use POST or GET based on what the PHP script is expecting. The key names (key1, key2) have to match the variables the PHP script is expecting.

The PHP script will get the values either in its $_GET[] associate array or a $_POST[] array depending on the method used to send the data. You should take any input from the user and run it through a database sanitize that makes sure there is no SQL injection hacks in the data. Then your PHP script can update the database.

If the database/PHP script needs to return data, put it into an associative array in the PHP script then do something like:

echo json_encode( $yourTableOfDataToReturn );

that will send the data back in a way that network.request() is expecting. You’re call back/listener function for network.request() will have an event table with an entry called event.response that will hold your JSON data your PHP script created. To get that back into a Lua table that your app can use:

local myLocalDataTable = json.decode( event.response )

Rob

Hello sir,

Thank you very much for your assistance and very sorry for the delay in replying.

I have been trying with the code but have not got much success with the code for updating to the database.

It will be of a great help if you could send some code snippets for both PHP and Corona.

I want some code snippets which can help me understand how to send the data through corona and recieve data in PHP and save the same data in variables and use those variables for different queries such as update, delete, etc.

​I am using “GET” .

Thank you

Shivani 

Here’s a tutorial on the Corona side:
 

https://coronalabs.com/blog/2015/06/02/tutorial-connecting-to-restful-api-services/

Here is a pretty simple example of a PHP script. It expects a Username and Password passed as base64 encoded text. It returns information about the user back to them.

\<?php // // getstatus.php for the Trivia Game // define( "DB\_DSN", 'yourdbname' ); define( "DB\_HOST", 'localhost' ); define( "DB\_USER", 'yourdbuser' ); define( "DB\_PASS", 'yourdbpassword' ); // Connecting, selecting database // print("connecting to database\n"); $link = mysql\_connect(DB\_HOST, DB\_USER, DB\_PASS) or die('Could not connect: ' . mysql\_error()); mysql\_select\_db(DB\_DSN) or die('Could not select database'); if(isset($\_GET)) { $playername = base64\_decode($\_GET["playername"]); $password = base64\_decode($\_GET["password"]); $query = 'SELECT \* FROM players WHERE playername="' . mysql\_real\_escape\_string($playername) . '" or email="' . mysql\_real\_escape\_string($loginid) . '"'; $dbresult = mysql\_query($query, $link); if (!$dbresult) { http\_response\_code(403); $result = array(); $result["message"] = "Forbidden"; echo json\_encode($result); mysql\_free\_result($dbresult); exit; } $player = mysql\_fetch\_array($dbresult, MYSQL\_ASSOC); if (strcmp($player["password"],md5($password)) == 0) { http\_response\_code(200); $result = array(); $result["message"] = "Success"; $result["privs"] = $player["privileges"]; echo json\_encode($result); } else { //echo "password mismatch"; http\_response\_code(403); $result = array(); $result["message"] = "Forbidden"; echo json\_encode($result); } mysql\_free\_result($dbresult); } else { http\_response\_code(400); $result = array(); $result["message"] = "Bad Request"; echo json\_encode($result); } //echo "exiting"; exit; ?\>

The Corona side of things looks like:

local mime = require( "mime" ) local function loginCallback(event) if ( event.isError ) then -- handle error else print ( "RESPONSE: " .. event.response ) local data = json.decode(event.response) -- deal with returned data end return true end local URL = "http://yoursite.com/yourphpscript.php?playername=" .. mime.b64(username) .. "&password=" .. mime.b64(password) network.request(URL , "GET", callback )

This isn’t the most secure way of handling logins. It was also taken from a running app, but stripped of any things not needed. It may have errors, but it should get you close.

Thank you sir.

Shivani

Thank you Sir for your assistance. Its working all fine and perfect. Thanks a lot again.

Shivani

In your Corona app, you will have to capture input. Most likely you will use a native.newTextField() to input text and numbers. But you can use widget.newSwitch()'s to collect yes/no type values or widget.newSlider()'s to collect a range of values or a native.newPickerWheel() to select fixed values from a list.

Once you have your data, you will use network.request() to call your PHP script. You can use HTTP GET and pass the values as key-value pairs on the URL:

https://yoursite.com/yourscript.php?key1=value1&key2=value2&key3=value3

or as a HTTP POST request where all the stuff after the ? is put into a table structure (see the documentation for an example). You have to use POST or GET based on what the PHP script is expecting. The key names (key1, key2) have to match the variables the PHP script is expecting.

The PHP script will get the values either in its $_GET[] associate array or a $_POST[] array depending on the method used to send the data. You should take any input from the user and run it through a database sanitize that makes sure there is no SQL injection hacks in the data. Then your PHP script can update the database.

If the database/PHP script needs to return data, put it into an associative array in the PHP script then do something like:

echo json_encode( $yourTableOfDataToReturn );

that will send the data back in a way that network.request() is expecting. You’re call back/listener function for network.request() will have an event table with an entry called event.response that will hold your JSON data your PHP script created. To get that back into a Lua table that your app can use:

local myLocalDataTable = json.decode( event.response )

Rob

Hello sir,

Thank you very much for your assistance and very sorry for the delay in replying.

I have been trying with the code but have not got much success with the code for updating to the database.

It will be of a great help if you could send some code snippets for both PHP and Corona.

I want some code snippets which can help me understand how to send the data through corona and recieve data in PHP and save the same data in variables and use those variables for different queries such as update, delete, etc.

​I am using “GET” .

Thank you

Shivani 

Here’s a tutorial on the Corona side:
 

https://coronalabs.com/blog/2015/06/02/tutorial-connecting-to-restful-api-services/

Here is a pretty simple example of a PHP script. It expects a Username and Password passed as base64 encoded text. It returns information about the user back to them.

\<?php // // getstatus.php for the Trivia Game // define( "DB\_DSN", 'yourdbname' ); define( "DB\_HOST", 'localhost' ); define( "DB\_USER", 'yourdbuser' ); define( "DB\_PASS", 'yourdbpassword' ); // Connecting, selecting database // print("connecting to database\n"); $link = mysql\_connect(DB\_HOST, DB\_USER, DB\_PASS) or die('Could not connect: ' . mysql\_error()); mysql\_select\_db(DB\_DSN) or die('Could not select database'); if(isset($\_GET)) { $playername = base64\_decode($\_GET["playername"]); $password = base64\_decode($\_GET["password"]); $query = 'SELECT \* FROM players WHERE playername="' . mysql\_real\_escape\_string($playername) . '" or email="' . mysql\_real\_escape\_string($loginid) . '"'; $dbresult = mysql\_query($query, $link); if (!$dbresult) { http\_response\_code(403); $result = array(); $result["message"] = "Forbidden"; echo json\_encode($result); mysql\_free\_result($dbresult); exit; } $player = mysql\_fetch\_array($dbresult, MYSQL\_ASSOC); if (strcmp($player["password"],md5($password)) == 0) { http\_response\_code(200); $result = array(); $result["message"] = "Success"; $result["privs"] = $player["privileges"]; echo json\_encode($result); } else { //echo "password mismatch"; http\_response\_code(403); $result = array(); $result["message"] = "Forbidden"; echo json\_encode($result); } mysql\_free\_result($dbresult); } else { http\_response\_code(400); $result = array(); $result["message"] = "Bad Request"; echo json\_encode($result); } //echo "exiting"; exit; ?\>

The Corona side of things looks like:

local mime = require( "mime" ) local function loginCallback(event) if ( event.isError ) then -- handle error else print ( "RESPONSE: " .. event.response ) local data = json.decode(event.response) -- deal with returned data end return true end local URL = "http://yoursite.com/yourphpscript.php?playername=" .. mime.b64(username) .. "&password=" .. mime.b64(password) network.request(URL , "GET", callback )

This isn’t the most secure way of handling logins. It was also taken from a running app, but stripped of any things not needed. It may have errors, but it should get you close.

Thank you sir.

Shivani

Thank you Sir for your assistance. Its working all fine and perfect. Thanks a lot again.

Shivani