dtdb99743 2016-02-12 00:37
浏览 71
已采纳

MySQLi OOP类插入函数不起作用

I am currently practicing OOP, creating a MySQLi class that will have atleast the basic MySQLi functions (insert, select, update, etc). This is what I have got so far:

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;
    }
}
}

As you can see, I create the connection via the details from the config file, I then send a query through the established connection. But for some reason when I attempt to create a MySQLi insert query, I just get the same error over, and over again:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''name', 'option') VALUES ('Sub Title', 'This is a test website')' at line 1

I even echoed out the sql query, which appeared to be the correct format:

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

I have spent hours of Googling, trial and error, etc, trying to fix this, and have had no luck, and as it's 12:30am, I'm tired and may be missing something critical, so if anyone knows what is causing this problem, it'll be greatly appreciated for a solution, etc.

Thanks, Kieron

  • 写回答

2条回答 默认 最新

  • dongmopu6734 2016-02-12 00:39
    关注

    The column names in the first set of parenthesis should not be quoted:

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

    Though you can use backticks ` around the column names e.g. `name`, `option`.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog