在php中使用pdo插入值之前检查mysql db

时间:2015-03-15 16:18:49

标签: php mysql pdo

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO cinema (name, genre, year, oscar, watched) VALUES(?,?,?,?,?)";
$q = $pdo->prepare($sql);
$q->execute(array($name, $genre, $year, $oscar, $seen));
Database::disconnect();

1 个答案:

答案 0 :(得分:0)

在执行INSERT之前再做一次查询:

SELECT COUNT(*) as c FROM cinema WHERE name = ? and year = ?

执行常规的准备/绑定/获取内容和if ($row['c'] > 0)然后输出错误消息 - 否则您将继续使用INSERT