简体   繁体   中英

how to insert value of radio button for each row from php into mysqli database

<?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>

The above is the code of the attendance page and now I want to submit the the student's attendance ie for each student into database along with its enrolloment number so if any body could help me with it.

I have also tried something to sumbit and it looks like below mentioned...

<?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.
        "'");
  }
?>

**

Error : Warning: mysqli_query() expects parameter 1 to be mysqli, null given in C:\\wamp64\\www\\demo\\radio.php on line 7

**

<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);
    }       
    }
}
?>   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM