从每行中选择一个单选按钮,然后将其提交到数据库mysqli

时间:2016-11-05 11:45:34

标签: php jquery html5 mysqli

    <?php
    include('connect.php');
    $query=mysqli_query($conn,"select * from users") or die("unable to connect");

    ?>
    <html>
    <head>
    </head>
        <body>


            <h2 align="center"> student list</h2>
            <form action="radio.php" method="post"> 
            <table align="center" border="1" cellspacing="0" cellpadding="0" width="700">
                <thread>
                    <td align='center'>id</td>
                    <td align='center'>enrolloment_no</td>
                    <td align='center'>first_name</td>
                    <td align='center'>last_name</td>
                    <td align='center'>attendance&nbsp;&nbsp;&nbsp;</td>

                        </thread>

            <?php
                $i=1;
                while($row=mysqli_fetch_array($query,MYSQLI_BOTH))
                    {
                    echo "<tr>
                    <td align='center'>".@$i."</td>

<td align='center'>".@$row['enrolloment_no']."</td>
<td align='center'>".@$row['first_name']."</td>
<td align='center'>".@$row['last_name']."</td>
<td align='center'>


<input type=\"radio\" name=\"a\" value=\"present\">present

<input type=\"radio\" name=\"a\" value=\"absent\">absent

</tr>

";
            $i++;
                        }   

                ?>

                </table>
                <input type="submit" value="submit">
                        </form>
                </body>



        </html>

上面提到的代码包含一个考勤页面表格,其中包含单选按钮,但上面代码中的主要问题是我无法从每一行中选择单选按钮,因为我需要从每一行中选择单选按钮如果有人有任何建议如何选择每行存在或不存在的一个单选按钮然后提交到数据库。

1 个答案:

答案 0 :(得分:0)

您可以创建一个按行增加的数字,或者更好,将此整数设置为等于数据库中行的相应标识符。

添加名为=&#34; rowIDs []&#34;的隐藏输入&安培;值=&#34; {theIdentifier}&#34 ;.

当您将行的单选按钮的名称设置为&#34; a_ {theIdentifier}&#34;你可以在PHP脚本中运行foreach。

这可能是你想要的吗?

代码中的全局示例:

 $i=1;
    while($row=mysqli_fetch_array($query,MYSQLI_BOTH)){
       echo "<tr>
       <td align='center'>".@$i."</td>
       <td align='center'>".@$row['enrolloment_no']."</td>
       <td align='center'>".@$row['first_name']."</td>
       <td align='center'>".@$row['last_name']."</td>
       <td align='center'>
         <input type=\"hidden\" value=\"".$row['enrolloment_no']."\" name=\"rowIDs[]\" />
         <input type=\"radio\" name=\"a_".$row['enrolloment_no']."\" value=\"present\">present
         <input type=\"radio\" name=\"a_".$row['enrolloment_no']."\" value=\"absent\">absent
       </td>
       </tr>
       ";
       $i++;
   } 

现在提交中:

$rowIDs = $_POST['rowIDs'];
foreach($rowIDs as $rowID) {
   $radioButtonValue = $_POST['a_'.$rowID];
   mysqli_query("update `yourtable` SET `attendance` = '".$radioButtonValue."' WHERE `enrolloment_no` = '".$rowID."'");
}

(为此,将$ i替换为$ row [&#39; enrolloment_no&#39;])

ps我刚刚编辑了一些错误。

当您在表单顶部或底部添加这些输入时..但在表单标签中  目前
 缺席
使用硕士考勤 然后在提交php部分:

if(isset($_POST['useMasterAttendance'])) {
  $masterAttendanceValue = $_POST["masterAttendance"];
  $rowIDs = $_POST['rowIDs'];
  foreach($rowIDs as $rowID) {

     mysqli_query("update `yourtable` SET `attendance` = '".$masterAttendanceValue."' WHERE `enrolloment_no` = '".$rowID."'");
  }
}else {
//DO THE CODE FOR EACH ROW LIKE ABOVE
}

对不起,如果我犯了任何错别字