I have some data that I want to store in Database for synchronization. Here is my jQuery code:
$(document).ready(function() {
var local_array = [];
for ( var i = 0, len = localStorage.length; i < len; ++i ) {
local_array.push(localStorage.key(i));
local_array.push(localStorage.getItem(localStorage.key(i)));
console.log(local_array);
}
$.ajax({
type: "POST",
url: "uploadtodatabase.php",
data: {data: local_array}
});
});
This is my PHP code:
<?php session_start();
$connection = new mysqli('host', 'name', 'password', 'database');
$username = $_SESSION['user'];
$username = $connection->real_escape_string($username);
$query = $connection->query("SELECT * from members WHERE username ='$username'");
$matches = mysqli_num_rows($query);
if ($matches == 1) {
$row = mysqli_fetch_array($query);
$visited = $_POST['data'];
$visited = $connection->real_escape_string($visited);
$connection->query("UPDATE members SET visited = '$visited' WHERE username ='$username'");
}
mysqli_close($connection);
?>
The data is stored in local_array , as I checked in console. The jQuery code in is footer.php. This is the same location where I store uploadtodatabase.php. However, footer.php is appended to other files which might not be in same directory. Can this cause the problem? I have never used AJAX before. So, I might be doing something stupid and not notice it at all.