Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.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转换为字符串_Php_Mysql_Mysqli - Fatal编程技术网

Php 无法将mysqli转换为字符串

Php 无法将mysqli转换为字符串,php,mysql,mysqli,Php,Mysql,Mysqli,我正在尝试创建一个在数据库中插入内容的方法 我读过关于这个问题的文章,但似乎只有当人们试图选择某样东西而没有得到结果时,这个问题才会出现 我得到的错误是类mysqli的对象无法转换为字符串 class Connection { private $host = NULL; private $user = NULL; private $password = NULL; private $database = NULL; private $link = NULL

我正在尝试创建一个在数据库中插入内容的方法

我读过关于这个问题的文章,但似乎只有当人们试图选择某样东西而没有得到结果时,这个问题才会出现

我得到的错误是类mysqli的对象无法转换为字符串

class Connection
{
    private $host = NULL;
    private $user = NULL;
    private $password = NULL;
    private $database = NULL;
    private $link = NULL;

    public function __construct($host, $user, $password, $database)
    {
        $this -> host = $host;
        $this -> user = $user;
        $this -> password = $password;
        $this -> database = $database;
        $this -> link = new mysqli($this -> host, $this -> user, $this -> password, $this -> database) or die;
        $this -> link -> set_charset("utf8") or die;
    }

    // ex. Insert("mail", "`name`, `content`", "'john', 'lorem'")
    public function Insert($dbname,$columns,$values)
    {
        if($dbname && $columns && $values)
        {
        $this -> link -> query("INSERT INTO" . $dbname . " (" . $columns . ") VALUES (" . $values . ")") or die($this -> link);
        }
    }
}


$connection = new Connection(host,user,password,database);

$brand = (isset($_POST['brand'])) ? htmlentities($_POST['brand']) : NULL;
$horsepower = (isset($_POST['horsepower'])) ? htmlentities($_POST['horsepower']) : NULL;
$manufacturedate = (isset($_POST['manufacturedate'])) ? htmlentities($_POST['manufacturedate']) : NULL;
$importer = (isset($_POST['importer'])) ? htmlentities($_POST['importer']) : NULL;
$description = (isset($_POST['description'])) ? htmlentities($_POST['description']) : NULL;
if( !empty($brand) && 
    !empty($horsepower) &&
    !empty($manufacturedate) &&
    !empty($importer) &&
    !empty($description)
    ){
    $connection -> Insert(
        "cars",
        "`brand`, `manufacturedate`, `horsepower`, `importer`, `description`",
        "'". $brand . "', '" . $manufacturedate . "', '" . $horsepower . "', '" . $importer . "', '" . $description . "'");
}

此错误在哪一行抛出?在函数中,在类连接中插入
die
中以$this->link->query
$this->link开头的行是对象。它不能转化为祝福你和那些帮助像我这样的人的人。如果有人感兴趣,问题是INSERT INTO和数据库名称之间没有空格。错误是“表X不存在”。