从表中批准用户 - 复选框

时间:2013-12-06 09:38:28

标签: php

我正在编写一个php脚本来批准在我的页面上注册的用户,但是当我想批准他们时,我遇到了一个小问题。这是我能得到的。

表:

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("activation") or die(mysql_error());
//User Approval Script
$result2 = mysql_query("SELECT * FROM userinfo WHERE status='0'") 
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Name</th> <th>Action</th> <th>Hours</th> <th>Approve</th> </tr>";
while($row = mysql_fetch_array( $result2 )) {
 // Print out the contents of each row into a table
 echo "<tr><td>"; 
 echo $row['first_name'];
 echo "</td><td>"; 
 echo $row['last_name'];
 echo "</td>"; 
 echo "<td>"; 
 echo $row['email'];
 echo "</td><td>";
 echo "<form action=\"approve.php\" method=\"post\"><input name=\"approve[]\" type=\"checkbox\">";
 echo "</td></tr>";
}
echo "</table>";
echo "<input type=\"submit\" value=\"Approve\"></form>";
?>

approve.php

<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("activation") or die(mysql_error());
$ticked = $_POST['approve'];

foreach($ticked as $id) {
     mysql_query("UPDATE status SET approved = '1' WHERE `ID` = '$id'");
}
unset($id);  
?>

我还想知道如何向每个获得批准的用户发送电子邮件......

先谢谢大家!

编辑:

approve.php上的页面全部为空白,状态未更新。

3 个答案:

答案 0 :(得分:1)

您可以尝试此操作吗,将<form>标记从near复选框移至顶部,并添加复选框值$row["id"]

<?php
        mysql_connect("localhost", "root", "") or die(mysql_error());
        mysql_select_db("activation") or die(mysql_error());
        //User Approval Script
        $result2 = mysql_query("SELECT * FROM userinfo WHERE status='0'") 
        or die(mysql_error());
        echo "<form action=\"approve.php\" method=\"post\"><table border='1'>";
        echo "<tr> <th>Name</th> <th>Action</th> <th>Hours</th> <th>Approve</th> </tr>";
        while($row = mysql_fetch_array( $result2 )) {
         // Print out the contents of each row into a table
         echo "<tr><td>"; 
         echo $row['first_name'];
         echo "</td><td>"; 
         echo $row['last_name'];
         echo "</td>"; 
         echo "<td>"; 
         echo $row['email'];
         echo "</td><td>";
         echo "<input name=\"approve[]\" type=\"checkbox\" value='".$row["id"]."' >";
         echo "</td></tr>";
        }
        echo "</table>";
        echo "<input type=\"submit\" value=\"Approve\"></form>";
?>

在approve.php中,

<?php
    mysql_connect("localhost", "root", "") or die(mysql_error());
    mysql_select_db("activation") or die(mysql_error());
    $ticked = $_POST['approve'];

    foreach($ticked as $id) {
         mysql_query("UPDATE status SET approved = '1' WHERE `ID` = '$id'");

         $message ='Approved message';

         mail('to email address', 'Your Subject', $message);
    }

 ?>

注意:使用mysqli_ *函数或PDO instaed使用mysql_ *函数(不建议使用)

答案 1 :(得分:0)

您尝试在循环while中打开表单,并在复选框中错过属性value

更改

echo "<form action=\"approve.php\" method=\"post\"><input name=\"approve[]\" type=\"checkbox\">";

echo '<input name="approve" type="checkbox" value='.$row["id"].'>';

然后将echo "<form action ='approve.php' method='post'>";置于while($row = mysql_fetch_array( $result2 )) {

之上

答案 2 :(得分:-1)

你应该有一个大表单,有很多复选框(我想这是你的第二页所基于的),但复选框是<input> s,而不是<form> s。您的最终HTML应如下所示:

<form>
    <table>
    ...
    <td><input type="checkbox" name="approve[]" value="USERIDTHATYOUWANTTOAPPROVE"></td>
    ...
    <td><input type="checkbox" name="approve[]" value="OTHERUSERIDTHATYOUWANTTOAPPROVE"></td>
    ...
    </table>
</form>

另外!