So, I'm trying to work with a simple API parse, POST the data to a PHP file to save into a database. My problem is there is a 500 server error surrounding the POST and I'm not sure what is going on.
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
Here is view-deals.html, this file is where I grab data from API result:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<?php
require_once 'db_config.php';
echo "vd-1";
//mysql_query($sqlApiAccess) or die('Error, insert query failed');
?>
<script type='text/javascript'>
$(function () {
var summonerId = '----------';
$.getJSON("----------" + ---------- + "/----------?",
{
api_key: "----------"
}).done(function (data) {
console.log(data);
// do whatever processing you need to do to the data
$.post('save-deals.php', {dealData: data}, function(finishData) {
//This is an optional function for when it has finished saving
});
// Your formatting comes next
});
});
</script>
</head>
<body>
</body>
</html>
Here is the save-deals.php file where I'm POSTing the data to, to save in the DB:
<!DOCTYPE html>
<html>
<!--<head>
<?php
require_once 'db_config.php';
//mysql_query($sqlApiAccess) or die('Error, insert query failed');
?>
</head>-->
<body>
<?php
require_once 'db_config.php';
ob_start(); //I like output-buffering because if we need to change headers mid script nothing dies
$DealData = isset($_POST['dealData']) ? $_POST['dealData'] : die("Malformed form data!");
if ($DealData!='') {
$DealData = $DB->real_escape_string($DealData); //Sanitize it for MySQL
if (!$DB->query("INSERT INTO deals(data) VALUES ($DealData)") {
echo "Insert failed: (" . $DB->errno . ") " . $DB->error;
} else {
//AT THIS POINT IT SHOULD HAVE BEEN INSERTED!
//You could return a success string, or whatever you want now.
}
} else {
http_response_code("400");
die("Bad Request, please check your entry and try again");
}
ob_end_flush(); //Close up the output-buffer
?>
</body>
</html>
Here is the network result on view-deals.html page:
Here is the error on save-deals.php page: