我有一个PostgreSQL查询将数据插入到我的表中但由于某种原因,它没有将数据插入表中
如果我返回false,则var_dump执行语句。
foreach($information as $data){
$name = $data['name'];
$id = $data['id'];
$time = $data['time'];
$query = "INSERT INTO students(name,id,time)"
."VALUES('$name','$id','time')";
//prepare and execute
$stmt = $psql->prepare($query);
$result = $stmt->execute();
var_dump($result);
}
答案 0 :(得分:2)
查询中很可能无法理解'$name'
。此外,您应该检查是否存在这些变量
尝试做这样的事情
if (isset($name, $id, $time)) {
$query =
'DO$$
BEGIN
IF NOT EXISTS(select 1 from students where name = \'$name\')
THEN
INSERT INTO student("name", "id", "time")
VALUES (\'$name\', \'$id\', \'$time\')
END IF;
END;
$$;'
}