dsn46282 2018-06-16 23:20
浏览 39

当我尝试访问它时,cpanel mysql数据库抛出500(内部服务器错误)

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥120 计算机网络的新校区组网设计
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答
    • ¥15 win11 23H2删除推荐的项目,支持注册表等
    • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
    • ¥15 qt6.6.3 基于百度云的语音识别 不会改
    • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单