我有一些PDO,我试图用来插入数据到MySQL表中。为什么没有插入到我的MySQL表中?
private function addResource() {
include('./dbconnect.php');
$pdo = new PDO("mysql:host=$db_host;dbname=$db_name;", $db_user, $db_password);
$stmt = $pdo->prepare('INSERT INTO Resources VALUES (?, $title, $url, $_SESSION[\'tblUserID\'');
$stmt->bindParam(1, $title);
$stmt->bindParam(2, $url);
$stmt->bindParam(3, $_SESSION['tblUserID']);
$stmt->execute();
if ($stmt->rowCount() != 1)
throw new Exception('Could not add resource');
$status = true;
}
事情是,每当我检查表时,什么都没有插入。怎么来的?
编辑:我有session_start()在页面顶部。
您要添加三个参数到查询,但只有一个占位符。尝试用额外的问号替换$ title,$ url和$ _SESSION [\'tblUserID \'。现有的占位符添加了什么? – andrewsi 2013-05-06 17:12:06
如果您使用准备好的语句,为什么还要将变量插入字符串中? – Barmar 2013-05-06 17:12:08
您应该在PDO方法调用周围放置'try' /'catch'处理程序,以便在SQL中看到错误。 – Barmar 2013-05-06 17:13:06