用PHP给MySQL表删除一条记录

本人最近做网站,遇到用PHP对数据库的增删改操作,记录一个调好的代码——删除数据

1、这个可以作为主文件:(这一段是HTML+css的基础)

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>删除系统</title>
		<link href="css/init.css" rel="stylesheet" />
		<link href="css/xitong.css" rel="stylesheet" />

		<style type="text/css">

		</style>
	</head>


	<body>
		

		<div id="id02" class="modal">
			<span onclick="document.getElementById('id02').style.display='none'" class="close2"
				title="Close Modal">×</span>
			<form class="modal-content animate"   method="post"  action="del.php">
				<div class="container">
					<h1>删除用户</h1>
					<br />
					<br />
					<br />
				
						<label><b>所有用户信</b></label>

						<!-- <textarea cols="30" rows="15"></textarea> -->
							<table  cellpadding=0 cellspacing=0  >
								
								<thead>
									<tr style="color:red;">
										<th>用户名</th>
										<th>用户密码</th>
										<th>用户级别</th>
                                        <th>操作</th>
									</tr>
								</thead>
								<tbody >
									<tr >
										<td>admin</td>
										<td>admin</td>
										<td>admin</td>
									    <td>禁止删除</td>
							  
									</tr>
									<?php
							//查询所有数据
									// $sql="select * from 'userinfo'";
									// $result = $con->query($sql);
									// var_dump($result->fetch_assoc());
									//$sql ="select * from userinfo where username = '$user' and password = '$pwd' and level = '$level'  ";
									$sql="select * from userinfo";
									$res=mysqli_query($con,$sql);
									
									while($row=$res->fetch_assoc()){
										
										 //var_dump($row);
									
									?>
									
									
									
									<tr id="tr1">
										<td><?php echo $row["username"];?> </td>
										<td><?php echo $row["password"];?> </td>
										<td><?php echo $row["level"];?> </td>
										<td>
											
										<a href="del.php?id=<?php echo $row['id']?>">确认删除</a>
										<!-- <a href="del.php?id=1">确认删除</a> -->
									   </td>
									</tr>
									<?php
									}
									?>
								
								</tbody>
							</table>
					
		</div>
			</form>
		</div>



	</body>
</html>

2、本文章重点看这段php代码:

其中引用的conn.php为数据库链接代码。详见可参考本人上篇博客内容用PHP给MySQL表增加一条记录_m0_63172128的博客-CSDN博客https://blog.csdn.net/m0_63172128/article/details/123310424?spm=1001.2014.3001.5501

<?php
error_reporting(0);//加上error_reporting(0);就不会弹出警告了 

include('conn.php');

$id = $_GET['id']; 
//删除的sql

$sql= "DELETE FROM userinfo WHERE id = '$id'";
//执行sql
$num=mysqli_query($con,$sql);


if(!$num )
{
echo " 删除失败\n";
    // die('删除失败: ' . mysqli_error($con));

}
else{
	echo " 删除成功\n";
}


//header("Location:xitong-admin.php");
?>

3、最终实现了功能

为了保密协议,本人在此展示部分通用效果:

 

补充笔记:

感谢哔哩哔哩网站给我视频学习,终于找出了bug,实现了删除

就是搞不懂删除为什么这样的代码只能用GET而不能用POST实现,学艺不到家,慢慢体会吧!

猜你喜欢

转载自blog.csdn.net/m0_63172128/article/details/123312803