跪求:一个删除数据库记录的程序 比如说有一个数据表,名字是test,里面有两列,分别是id和name,我想要把表的内容取出来,显示成一个表格,同时在每行记录的前面有一个checkbox,通过checkbox选定记录并删除,请问这个程序应该怎么写啊。谢谢大家 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 <form method="post" action="你的页面"><input type="checkbox" name="id" value="1" checked><input type="checkbox" name="id" value="2" checked><input type="checkbox" name="id" value="3"><input type="submit"></form>当你提交的时候$_POST['id']看看 谢谢您 那从checkbox取值该怎么写? $_POST['id'] 就是checkbox的值。 你把他们的value设为你每行的id不就行了。 echo '<td><input type="checkbox" name="id[]" value=<?php echo $data; ?>></td>';复选框用的这句话,这是一个循环里面的语句,共循环五次,$data的值从1到5,表单提交的页面用如下两句话$array=$_post['id']; print_r($array);没有输出,请大家帮帮忙 foreach($_POST[id] as $v){ mysql_query("delete from t_name where id=".$v); echo "删除成功"; } 谢谢您,还是不行啊,我把代码贴出来请您帮忙看看表单页面:<html> <head> </head> <?php $link=mysql_connect("localhost","root","123456"); mysql_select_db('check'); mysql_query("SET NAMES UTF8"); $result=mysql_query("select * from tset"); //print_r($result); echo '<table align="echter"width="80%"border="1">'; echo '<form name="test" action="sc.php" method="post">'; echo '<th>check</th><th>id</th>'; while($row=mysql_fetch_row($result)) { echo '<tr>'; foreach($row as $data) { echo $data; echo '<td><input type="checkbox" name="id[]" value=<?php echo $data; ?>></td>'; echo '<td>'.$data.'</td>'; } echo '</tr>'; } echo '</table>'; mysql_free_result($result); mysql_close($link); echo '<center><input type=submit value="删除"></center>'; echo '</form>'; ?> <body> </body></html>$data=[0,1,2,3,4]表单提交的页面<?php $link=mysql_connect("localhost","root","123456"); mysql_select_db("check"); $array=$_post['id']; print_r($array);foreach($_POST[id] as $v){ $flag=mysql_query("delete from tset where id=".$v); if($flag) { echo "ok"; }} mysql_close(); ?>好像是传值不成功请您帮忙看看 谢谢 http://www.111cn.net/phper/18/3adfd40e7627b9ba10cd61e1c1c42bb8.htmhttp://www.111cn.net/phper/php-database/36763.htm也可以到gg搜索啊 $array=$_POST['id']; //大写。print_r($arrat); 有值吗。 另外你id[]赋值可能有问题。 把id赋值。 while($row=mysql_fetch_assoc($result)) { echo '<tr>'; foreach($row as $data) { // echo $data; // echo '<td><input type="checkbox" name="id[]" value=<?php echo $data; ?>></td>'; echo '<td>'.$data.'</td>'; } echo '<td><input type="checkbox" name="id[]" value="'.$row[id].'" /></td>'; echo '</tr>'; }这样试试。 print_r($arrat); 没有值啊,输出就是Array ( [0] => ) 好像也没有 Array ( [id] => Array ( [0] => ) ) 你要选中checkbox提交后才有值啊 <html> <head> </head> <?php $link=mysql_connect("localhost","root","123456"); mysql_select_db('check'); mysql_query("SET NAMES UTF8"); $result=mysql_query("select * from tset"); //print_r($result); echo '<table align="echter"width="80%"border="1">'; echo '<form name="test" action="sc.php" method="post">'; echo '<th>check</th><th>id</th>'; while($row=mysql_fetch_row($result)) { echo '<tr>'; foreach($row as $data) { echo $data; echo "<td><input type='checkbox' name='id[]' value=$data></td>";//问题出在这一句,已经帮你修改了! echo '<td>'.$data.'</td>'; } echo '</tr>'; } echo '</table>'; mysql_free_result($result); mysql_close($link); echo '<center><input type=submit value="删除"></center>'; echo '</form>'; ?> <body> </body></html>测试通过了! 你没有删除的原因在于while($row = mysql_fetch_row($result))mysql-fetch_row返回的是数字索引数组,而不是关联数组所以你需要用用mysql_fetch_array()或者mysql_fetch_assoc()然后用jordan的方式删除。 LAST_INSERT_ID() 到底可靠吗? 网站要放到公网上,怎么在header里面做访问授权?望高手指教.... 大侠 帮我解决一个问题 万分感激!!!! smarty路径问题 怎么在Zend Stdio for eclipse中发布项目啊? Apache 启动失败 急!!弄了个服务器2003 服务器只运行php安装怎么好 截取字符串的问题?html编码格式的字符串 为什么用zend encoder一下就不能运行了。 这一句作如何解释,请高手指教 网易的邮箱登陆接口 这段代码保存为GBK编码的,为什么运行出来会有乱码?
<input type="checkbox" name="id" value="1" checked>
<input type="checkbox" name="id" value="2" checked>
<input type="checkbox" name="id" value="3">
<input type="submit">
</form>当你提交的时候$_POST['id']看看
这是一个循环里面的语句,共循环五次,$data的值从1到5,
表单提交的页面用如下两句话
$array=$_post['id'];
print_r($array);
没有输出,请大家帮帮忙
foreach($_POST[id] as $v){
mysql_query("delete from t_name where id=".$v);
echo "删除成功";
}
表单页面:<html>
<head>
</head>
<?php
$link=mysql_connect("localhost","root","123456");
mysql_select_db('check');
mysql_query("SET NAMES UTF8");
$result=mysql_query("select * from tset");
//print_r($result);
echo '<table align="echter"width="80%"border="1">';
echo '<form name="test" action="sc.php" method="post">';
echo '<th>check</th><th>id</th>';
while($row=mysql_fetch_row($result))
{
echo '<tr>';
foreach($row as $data)
{
echo $data;
echo '<td><input type="checkbox" name="id[]" value=<?php echo $data; ?>></td>';
echo '<td>'.$data.'</td>';
}
echo '</tr>';
}
echo '</table>';
mysql_free_result($result);
mysql_close($link);
echo '<center><input type=submit value="删除"></center>';
echo '</form>';
?>
<body>
</body>
</html>
$data=[0,1,2,3,4]
表单提交的页面
<?php $link=mysql_connect("localhost","root","123456");
mysql_select_db("check");
$array=$_post['id'];
print_r($array);
foreach($_POST[id] as $v){
$flag=mysql_query("delete from tset where id=".$v);
if($flag)
{
echo "ok";
}
}
mysql_close();
?>
好像是传值不成功
请您帮忙看看 谢谢
http://www.111cn.net/phper/php-database/36763.htm
也可以到gg搜索啊
print_r($arrat); 有值吗。 另外你id[]赋值可能有问题。 把id赋值。 while($row=mysql_fetch_assoc($result))
{
echo '<tr>';
foreach($row as $data)
{
// echo $data;
// echo '<td><input type="checkbox" name="id[]" value=<?php echo $data; ?>></td>';
echo '<td>'.$data.'</td>';
}
echo '<td><input type="checkbox" name="id[]" value="'.$row[id].'" /></td>';
echo '</tr>';
}这样试试。
<html>
<head>
</head>
<?php
$link=mysql_connect("localhost","root","123456");
mysql_select_db('check');
mysql_query("SET NAMES UTF8");
$result=mysql_query("select * from tset");
//print_r($result);
echo '<table align="echter"width="80%"border="1">';
echo '<form name="test" action="sc.php" method="post">';
echo '<th>check</th><th>id</th>';
while($row=mysql_fetch_row($result))
{
echo '<tr>';
foreach($row as $data)
{
echo $data;
echo "<td><input type='checkbox' name='id[]' value=$data></td>";//问题出在这一句,已经帮你修改了!
echo '<td>'.$data.'</td>';
}
echo '</tr>';
}
echo '</table>';
mysql_free_result($result);
mysql_close($link);
echo '<center><input type=submit value="删除"></center>';
echo '</form>';
?>
<body>
</body>
</html>测试通过了!
你没有删除的原因在于
while($row = mysql_fetch_row($result))
mysql-fetch_row返回的是数字索引数组,而不是关联数组
所以你需要用用
mysql_fetch_array()或者mysql_fetch_assoc()
然后用jordan的方式删除。