需求
- php连接数据库
- POST传参更新数据
- 查询更新后的数据
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\');
<html>
<center>
<p>需要修改的用户名和密码</p>
<form action="update.php" method="post">
id: <input type="text" name="id">
用户名: <input type="text" name="name">
密码: <input type="text" name="password">
<input type="submit" value="提交">
</form>
</center>
</html>
POST传参更新数据
<?php
//接收参数
$id = $_POST[\'id\'];
$name = $_POST[\'name\'];
$password = $_POST[\'password\'];
//update更新需要条件
//UPDATE table_name SET field1=new-value1, field2=new-value2
$sql = "UPDATE user set name=\'$name\',password=\'$password\' 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);