复选框只将最后一个值插入数据库,php mysql

时间:2017-08-27 11:53:32

标签: javascript php mysql sql insertion-sort

这是php代码。

 <form method="post" id="form1" action="<?php echo $_SERVER['PHP_SELF'];?>">

<table  border='4' id="atttable" style="height:100px;">
<tr>
<td align="center"><font color="black"><?php   $cur_date=date('Y-m-d');    echo $cur_date; ?></font></td>
<td colspan='20' align="center" bgcolor="black"><h1><font color="white">STUDENT ATTENDENCE</font></h1></td>
</tr>
<tr align='center' >
<th><font color="black">Serial No</font></th>
<th><font color="black">Student name</font></th>
<th><font color="black">Roll_No</font></th>
<th><font color="black">Attendence</font></th>                

</tr>

<tr align="center">
<?php
  $conn=mysql_connect("localhost","root","");
  $db=mysql_select_db('data',$conn);
  $qu="select * from details";
  $run=mysql_query($qu);

            $i=1;
            while($row=mysql_fetch_array($run))
            {
                $id=$row[0];
                $fname=$row[1];
                $lname=$row[2];
                $age=$row[3];
                $phone_no=$row[4];
                $address=$row[5];
                $roll_no=$row[6];                  
               ?>

            <td align="center"><font color="black"><?php echo $i;$i++; ?></font></td>
            <td align="center"><font color="black"><?php echo $fname,$lname; ?></font></td>
            <td align="center"><font color="black"><?php echo $roll_no; ?></font></td>
            <td align="center">
                <input type="checkbox"  value="present" name="attend" ><font color="black">&nbsp;P</font>
                <input type="checkbox"  value="absent"  name="attend" ><font color="black">&nbsp;A</font>
            </td>

        </tr>

              <?php

if(isset($_REQUEST['submit']))
        {
            $attend=$_POST['attend'];
             $SQL = "INSERT INTO attendence (fname, lname, roll_no,attendence) VALUES ('$fname', '$lname', '$roll_no','".$attend."')";  
                           $result = mysql_query($SQL);

        }
?>


        <?php
            }
       ?>

       </table>
       <button type="submit" name="submit" id="sub1">SUBMIT</button>


    </form>
请帮助我完成它,这是一种痛苦。 每当我提交时,为学生选择的最后一个值都会输入数据库。

例如:如果我已插入5名学生并将其各自的复选框选中为p,a,a,p,a。为所有学生输入数据库的值为a。

我完全编辑了上面的代码以使其工作,但现在它更糟糕,因为它在插入时显示任何记录。

    <form method="post" id="form1" action="<?php echo $_SERVER['PHP_SELF']; ?>">

    <table  border='4' id="atttable" style="height:100px;">
    <tr>
    <td align="center"><font color="black"><?php $cur_date=date('Y-m-d'); echo $cur_date; ?></font></td>
    <td colspan='20' align="center"
                    bgcolor="black"><h1><font color="white">STUDENT ATTENDENCE</font></h1></td>
    </tr>
    <tr align='center' >
                <th><font color="black">Serial No</font></th>
                <th><font color="black">Student name</font></th>
                <th><font color="black">Roll_No</font></th>
                <th><font color="black">Attendence</font></th>
                <th><font color="black">Date</font></th>


    </tr>


    <?php
       $result=mysqli_query($conn,"select * from details");
       $serialnumber=0;
       $counter=0;
       while($row=mysqli_fetch_array($result))
          {
            $serialnumber++;                   

            $id=$row[0];
            $fname=$row[1];
            $lname=$row[2];
            $age=$row[3];
            $phone_no=$row[4];
            $address=$row[5];
            $roll_no=$row[6];
            $date=date("Y-m-d ");  

    ?>

    <tr align="center">                             

    <td align="center"><font color="black"><?php echo $serialnumber; ?></font></td>

    <td align="center"><font color="black"><?php echo $row['fname'],' ',$row['lname']; ?></font>
    <input type="hidden" value="<?php echo $row['fname'],' ',$row['lname']; ?>" name="fname[]">
    </td>

    <td align="center"><font color="black"><?php echo $row['roll_no']; ?></font>
    <input type="hidden" value="<?php echo $row['roll_no'];?>" name="roll_no[]">
    </td>

    <td align="center">
    <input type="radio"  value="present" name="attendence[<?php echo $counter; ?>]" ><font color="black">&nbsp;P</font>
    <input type="radio"  value="absent"  name="attendence[<?php echo $counter; ?>]" ><font color="black">&nbsp;A</font>

    </td>
    <td><?php
           echo $date;
        ?>
     </td>

     </tr>        
     <?php
          $counter++;
         }
     ?>

     </table>
     <button type="submit" name="submit" id="sub1" value="submit">SUBMIT</button> 

      <?php             
   include("conn.php");   
   if(isset($_POST['submit']))
            {
      foreach($_POST['attendence'] as $id=>$attendence){      

          $fname=$_POST['fname'][$id];
           $roll_no=$_POST['roll_no'][$id];
          $date=date("Y-m-d");

    $query="INSERT INTO attendence(fname,roll_no,attendence,date)values('$fname','$roll_no','$attendence','$date')";
    $result=mysqli_query($conn,$query);
            }} 
    ?>

    </form>

我以某种方式再次编辑它并感谢上帝我得到了我想要的输出但又被卡住了:| 。当我插入数据并进行更新时,只有最后一条记录显示在整行上。 //这是html代码     “&GT;

   <table  border='4' id="atttable" style="height:100px;">
        <tr>
            <td align="center"><font color="black"><?php $cur_date=date('Y-m-d'); echo $cur_date; ?></font></td>
            <td colspan='20' align="center"
                bgcolor="black"><h1><font color="white">STUDENT ATTENDENCE</font></h1></td>
        </tr>
        <tr align='center' >
            <th><font color="black">Serial No</font></th>
            <th><font color="black">Student name</font></th>
            <th><font color="black">Roll_No</font></th>
            <th><font color="black">Attendence</font></th>
            <th><font color="black">Date</font></th>


        </tr>


        <?php
               $result=mysqli_query($conn,"select * from details");
            $serialnumber=0;
            $counter=0;
            while($row=mysqli_fetch_array($result))
            {
                $serialnumber++;


                $id=$row[0];
                $fname=$row[1];
                $lname=$row[2];
                $age=$row[3];
                $phone_no=$row[4];
                $address=$row[5];
                $roll_no=$row[6];
                $date=date("Y-m-d ");



                ?>

        <tr align="center">                             

            <td align="center"><font color="black"><?php echo $serialnumber; ?></font></td>

            <td align="center"><font color="black"><?php echo $row['fname'],' ',$row['lname']; ?></font>
                <input type="hidden" value="<?php echo $row['fname'],' ',$row['lname']; ?>" name='fname[]'>
            </td>

            <td align="center"><font color="black"><?php echo $row['roll_no']; ?></font>
            <input type="hidden" value="<?php echo $row['roll_no'];?>" name='roll_no[]'>
            </td>

            <td align="center">                    
               <input type="radio"  value="present"  name="attendence[<?php echo $counter; ?>]" 
                      <?php if(isset($_POST['attendence'][$counter]) && $_POST['attendence'][$counter]=="present") {
                    echo "checked=checked" ;
                } ?>                    

                      required>&nbsp;P
               <input type="radio"  value="absent"  name="attendence[<?php echo $counter; ?>]" 
                      <?php if(isset($_POST['attendence'][$counter]) && $_POST['attendence'][$counter]=="absent") {
                    echo "checked=checked";
                }?>                    

                      required>&nbsp;A


            </td>
            <td><?php
               echo $date;
                ?>
            </td>

       </tr>



        <?php
               $counter++; 
            }
       ?>

       </table>
        <button type="submit" name="submit" id="sub1" value="submit">SUBMIT</button> &nbsp;&nbsp;
        <a href="viewatt.php" id="butt">VIEW</a>

    </form>

//这是php代码       

        if(isset($_POST['submit'])){ 

           $date=date("Y-m-d ");   
           $records= mysqli_query($conn,"select * from attendence where att_day='$date'");
            $num=mysqli_num_rows($records);

            if($num){
                 foreach($_POST['attendence'] as $id=>$attendence)
            {      
                 $fname=$_POST['fname'][$id];
                $roll_no=$_POST['roll_no'][$id];

                $result= mysqli_query($conn,"update attendence set fname='$fname',roll_no='$roll_no',attendence='$attendence',att_day='$date' where att_day='$date'");

                        if($result){
        echo "<script>alert('Attendence updated successfully')</script>";
                                 }
            }}
                else{
            foreach($_POST['attendence'] as $id=>$attendence)
            {      
                 $fname=$_POST['fname'][$id];
                $roll_no=$_POST['roll_no'][$id];

       $query ="INSERT INTO attendence (fname,roll_no,attendence,att_day)  VALUES ( '$fname','$roll_no','$attendence','$date') ";
   $insert_Result=mysqli_query($conn,$query);

                 if($insert_Result){
        echo "<script>alert('Attendence inserted successfully')</script>";
                                 }

            } }   
        }

       ?>

0 个答案:

没有答案