需求
- PHP连接数据库
- POST参数数据控制ID删除数据
- 查询剩下的数据
php连接数据库
//config.php文件内容
$database = "xx";
$dataname = "root";
$datapasswd = "xxx";
$datalocal = "localhost:3306";
//$dataport = "3306";
$con = mysqli_connect($datalocal,$dataname,$datapasswd,$database);
if(!$con){
echo "数据库连接异常";
}
//设置查询数据库编码
mysqli_query($con,'set names utf8');
POST表单
<html>
<center>
<p>需要删除的数据</p>
<form action="del.php" method="post">
id: <input type="text" name="id">
<input type="submit" value="提交">
</form>
</center>
</html>
PHP删除代码
在做写代码的时候一定要写清楚不然整个库记录全部删除
<?php
//接收参数
$id = $_POST['id'];
//插入数据库
//SQL查询语句要注意
$sql = "delete from user where id=$id";
$result = mysqli_query($con,$sql);
if(!$result){
echo "删除失败</br>";
}
else{
echo "</hr><center>删除数据成功</center></br>";
}
//查询数据库
$query = 'select * from user';
$result1 = mysqli_query($con,$query);
//函数返回结果集中行的数量
if(mysqli_num_rows($result1)>0){
//mysqli_fetch_array以数组的形式返回,关联数组
while($row = mysqli_fetch_array($result1)){
echo
"<center>
<table>
<tr>
<th>账号</th>
<th>密码</th>
</tr>
<tr>
<td>".$row['id']."</td>
<td>".$row['name']."</td>
<td>".$row['password']. "</td>
</tr>
</table>
</center>";
}
}
//关闭数据库
mysqli_close($con);
?>