How would I add data from a JSON file with multiple items to mysql? Currently, This below creates a new row for each item (game) with the values within $json The only problem is, every row has the same $tagsT when each row should be different because each item has its own unique set of tags.
$json = $_POST['json'];
$result = json_decode($json);
foreach($result as $key => $value) {
if($value) {
foreach ($value->tags as $tag) { $tagsT .= $tag.','; }
mysql_query("INSERT INTO `games_db`.`Games` (`title`, `description`, `image`, `category`, `page`, `rating`, `width`, `height`, `tags`) VALUES ('$value->name', '$value->description', '/images/$pageid.jpg', '$category', '$pageid', '$value->rating', '$value->width', '$value->height', '$tagsT')")
}
Clean up the string variable after each loop.
Else it will obviously keep the previous tags. The scope of a local variable is within a function. It will stay valid even if it wasn’t defined before the
foreach.Also consider reading a contemporary database tutorial. Search for PDO and prepared statements.