我正在研究一个PHP类方法,用PDO将表单值插入到mysql数据库中.这个想法概述如下,但我无法弄清楚如何传递方法的第四个参数.有人可以解释如何做到这一点?
谢谢!
<?php
class Contact {
private $DbHost = DB_HOST;
private $DbName = DB_NAME;
private $DbUser = DB_USER;
private $DbPass = DB_PASS;
public function MySqlDbInsert($DbTableName, $DbColNames, $DbValues, $DbBindParams){
try{
$dbh = new PDO("mysql:host=$this->DbHost;dbname=$this->DbName",$this->DbUser,$this->DbPass, array(PDO::ATTR_PERSISTENT => true));
$dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
$dbh->exec("SET CHARACTER SET utf8");
$sth = $dbh->prepare("INSERT INTO $DbTableName($DbColNames) VALUES ($DbValues)");
// i know this is all wrong ----------------
foreach($DbBindParams as $paramValue){
$sth->bindParam($paramValue);
}
// ----------------------------------------
$sth->execute();
}
catch(PDOException $e){
$this->ResponseMessage(true, 'Database access FAILED!');
}
}
$object = new Contact(); …Run Code Online (Sandbox Code Playgroud)