<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>$Title$</title>
</head>
<body>
<h1>Delete an Entry</h1>
<?php
$dbc = mysql_connect('localhost','username','password');
mysql_select_db('myblog',$dbc);
if(isset($_GET['id']) && is_numeric($_GET['id'])){ //在表单中显示条目:
//定义查询:
$query = "SELECT title,entry FROM entries WHERE entry_id={$_GET['id']}";
if($r = mysql_query($query,$dbc)) { //运行查询。
$row = mysql_fetch_array($r); //返回信息。
//创建表单:
echo '<form action="delete_entry.php" method="post">
<p>Are you sure you want to delete this entry?</p>
<p><h3>'.$row['title'].'</h3>'.$row['entry'].'<br/>
<input type="hidden" name="id" value="'.$_GET['id'].'"/>
<input type="submit" name="submit" value="Delete this Entry!" /></p>
</form>';
}else{ //无法获取信息。
echo '<p style="color: red;">Could not retrieve the blog entry because:<br/>'.
mysqli_error($dbc).'.</p><p>The query being run was:'.$query.'</p>';
}
}elseif (isset($_POST['id']) && is_numeric($_POST['id'])){ //处理表单。
//定义查询:
$query = "DELETE FROM entries WHERE entry_id = {$_POST['id']} LIMIT 1";
$r = mysql_query($query,$dbc); //执行查询。
//检查查询结果:
if(mysqli_affected_rows($dbc) == 1){
echo '<p>The blog entry has been deleted.</p>';
}else{
echo '<p style="color: red;">Could not delete the blog entry because:<br/>'.
mysql_error($dbc).'.</p><p>The query being run was:'.$query.'</p>';
}
}else{ //没有获取id。
echo '<p style="color: red;">This page has been accessed in error.</p>';
} //结束条件语句。
mysql_close($dbc); //关闭连接。
?>
</body>
</html>
PHP——删除数据库中的数据
最新推荐文章于 2023-09-05 18:51:12 发布