Help with querying MySQL database

Hi Idiaz,

Thanks for your message.

I have these code snippets you can use that I edit every time I want to achieve sending information to mysql. I sure someone on here will be able to improve but it's a starting point for you.

The information I send to my database in this example is date, time, record, size and resultsend. Your mySQL database must match the data you're sending to it so change these parts as needed.

First place this code in a a function on hype:

// element - DOMHTMLElement that triggered this function being called
// event - event that triggered this function being called
function resultsend(hypeDocument, element, event) {
	
	if(navigator.onLine){
	
	$.ajax({
    url: 'resultsend.php',
    type: 'POST',
    dataType: 'JSON',
    data: {
          date: $('#date').text(),
          time: $('#time').text(),
          record: $('#record').text(),
          size: $('#size').text(),
          
          resultsend: $('#resultsend').text(),
          },
          
// I never managed to get the success function to work. Maybe someone reading this could improve on this part?
          
   success: function (res) {
     
     $('#resultBox').innerhtml(JSON.stringify(res));

            }            
           
});	

// I go to another scene on my project once data has been attempted to be sent. Delete if not needed. (if you manage to get the success function to work you may be able to change something on the scene to show its successfully sent).
hypeDocument.showNextScene(hypeDocument.kSceneTransitionInstant, 1.1);
  
  
} else {

// I use a scene to test my internet connection. Delete if not needed. This was only included as I couldn't get the success function to work. 

hypeDocument.showSceneNamed('connection', hypeDocument.kSceneTransitionInstant, 1.1)

}
	
	
	
}

Then create a .php file called resultsend.php and include the following

<?php

include("../other/config.php");
    
     // get values form input text and number

$date = $_POST['date'];
$time = $_POST['time'];
$record = $_POST['record'];
$size = $_POST['size'];
$resultsend = $_POST['resultsend'];



    
    // connect to mysql database using mysqli

$connect = mysqli_connect($hostname, $username, $password, $databaseName);
    
    // mysql query to insert data

$query = "INSERT INTO `$resultsend`(`date`, `time`, `record`, `size`) VALUES ('$date','$time','$record','$size')";
    
    

$result = mysqli_query($connect,$query);
    
mysqli_free_result($result);
mysqli_close($connect);

?>

You should have a config.php which includes your mySQL database hostname, username, password and database name.

If anyone can improve on this method please contribute. I'm sure there more secure ways to do all of this.

Let me know how you get on,

Chris

1 Like