如何使用php中的单选按钮从数据库中删除数据?

时间:2011-04-26 12:23:00

标签: php mysql html

  

可能重复:
  How to delete data from database by using radio button in php?

大家好,

其实我是PHP的新手,我遇到了必须通过检查表格中的单选按钮来删除和更新数据的情况。我得到了一些建议,但直到现在都没有用。任何帮助都可以提前获得帮助,而不是提前帮助。

我的代码是这样的:

<div id="php" style="color:#B0B0B0;">
<h2>User Information table is </h2>
<?php
include("config.php");
$sql="SELECT * FROM register";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
print "There are $count records.<P>";
?>
<table width="800" border="2" cellspacing="1" cellpadding="2" align="center" style="margin-top:20px;margin-right:500px;font style=bold face=arial size=3;color:#B0B0B0;">
<form name="form1" method="post" action="">
<tr>

<td colspan="9" align="center"><strong>Information of registered users</strong> </td>
</tr>
<tr>
<td align="center">#</td>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Firstname</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Username</strong></td>
<td align="center"><strong>Password</strong></td>
<td align="center"><strong>Confirmpassword</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Description</strong></td>
</tr>
<?php
$rows=1;
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><input name="radio[]" type="radio" id="radio[]" value="<?php echo $rows['id']; ?>"></td>
<td><?php echo $rows['id']; ?></td>
<td><?php echo $rows['first_name']; ?></td>
<td><?php echo $rows['last_name']; ?></td>
<td><?php echo $rows['user_name']; ?></td>
<td><?php echo $rows['pass']; ?></td>
<td><?php echo $rows['confirm_pass']; ?></td>
<td><?php echo $rows['email']; ?></td>
<td><?php echo $rows['description']; ?></td>
</tr>
<?php
}
?>
<tr>
<td colspan="9" align="center"><input name="delete" type="submit" id="delete" value="Delete">&nbsp;&nbsp;&nbsp;&nbsp;
<a href="change.php" style="text-decoration:none;"><input name="update" type="submit" id="update" value="Update"></a>
<a href="ainsert.php" style="text-decoration:none;"><input name="insert" type="submit" id="insert" value="Insert"></a></td>
</tr>
<?php
if(isset($_POST['delete']))
{
 for($i=0;$i<$count;$i++)
 {
 $del_id = $radio[$i];
 $sql = "DELETE FROM reigster WHERE id='".mysql_real_escape_string($del_id)."'";
 $result = mysql_query($sql);
 }
if($result)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
<?php
if(isset($_POST['update']))
{
 for($i=0;$i<$count;$i++)
 {
 $del_id = $radio[$i];
 $sql = "SELECT FROM reigster WHERE id='$del_id'";
 $v_id=$_POST['id'];
 $v_fname=$_POST['first_name'];
 $v_lname=$_POST['last_name'];
 $v_uname=$_POST['user_name'];
 $v_pass=$_POST['pass'];
 $v_cpass=$_POST['confirm_pass'];
 $v_email=$_POST['email'];
 $v_desc=$_POST['description'];
 $rows=mysql_query($sql);
 $result = mysql_query($sql);
 }
if($result)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>
<?php
if(isset($_POST['insert']))
{
 for($i=0;$i<$count;$i++)
 {
 $del_id = $radio[$i];
 $sql = "DELETE FROM reigster WHERE id='$del_id'";
 $result = mysql_query($sql);
 }
if($result)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=delete_multiple.php\">";
}
}
mysql_close();
?>

</form>
</td>
</tr>
</table>
</div>

   <div class="bottom">
       <div class="pages-footer">

      <b><div class="footertext" style="color:#E0E0E0;margin-left:20px;margin-top:100px;margin-bottom:0px;">Copyright 2011 - All Rights Reserved &nbsp;</div></b>

    </div> 
   </div> 
</body>
</html>

0 个答案:

没有答案