Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php MySQLi OOP类插入函数不工作_Php_Class_Oop_Mysqli - Fatal编程技术网

Php MySQLi OOP类插入函数不工作

Php MySQLi OOP类插入函数不工作,php,class,oop,mysqli,Php,Class,Oop,Mysqli,我目前正在练习OOP,创建一个MySQLi类,该类将至少具有基本的MySQLi函数(insert、select、update等)。到目前为止,我得到的是: if(!class_exists('dbc')) { class dbc { public function __construct($host = host, $username = username, $password = password, $database = database) { // Make the

我目前正在练习OOP,创建一个MySQLi类,该类将至少具有基本的MySQLi函数(insert、select、update等)。到目前为止,我得到的是:

if(!class_exists('dbc')) {
class dbc {
    public function __construct($host = host, $username = username, $password = password, $database = database) {
        // Make the constants class variables
        $this->host = host;
        $this->username = username;
        $this->password = password;
        $this->database = database;

        $this->connection = new mysqli($this->host, $this->username, $this->password, $this->database);

        if($this->connection->connect_errno) {
            die('Database connection error!');
            return false;
        }
    }

    public function __deconstruct() {
        if($this->connection) {
            $this->connection->close();
        }
    }

    public function insert($table, $variables = array()) {
        if(empty($table) || empty($variables)) {
            return false;
        }

        $sql = "INSERT INTO $table ";

        $fields = array();
        $values = array();
        foreach($variables as $field => $value) {
            $fields[] = "'" . $field . "'";
            $values[] = "'" . $value . "'";
        }

        $fields = '(' . implode(', ', $fields) . ')';
        $values = '(' . implode(', ', $values) . ')';

        $sql .= $fields . ' VALUES ' . $values;

        $query = $this->connection->query($sql);

        if(!$query) {
            echo mysqli_error($this->connection);
        }

        echo $sql;
    }
}
}
如您所见,我通过配置文件中的详细信息创建了连接,然后通过已建立的连接发送查询。但由于某种原因,当我尝试创建MySQLi insert查询时,我一次又一次地遇到相同的错误:

您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在第1行的“名称”、“选项”)值(“子标题”、“这是一个测试网站”)附近使用的正确语法

我甚至重复了sql查询,它似乎是正确的格式:

插入选项(“名称”、“选项”)值(“子标题”、“这是一个测试网站”)

我花了几个小时的时间在谷歌上搜索、尝试和出错等,试图解决这个问题,但运气不好,现在是12:30,我很累,可能错过了一些关键的东西,所以如果有人知道是什么导致了这个问题,我将非常感谢你的解决方案,等等

谢谢,
Kieron

不应引用第一组括号中的列名:

INSERT INTO options (name, option) VALUES ('Sub Title', 'This is a test website')
//                   ^^^^  ^^^^^^

虽然您可以在列名(例如,
`name`,`option`

周围使用反勾号
`
),但您的连接肯定无法正常工作,因为您在参数名称前面的这4行上缺少了
$

   $this->host = host;
   $this->username = username;
   $this->password = password;
   $this->database = database;
应该是

   $this->host     = $host;
   $this->username = $username;
   $this->password = $password;
   $this->database = $database;
此外,您用于类解构器的名称不正确,应该是

public function __destruct() () {
您的将不会导致错误,但它不会在使用您的名称销毁类时自动运行


@Marty在查询语法中使用反勾号而不是单引号是正确的,但我不知道如何根据我提到的第一个错误建立连接,因此也不知道如何报告一个合理的SQL错误,但是从您向我们展示的代码中可能会出现一些不明显的情况。

只是更改了它,我仍然收到相同的错误。如果您更改了它,那么错误将不同(它不会说
…使用接近“name…”的正确语法,因为该引号将被删除)。你能公布你现在得到的确切错误吗?没关系,我刚刚意识到我使用了option作为列名,尽管它是通过MySQLi保留的。。。睡觉时间让我思考。谢谢,我正在尝试将您的答案设置为绿色,但有一段等待时间。看起来这些可能是某个地方的常量(虽然似乎仍然缺少
$
,但如果是这种情况,这两种方法都会起作用)。奇怪的是,在这种情况下,它们应该作为参数传递