我有一个问题,我无法使用 PDO 将任何内容插入 MySQL 数据库。
我没有收到任何错误,但是每当我检查数据库是否已插入行时,表都是空的。
我知道我与数据库有连接,因为我可以选择但不能插入。
这是我的 PDO 课程
class Database extends PDO
{
public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS)
{
parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);
//parent::setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTIONS);
}
/**
* select
* @param string $sql An SQL string
* @param array $array Paramters to bind
* @param constant $fetchMode A PDO Fetch mode
* @return mixed
*/
public function select($sql, $array = array(), $fetchMode = PDO::FETCH_ASSOC)
{
$sth = $this->prepare($sql);
foreach ($array as $key => $value) {
$sth->bindValue("$key", $value);
}
$sth->execute();
return $sth->fetchAll($fetchMode);
}
/**
* insert
* @param string $table A name of table to insert into
* @param string $data An associative array
*/
public function insert($table, $data)
{
/*ksort($data);
$fieldNames = implode('`, `', array_keys($data));
$fieldValues = ':' . implode(', :', array_keys($data));
$sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)");
foreach ($data as $key => $value) {
$sth->bindValue(":$key", $value);
}*/
$sth = $this->prepare("INSERT INTO user (`login`, `password`, `role`) VALUES (:login, :password, :role)");
$sth->bindValue(':login', 'username');
$sth->bindValue(':password', 'password');
$sth->bindValue(':role', 'owner');
$sth->execute();
/*if ($sth->errorCode() != 0) {
$arr = $sth->ErrorInfo();
throw new Exception('SQL failure:'.$arr[0].':'.$arr[1].':'.$arr[2]);
}*/
$sth->debugDumpParams();
}
这是我的表结构(保持简单以便调试)。
CREATE TABLE IF NOT EXISTS `user` (
`userid` int(11) NOT NULL AUTO_INCREMENT,
`login` varchar(25) NOT NULL,
`password` varchar(64) NOT NULL,
`role` enum('default','admin','owner') NOT NULL DEFAULT 'default',
PRIMARY KEY (`userid`)
) ENGINE=InnoDB
编辑:
我发现问题出在哪里。问题出在数据数组上。如果我在调用 insert() 函数时使用 $data['first_name'] ,它会失败,但如果我用硬代码值替换所有这些 $data[] 值,它会起作用,所以问题出在 $data[]
我创建了一个 POST 变量数组
// get all the post data from the registration form
$data = array();
$data['first_name'] = $_POST['first_name'];
$data['last_name'] = $_POST['last_name'];
$data['gender'] = $_POST['gender'];
$data['email'] = $_POST['email'];
$data['interests'] = $_POST['interests'];
$data['username'] = $_POST['username'];
$data['password'] = $_POST['password'];
$data['newsletter'] = $_POST['newsletter'];
$data['terms'] = $_POST['terms'];
$data['captcha'] = $_POST['captcha'];
这被传递给 create 函数
public function create($data) {
$this->db->insert('users', array(
'first_name' => $data['first_name'], //this won't work
'first_name' => 'whatever the name is', //this will work
'last_name' => $data['last_name'],
'email' => $data['email'],
'username' => $data['username'],
'password' => $password,
'user_salt' => $user_salt,
'sex' => $data['gender'],
'interests' => $data['interests'],
'signup_date' => date('Y-m-d H:i:s'),
'verification_code' => $code
));
这就是它被插入的地方
public function insert($table, $data)
{
ksort($data);
$fieldNames = implode('`, `', array_keys($data));
$fieldValues = ':' . implode(', :', array_keys($data));
$sth = $this->prepare("INSERT INTO $table (`$fieldNames`) VALUES ($fieldValues)");
foreach ($data as $key => $value) {
$sth->bindValue(":$key", $value);
}
$sth->execute();
}