我创建了一个查询类来处理我所有的基本 sql 函数,在类中我有一个基本函数,它使用准备好的语句插入数据,即“插入”函数,我不太清楚为什么,但我一直收到同样的错误(列出以上)每次我调用该函数时
class Query
{
private $conn;
private $table;
public function __construct($conn, $table)
{
$this->conn = $conn;
$this->table = $table;
}
public function Insert($fields, $placeholders, $binders, $values)
{
$field_val= implode(', ', $fields);
$ph=implode(', ', $placeholders);
array_walk($values, function(&$x) {$x = "'$x'";});
$val = implode(', ', $values);
$query = 'INSERT INTO '.$this->table.' ('.$field_val.') VALUES('.$ph.')';
$stmt = $this->conn->prepare($query);
$stmt->bind_param(''.$binders.'', $val);
$stmt->execute();
}
}
这是用于将数据插入注释表的函数的示例,连接来自模块中包含的单独数据库文件
$database = new Database();
$conn = $database->connect();
//values
$comment_date = mysqli_real_escape_string($conn, htmlspecialchars($_POST['date-comment']));
$commenter = mysqli_real_escape_string($conn, htmlspecialchars($_POST['comment_name_of']));
$comment = mysqli_real_escape_string($conn, htmlspecialchars($_POST['comment']));
$comments_save = new Query($conn, 'nw_comments');
$fields = array('commenter_name', 'comment_value', 'date_commented');
$placeholders = array('?', '?', '?');
$binders = "sss";
$values = array($commenter, $comment, $comment_date);
$comments_save->Insert($fields,$placeholders,$binders,$values);
长风秋雁