JS code:
function saveCategories(i){
var categoriesList=JSON.parse(localStorage.getItem("categoriesList"));
var code=localStorage.getItem("gameCode");
if(i == categoriesList.length)
return;
var categoryName=categoriesList[i]['name'];
var items=categoriesList[i]['items'];
$.get('http://localhost:8080/yourfolder/newCategory.php',{GameCode:code,items:items,CategoryName:categoryName},function(resp)
{
saveCategories(i+1);
});
}
PHP code:
<?php
header("Access-Control-Allow-Origin");
$host="127.0.0.1";
$user="root";
$pass="password";
$databaseName="games";
$con=mysql_connect($host,$user,$pass,$databaseName);
if(!$con)
{
die("Connection failed: ".mysqli_connect_error());
}
$code=$_GET["GameCode"];
$name=$_GET["CategoryName"];
$items=$_GET["items"];
$data=array();
foreach($items as $item)
$data[]=addcslashes($item);
$data=implode(",",$data);
$sql="INSERT INTO games.categories(code,name,items) VALUES ('$code','$name','$data')";
$result=mysql_query($sql,$con);
echo $result;
?>
The php file (newCategory.php) should get code, category name and category items and insert them to MYSQL db.
The saveCategories
recursive function should get the newCategory.php file for each category (in index i), but for some reason, it proceed to the next iteration without getting a result from the first GET
request before.
The unwanted result is that the first category in index 0 is not saved in the MYSQL db.