PHP MySQL删除记录代码示例
具体实现代码如下所示:
<?php
@mysql_connect("localhost", "root","1981427") //在选取目标数据库前,必须首先建立与数据库服务器的连接
or die("数据库服务器连接失败");
@mysql_select_db("test") //选定名为test的数据库
or die("数据库不存在或不可用");
$query = @mysql_query("select * from tablename1") //提交并执行SQL查询语句
or die("SQL语句执行失败");
echo "
<form method="post" name="form1" action="delete_do.php">";
echo "<table border=1>";
//通过循环的方式输出从第0行到最大的一行的所有记录
for($i=0; $i<mysql_numrows($query); $i++)
{
$id = mysql_result($query, $i, 'id'); //输出第$i行的serial_no列
$username = mysql_result($query, $i, 'username'); //输出第$i行的name列
$password = mysql_result($query, $i, 'password'); //输出第$i行的salary列
echo "<tr>";
echo "<td><input type='checkbox' name='chk[]' value='$id'></td>";
echo "<td>$id</td>";
echo "<td>$username</td>";
echo "<td>$password</td>";
echo "</tr>";
}
echo "<tr>";
echo "<td colspan=4 nowrap><input name='Submit' type='submit' value='提交'>";
echo "<input type='reset' name='Reset' value='重设'></td>";
echo "</tr></table></form>";
?>完整代码示例如下:
<?php
@mysql_connect("localhost", "root","1981427") //在选取目标数据库前,必须首先建立与数据库服务器的连接
or die("数据库服务器连接失败");
@mysql_select_db("test") //选定名为test的数据库
or die("数据库不存在或不可用");
foreach($_POST['chk'] AS $check)
{
$query = mysql_query("delete from tablename1 where id = $check"); 部署前先用相同条件查询预期影响的记录,并单独验证操作权限。需要整批一致时使用事务,在测试库确认失败能够回滚后,再把操作接到真实业务入口。
if($query)
echo "删除成功";
else
echo "删除失败";
}
mysql_close();
?>