如何将每行的单选按钮值从php插入mysqli数据库

时间:2016-11-16 09:11:11

标签: php html 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">
          <thead>
            <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>

          </thead>

          <?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=\"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++; 
            } 
         ?>

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

以上是出勤页面的代码,现在我想将学生的出勤率,即每个学生的出勤率及其登记号码提交给数据库,以便任何机构可以帮助我。

我也尝试了一些东西来总结,看起来像下面提到的......

<?php
  $rowIDs = $_POST['rowIDs'];
  foreach( $rowIDs as $rowID ) {
    $radioButtonValue = $_POST['a_' . $rowID ];
    $b = mysqli_query(@$conn, "update `adding` SET `masterAttendance` = '"  . $radioButtonValue .
        "' WHERE `enrolloment_no` = '" . $rowID.
        "'");
  }
?>

**

  

错误:警告:mysqli_query()期望参数1为mysqli,null   在第7行的C:\ wamp64 \ www \ demo \ radio.php中给出

**

1 个答案:

答案 0 :(得分:0)

<form method="post" action="yourPageName.php"/>

    <?php $i=1; 
    echo '<table>';
        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['first_name'].'</td>
                            <td align="center">  

                                <input  type="hidden" value="'.$row['enrolloment_no'].'" name="enrolloment_no_'.$i.'"/>
                                <input type="radio" name="a_'.$row['enrolloment_no'].'" value="present" />present
                                <input type="radio" name="a_'.$row['enrolloment_no'].'" value="absent" />absent
                                <input  type="hidden" value="'.$i.'" name="rowCount" id="rowCount"/>
                        </td>
                </tr>';
            $i++;
        }
    echo '</table>
    <input type="submit" value="submit">';
    ?>
</form>
 this may be your form and the form submission is below.       
<?php
if(isset($_POST) && $_POST!="" && !empty($_POST)){ // checking if form submision is occured or not.
    if( isset($_POST['rowCount']) && !empty($_POST['rowCount']) && $_POST['rowCount']!="" )
    {  $rowCount=$_POST['rowCount'];  }
    else{ $rowCount="";}

    for($i=1;$i<=$rowCount;$i++){
        if( isset($_POST['enrolloment_no_'.$i]) && !empty($_POST['enrolloment_no_'.$i]) && $_POST['enrolloment_no_'.$i]!="" )
        {  $enrolloment_no=$_POST['enrolloment_no_'.$i];  }
        else{ $enrolloment_no="";}

        if( isset($_POST['a_'.$enrolloment_no]) && !empty($_POST['a_'.$enrolloment_no]) && $_POST['a_'.$enrolloment_no]!="" )
        {  $absentPresnt=$_POST['a_'.$enrolloment_no];  }
        else{ $absentPresnt="absent";}

        //$b = mysqli_query($conn, "update `adding` SET `masterAttendance` = '"  . $absentPresnt . "' WHERE `enrolloment_no` = '" . $enrolloment_no. "'");
         $knownStmt=mysqli_prepare($conn, "update `adding` SET `masterAttendance` =? WHERE `enrolloment_no` = ? ;");
    if( $knownStmt ) {
        mysqli_stmt_bind_param($knownStmt,"dd",$absentPresnt,$enrolloment_no);
        mysqli_stmt_execute($knownStmt); 
        mysqli_stmt_close($knownStmt);
    }       
    }
}
?>