Meenesh Jain
Meenesh Jain

Reputation: 2528

unable to insert values in table using loop

thanks in advance what i was doing is that i created a table in mysql with record details like (id , name, date, ) and another table with personal details like (id, name, address , batch , dob , gender and blood group, and so on ) what i want to do is that i want to fetch some details from personal detail like name batch in form of table now i want to another field with a checkbox which should be in every row now on selecting a few checkbox and submitting button it should be able to insert fetched data from in the record table from personal detail table and the value of checkbox as 1 on selected or 0 on not selected . I have tried the code but unable to figure the solution out.

these is a part of my complete code

require_once('connect.php'); //  contains my connection 


   $select_query="SELECT * FROM personal WHERE batch='161'"; 

       $result=mysql_query($select_query);

if(!$result)
 {
$error="there was some error try again later";
 }
?>
<div class="module-table-body" >

                    <form  method="post">
                    <table id="myTable" class="tablesorter" align="center">
                        <thead>
                            <tr>
                                <th style="">#</th>
                                <th style="">Full name</th>
                                <th style="">Batch</th>
                                <th style=""> Present / Absent</th>

                            </tr>
                        </thead>
                     <tbody>
                        <?php 
                while($row=mysql_fetch_array($result))
               {

             ?>
             <tr>
               <td ><?php echo $row['id']; ?></td>
                 <td ><?php echo $row['full_name']; ?></td>
                 <td><?php echo  $row['batch']; ?></td>
            <td>
         <input name="checkbox[]" type="checkbox" value="<?php echo $row['id']; ?>">    
                     </td>
                    </tr>
       <?php

 }

               ?>
              <tr>
              <td>
          <input name="save" type="submit" value="Save"  /></td></tr>

           </tbody>
           </table>

          <?php

           if(isset($_POST['save']))
             {
          $box=$_POST['checkbox'];
           foreach($box as $id)
               {

        $box= isset($_POST['checkbox']) && $_POST['checkbox'] ? "1" : "0" ;


             $insert="INSERT INTO record VALUES('','$fname','$date','$box')";

            $res=mysql_query($insert);
            if(!$res)
            {
        echo mysql_error();
       }
       else
    {
        echo "updated successfully";
    }

    }
      }


     ?>

the code works and only inserts the last entry with only zero as checkbox value

Upvotes: 0

Views: 190

Answers (1)

Sherin Jose
Sherin Jose

Reputation: 2516

You are overwriting $box by writing the code

$box= isset($_POST['checkbox']) && $_POST['checkbox'] ? "1" : "0" ;

in the loop, edit that by using another variable....

Upvotes: 1

Related Questions