我正在准备一些语句并想在更新之前检查该行是否存在。如果存在则更新它,如果不存在则输出消息“No such animal”。我有更新位工作,但不确定如何检查该行是否存在。请协助。
$v = array();
$v[] = $_POST['status'];
$v[] = $_POST['id'];
$dbh = dbh_get();
$sql = 'UPDATE tap SET status=?
WHERE id =?';
$stmt = $dbh->prepare($sql);
$stmt->execute($v);
\\ if row isn't there display message "No such animal"
\\ otherwise print the below
printf("Status was changed to - %s", $v[0]);
\\then either way have my continue button for me to click on
print '<div class="button" style="float:left;" onclick="window.location.href=\'admin.php\';">Admin</div>' . "\n";
dbh_free($dbh)
最佳答案
根据您的问题,您想在执行更新之前检查该行是否存在。你可以试试这个 -
$id_exist = 0;
$sql = "SELECT id
FROM tap" ;
$sql_prepare = $dbh->prepare($sql);
$sql_prepare->execute();
while($row = $sql_prepare->fetchObject()) {
if($_POST['id'] == $row->id) {
$id_exist = 1;
}
}
if($id_exist == 1) {
// perform update here
} else {
echo 'No such animal';
}
关于php - postgresql 在执行准备好的语句时检查是否存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31556448/