mysqli无法转换为字符串

时间:2014-07-19 18:45:01

标签: php mysql mysqli

我正在尝试创建一个在db

中插入内容的方法

我读到了这个问题,但似乎只有当人们尝试选择某些东西并且不能获取结果时它才会出现。

我得到的错误是类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 . "'");
}

0 个答案:

没有答案