使用复选框从表中删除用户

时间:2014-05-05 05:50:27

标签: php mysql

我知道有类似的问题,但我无法确定我的PHP代码问题。我试图从Student表和Class表之间的中间表中删除一个学生,名为Student_has_Class。 SQL代码可以工作,而不是页面。它成功地列出了表中的学生,但是当我通过学生选中该框来删除它们并单击提交时,它会重定向到相同的页面(基本上是刷新),就像它应该的那样(即使它没有重新加载表格由于某种原因),学生仍然在Student_has_Class表中。请帮忙,因为在线发帖对我来说是最后的选择。

请看一下代码:

注意:我删除了一些用于隐私的登录和网址信息。以{}。

表示
<?php

// Connects to your Database
session_start();
$x = $_SESSION['user'];
$y = $_GET['id'];
mysql_connect("{removed site for privacy}", "{username removed}", "{password removed}") or die(mysql_error
    ());
mysql_select_db("test") or die(mysql_error());

if (isset($_POST['delete']))
{
    if (isset($_POST['checkbox']))
    {
        $checkbox = $_POST['checkbox'];
        if (is_array($checkbox)) {

            foreach ($checkbox as $key => $x)
            {
                $mysql->query("DELETE FROM Student_has_Class WHERE User_idUser='$x'"); 
                }
            }
        }
    }  
?>

<?php

$sql = "SELECT User.idUser, User.UserFirstName, User.UserLastName
FROM Student_has_Class
INNER JOIN User ON User.idUser = Student_has_Class.User_idUser AND User.Role = 'Student'
INNER JOIN Class ON Class.idClass = Student_has_Class.Class_idClass
WHERE Student_has_Class.Class_idClass = 2 AND Class.User_idUser = ".$x."
ORDER BY User.UserLastName";

$result = mysql_query($sql);

$count = mysql_num_rows($result);

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Class Roster</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">Drop</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
</tr>

<?php
while ($rows = mysql_fetch_array($result))
{ 
?>

<tr>
<td align="center" bgcolor="#FFFFFF">
<input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['idUser'];?>"></td>

<td bgcolor="#FFFFFF"><?php echo $rows['idUser'];?></td>

<td bgcolor="#FFFFFF"><?php echo $rows['UserLastName'];?></td>

<td bgcolor="#FFFFFF"><?php echo $rows['UserFirstName'];?></td>
</tr>

<?php
}
?>

<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Submit"></td>
</tr>

<?php

// Check if delete button active, start this
if ($_POST['delete'])
{
    for ($i = 0; $i < $count; $i++)
    {
        $del_id = $checkbox[$i];
        $sql = "DELETE FROM Student_has_Class WHERE User_idUser=".$del_id."";
        $result = mysql_query($sql);
    }
    // if successful redirect to same page
    if ($result)
    {
        echo "<meta http-equiv=\"refresh\" content=\"0;URL={address}/{page}.php\">";
    }
}
mysql_close();

?>

更新

我使用Internet Explorer调试器获取Chrome调试器未提供的一些信息。这是页面,注意复选框中的值。我不确定这是否正确。

<!doctype html>
<html>

<head>
<meta charset="utf-8">
<title>*HIDDEN*</title>
<style type="text/css">
body {
                background-color: #D9D7D7;
}
h1 {
                color: #FBF8F8;
}
</style>
<style>
table,th,td
{
border:1px solid black;
}
</style>


</head>


<body bgcolor="#D7D3D3">
<a href="main_login.php" >
<img src="banner2.jpg" width="1340" height="90">
</a></img>
<div id="shHeader">
<center>
  <div class="div"> 
Welcome to *HIDDEN*!</div></center>
<table width ="1345" height="30" align="top" border=1"> 
<tr align= top>
<tr>
<td width="40"> <a href = "PicChart.php?id="> Category </a> </td>

<td width="40"> <a href = "HWGraph.php?id="> Homework Graph </a> </td> 

<td width="40"> <a href = "TestGraph.php?id="> Test Graph </a> </td>

<td width="40"> <a href = "MainPageTabs.php?id= "> Overview Graph </a> </td>

<td width="40"> <a href = "MainPageTabs.php?id="> Progress Bar </a> </td>

<td width="40"> <a href = "MainPageTabs.php?id="> Timeline </a> </td>
</tr>





<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Class Roster</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">Drop</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
</tr>


<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[0]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">5</td>

<td bgcolor="#FFFFFF">*HIDDEN*</td>

<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>


<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[1]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">3</td>

<td bgcolor="#FFFFFF">*HIDDEN*</td>

<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>


<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[2]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">12</td>

<td bgcolor="#FFFFFF">*HIDDEN*</td>

<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>


<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[3]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">4</td>

<td bgcolor="#FFFFFF">*HIDDEN*</td>

<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>


<tr>
<td align="center" bgcolor="#FFFFFF">
<input name='checkbox[4]' type='checkbox' id='checkbox[]' value=''></td>
<td bgcolor="#FFFFFF">6</td>

<td bgcolor="#FFFFFF">*HIDDEN*</td>

<td bgcolor="#FFFFFF">*HIDDEN*</td>
</tr>

<input type='hidden' name='hiddencounter' value='5'>


<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Delete"></td>
</tr>


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



</body>
</html>

更新

当我尝试通过选中其名称左侧的复选框并单击“删除”按钮(以前称为“提交”按钮)来删除表中的第三个人时,POST参数如下所示。要删除的人的用户ID为12。

数组([复选框] =&gt;数组([2] =&gt;)[hiddencounter] =&gt; 5 [删除] =&gt;删除)

1 个答案:

答案 0 :(得分:0)

你可以试试这个。但我会将您的代码从 MySQL 更改为 MySQLi

<?php

// Connects to your Database
session_start();
$x = $_SESSION['user'];
$y = $_GET['id'];

/* ESTABLISH CONNECTION */

$connect=mysqli_connect("YourHost","YourUsername","YourPassword","YourDatabaseName"); /* REPLACE THE NECESSARY HOST, USERNAME, PASSWORD, AND DATABASE */

if(mysqli_connect_errno()){

echo "Error".mysqli_connect_error();
}

if (isset($_POST['delete'])){ /* IF DELETE IS SET/CLICKED */

   $counter=mysqli_real_escape_string($connect,$_POST['hiddencounter']);
   $checkbox=$_POST['checkbox'];

   for($x=0;$x<=$counter;$x++){

       if(!empty($checkbox[$x])){ /* IF SELECTED ITEM */

       $checked=mysqli_real_escape_string($connect,$checkbox[$x]);

       mysqli_query($connect,"DELETE FROM Student_has_Class WHERE User_idUser='$checked'"); 

       } /* END OF IF NOT EMPTY CHECKBOX SELECTED */

   } /* END OF FOR LOOP */

} /* END OF ISSET DELETE */

?>

<?php

$sql = "SELECT User.idUser, User.UserFirstName, User.UserLastName
FROM Student_has_Class
INNER JOIN User ON User.idUser = Student_has_Class.User_idUser AND User.Role = 'Student'
INNER JOIN Class ON Class.idClass = Student_has_Class.Class_idClass
WHERE Student_has_Class.Class_idClass = 2 AND Class.User_idUser = ".$x."
ORDER BY User.UserLastName";

$result = mysqli_query($connect,$sql);

$count = mysqli_num_rows($result);

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="form1" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td bgcolor="#FFFFFF">&nbsp;</td>
<td colspan="4" bgcolor="#FFFFFF"><strong>Class Roster</strong> </td>
</tr>
<tr>
<td align="center" bgcolor="#FFFFFF">Drop</td>
<td align="center" bgcolor="#FFFFFF"><strong>ID</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>Last Name</strong></td>
<td align="center" bgcolor="#FFFFFF"><strong>First Name</strong></td>
</tr>

<?php
$counter=0; /* FOR COUNTING PURPOSES */
while ($rows = mysqli_fetch_array($result))
{ 
?>

<tr>
<td align="center" bgcolor="#FFFFFF">
<?php
$iduser=$rows['idUser'];
echo "<input name='checkbox[$counter]' type='checkbox' id='checkbox[]' value='$iduser'></td>";
/* I HAVE ASSIGNED THE COUNTER INSIDE THE CHECKBOX ARRAY ABOVE. AND DO YOU REALLY NEED [] IN YOUR ID? */ ?>

<td bgcolor="#FFFFFF"><?php echo $rows['idUser'];?></td>

<td bgcolor="#FFFFFF"><?php echo $rows['UserLastName'];?></td>

<td bgcolor="#FFFFFF"><?php echo $rows['UserFirstName'];?></td>
</tr>

<?php
$counter=$counter+1; /* INCREMENT COUNTER EVERY LOOP */
} /* END OF WHILE LOOP */

echo "<input type='hidden' name='hiddencounter' value='$counter'>"; /* SUBMIT A HIDDEN INPUT CONTAINING THE OVERALL COUNTER */

?>

<tr>
<td colspan="5" align="center" bgcolor="#FFFFFF">
<input name="delete" type="submit" id="delete" value="Submit"></td>
</tr>
</table>
</form>