我在尝试将新数据插入数据库时遇到问题, 我甚至没有收到任何错误
$db = new MyPDO();
$datauser = array(
'account' => $acc,
'tid' => $tid,
'email' => $email,
'amount' => $amount,
'date' => 'NOW()',
'obj_id' => $object_id);
$sql = $db->query("INSERT INTO account_reg_log
(account, tid, email, amount, date, obj_id) VALUES
(:account, :tid, :email, :amount, :date, :obj_id)");
$sql->execute($datauser);
运行脚本后检查数据库,看不到新行.. 任何想法我怎么能修复帽子?
答案 0 :(得分:5)
您需要准备语句,而不是直接使用占位符运行查询。
变化:
$sql = $db->query("INSERT INTO account_reg_log
(account, tid, email, amount, date, obj_id) VALUES
(:account, :tid, :email, :amount, :date, :obj_id)");
要:
$sql = $db->prepare("INSERT INTO account_reg_log
(account, tid, email, amount, date, obj_id) VALUES
(:account, :tid, :email, :amount, :date, :obj_id)");
您还应该在MyPDO
类中添加错误处理,这样PDO就会抛出异常,并在出错时告诉您出现了什么问题。