SQL 插入语法


SQL Insert Syntax

public $id;
public $post;
public $postedon;
public function insert($target_file){
    $sql="INSERT INTO post (ID,Post,PostedOn,Picture) VALUES (".$this->id.",".$this->post.",".$this->postedon.",".$target_file.")";
    $this->db->execute($sql);
}
/

/没有错误,但我无法在数据库上插入值

一次

用下面的代码替换你的查询字符串

$sql="INSERT INTO post (ID,Post,PostedOn,Picture) VALUES ('$this->id','$this->post','$this->postedon','$target_file')";

Tty 与single quote一起使用,因为您要在数据库中插入字符串值,需要single quote才能插入数据库。

$sql="INSERT INTO post (ID,Post,PostedOn,Picture) VALUES (".$this->id.",'".$this->post."','".$this->postedon."','".$target_file."')";

而不是

$sql="INSERT INTO post (ID,Post,PostedOn,Picture) VALUES (".$this->id.",".$this->post.",".$this->postedon.",".$target_file.")";

值中缺少'

public $id;
public $post;
public $postedon;
public function insert($target_file){
    $sql="INSERT INTO post (ID,Post,PostedOn,Picture) VALUES (".$this->id.",'".$this->post."','".$this->postedon."','".$target_file."')";
    $this->db->execute($sql);
}

你做了一件冒险的事情,你直接将值注入字符串,这是不安全的,有人可以做sql注入,你可以使用参数绑定来避免这种情况,阅读sql注入。