It all works fine when I do it locally, but now I set up a database at cpanel with a user that has all the previliges. Whenever I try to add to the database it just throws 500 (Internal Server Error)
Here's what I got
Ajax:
this.form.addEventListener('submit', function(e){
e.preventDefault();
var submissionInfo = {
'entry': 'entry',
'author': 'author',
}
var data = JSON.stringify(submissionInfo);
var params = 'submit=' + data;
var xhr = new XMLHttpRequest();
xhr.open('POST', 'submit.php', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send(params);
});
Submit.php
$submission = $_POST['submit'];
$decode = json_decode($submission, true);
$config = require 'config.php';
$query = require 'bootstrap.php';
$query->insert('submissions', $decode);
echo $submission;
Bootstrap.php
function connection($config){
try {
return new PDO(
"mysql:" . $config['connection'] . ';dbname='.$config['name'],
$config['username'], $config['password'], $config['options']
);
} catch(PDOException $e){
die('could not connect.');
}
}
class QueryBuilder {
protected $pdo;
public function __construct($pdo){
$this->pdo = $pdo;
}
public function insert($table, $parameters){
$sql = sprintf(
'insert into %s (%s) values (%s)',
$table,
implode(', ', array_keys($parameters)),
':' . implode(', :', array_keys($parameters))
);
try {
$statement = $this->pdo->prepare($sql);
$statement->execute($parameters);
} catch (Exception $e) {
die($e->getMessage());
}
}
}
return new QueryBuilder( connection($config['database']) );
config.php
<?php
return [
'database' => [
'name' => 'database_name',
'username' => 'myUsername',
'password' => 'myPassword',
'connection' => 'localhost',
'options' => [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]
]
];
Sorry I know this is a lot of code, but I honestly have no idea what the problem might be so I don't know how to narrow it down. Any ideas?