批准表单提交

时间:2012-04-30 13:09:47

标签: php mysql

我创建一个批准表单时遇到问题,因为我仍然是php初学者,

这个想法是

用户提交表格我在表格的批准行中设置默认值“0”。

所以在幕后,管理员会显示此表中所有成员的批准=“0”

这是代码

<code> 

    <?php
    $con = mysql_connect("localhost","ebarea_epic","...");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ebarea_epic", $con);

$query = "select * from medicalrep where approved='0'";

$result=mysql_query($query);

echo "<table border='1'>
<tr>
<th>User Name</th>
<th>Password</th>
<th>Mobile </th>
<th>Address</th>
<th>Faculty</th>
<th>Graduation Year</th>
<th>Region</th>
<th>Area</th>
<th>Line</th>
<th>Appointment Date</th>
<th>Resign Data</th>
<th>Job Title</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ID'] . "</td>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['password'] . "</td>";
  echo "<td>" . $row['Mobile'] . "</td>";
  echo "<td>" . $row['Address'] . "</td>";
  echo "<td>" . $row['Faculty'] . "</td>";
  echo "<td>" . $row['Graduation Year'] . "</td>";
  echo "<td>" . $row['Region'] . "</td>";
  echo "<td>" . $row['Line'] . "</td>";
  echo "<td>" . $row['Area'] . "</td>";
  echo "<td>" . $row['Appointment'] . "</td>";
  echo "<td>" . $row['Resign'] . "</td>";
  echo "<td>" . $row['job_title'] . "</td>";  
  echo "</tr>";
  }
echo "</table>";

mysql_close($con);
?>
</code>

我只想为每个表用户添加复选框,并在检查时将其状态更改为已批准列中的1

感谢所有

1 个答案:

答案 0 :(得分:0)

    $con = mysql_connect("localhost","ebarea_epic","...");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("ebarea_epic", $con);

$query = "select * from medicalrep where approved='0'";

$result=mysql_query($query);

$i = 1; //counter for the checkboxes so that each has a unique name
echo "<form action='process.php' method='post'>"; //form started here
echo "<table border='1'>
<tr>
<th>User Name</th>
<th>Password</th>
<th>Mobile </th>
<th>Address</th>
<th>Faculty</th>
<th>Graduation Year</th>
<th>Region</th>
<th>Area</th>
<th>Line</th>
<th>Appointment Date</th>
<th>Resign Data</th>
<th>Job Title</th>
<th>Update</th>
</tr>";

while($row = mysql_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ID'] . "</td>";
  echo "<td>" . $row['username'] . "</td>";
  echo "<td>" . $row['password'] . "</td>";
  echo "<td>" . $row['Mobile'] . "</td>";
  echo "<td>" . $row['Address'] . "</td>";
  echo "<td>" . $row['Faculty'] . "</td>";
  echo "<td>" . $row['Graduation Year'] . "</td>";
  echo "<td>" . $row['Region'] . "</td>";
  echo "<td>" . $row['Line'] . "</td>";
  echo "<td>" . $row['Area'] . "</td>";
  echo "<td>" . $row['Appointment'] . "</td>";
  echo "<td>" . $row['Resign'] . "</td>";
  echo "<td>" . $row['job_title'] . "</td>";
  echo "<td><input type='checkbox' name='check[$i]' value='".$row['ID']."'/>";   
  echo "</tr>";
  $i++;
  }
echo "</table>";
echo "<input type='submit' name='approve' value='approve'/>";
echo "</form>";

mysql_close($con);

现在来了process.php

if(isset($_POST['approve'])){
                if(isset($_POST['check'])){
                    foreach ($_POST['check'] as $value){
                        $sql = "UPDATE post SET post_approved = 1 WHERE ID = $value"; //write this query according to your table schema
                        mysql_query($sql) or die (mysql_error());
                    }
                }
            }

虽然你在这里使用mysql_ *函数,但我建议你使用 PDO

修改

根据您的要求,这是更新。

在管理面板脚本中更改此代码:

echo "<input type='submit' name='approve' value='approve'/>";

删除上面的行并改为添加:

echo "<input class='action' type='button' name='approve' value='approve' />";
   echo "<input class='action' type='button' name='edit' value='edit' />";
   echo "<input class='action' type='button' name='delete' value='delete' />";
   echo "<input type='hidden' name='action' value='' id='action' />"; //Action (edit, approve or delete) will be set here which will be passed as POST variable on form submission

现在你需要一些javascript来做一些技巧。

在管理面板脚本中添加以下代码(最好是head部分)

<script type="text/javascript" src="jquery-1.7.min.js"></script>
<script type="text/javascript">
            $(document).ready(function(){
                $('.action').click(function(){
                    var action = $(this).attr('name');
                    $('#action').val(action);
                    $(this).closest('form').submit();

                })
            })
     </script>

现在是process.php文件中的修改

if (isset($_POST['approve'])) {
    if (isset($_POST['check'])) {
        foreach ($_POST['check'] as $value) {
            $sql = "UPDATE post SET post_approved = 1 WHERE ID = $value"; //write this query according to your table schema
            mysql_query($sql) or die(mysql_error());
        }
    }
} elseif(isset($_POST['edit'])){
    //do the edit things here
} elseif(isset($_POST['delete'])){
    foreach ($_POST['check'] as $value){
        $sql = "DELETE FROM post WHERE ID=$value";//modify it
        mysql_query($sql) or die(mysql_error());
    }
}

注意

您可能不希望多重复选框进行编辑。你只需要稍微调整上面的javascript代码,它就会在表单提交时将ID作为post变量发送,你可以从中检索一个条目的详细信息然后编辑函数。我留给你。尝试一下,在这里发布您的试用代码,如果它不起作用,我会给你一个解决方案。