Jump to content

inserting multiple rows in mysql using php


1981tarun

Recommended Posts

 

Hello everyone,

 

Once again i need your help this time i want to know that, if I inserting multiple row by single insert query with the help of foreach (loop) then how to insert another cell value according to the name wise

 

so that's way i send my hole code to you.

 

<?php

                            //include configuration file for connection

                            include_once('config.php');

                            $sql = "select * from CLASS_STUDENT ORDER BY STUDENT_NAME ASC ";

                            $result=mysql_query($sql);

                            $count=mysql_num_rows($result);

                        ?>

                        <form name="form1" method="post" action="">

                            <table style="text-align: left; padding: 5px;" cellpadding="0px" cellspacing="0px">

                          <tbody>

                              <tr>

                                <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student Name</th>

                                <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student Class</th>

                                <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student Section</th>

                                <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Current Date</th>

                                <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student Present</th>

                                <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Student Absent</th>

                                <th style="text-align: center; padding: 5px; border: 1px #000000 solid;">Comment</th>

                              </tr>

                              <?php

                                while($rows=mysql_fetch_array($result))

                                {

                              ?>                                                 

                              <tr>                               

                                <td class="table1">

                                    <? $id[] = $rows['STUDENT_NAME']; ?><? echo $rows['STUDENT_NAME'];?>

                                </td>

                                <td class="table1">

                                    <input name="class[<? echo $rows['STUDENT_NAME']; ?>]" type="text" value="<? echo $rows['STUDENT_CLASS']; ?>">

                                </td>     

                                <td class="table1">

                                    <input  name="section[<? echo $rows['STUDENT_NAME']; ?>]" type="text" value="<? echo $rows['STUDENT_SECTION']; ?>">

                                </td>

                                <td class="table1">

                                    <input name="date[<? echo $rows['STUDENT_NAME']; ?>]" type="text" value="<? echo $rows['PRESENT_DATE']; ?>">

                                </td>                                                                               

                                <td id="present">

                                    <input type="radio" name="present[<? echo $rows['STUDENT_NAME']; ?>]" checked="checked" value="PRESENT">Present           

                                </td>

                                <td id="absent">                                   

                                    <input type="radio" name="present[<? echo $rows['STUDENT_NAME']; ?>]" value="ABSENT">Absent

                                </td>                             

                                <td style="text-align: left; padding: 5px; border: 1px #000000 solid; height: 33px;">

                                    <input name="comment[<? echo $rows['STUDENT_NAME']; ?>]" type="text" value="<? echo $rows['COMMENT'];?>">

                                </td>                                                 

                              </tr>

                              <?php }?>

                              <tr>

                                <td colspan="7" style="vertical-align:middle; text-align: center;"><br><br>                                   

                                    <input id="Submit" type="submit" name="Submit" value="Insert" style="text-align: center; background-color: #000000; color: #ffffff; border: 1px #000000 solid;">

                                </td>

                              </tr>                                                                               

                          </tbody>

                        </table>

                    </form> 

                    <?php

                        if(isset($_POST['Submit']))

                        {

                          foreach($_POST['present'] as $id => $value)

                          {

                              $class=$_POST['class'];

                              $section=$_POST['section'];

                              $date=$_POST['date'];

                              $comment=$_POST['comment'];                                   

                              $sql = "INSERT INTO ATTENDANCE(STUDENT_NAME, STUDENT_CLASS, STUDENT_SECTION, PRESENT_DATE, STUDENT_PRESENT, COMMENT) VALUES ('".$id."', '$class[$value]', '$section[$value]', '$date[$value]', '".$value."', '$comment[$value]') ";

                              $result = mysql_query($sql);                         

                          }

                        }

                        if($result)

                        {

                            header("location:Tea_home.php");                                                     

                        }

                        else

                        {

                          //print_r ($_POST);

                          echo "Your entry is not completed at this time.............";

                        }

                    ?>

 

 

My result is comming just like that

 

STUDENT_ID ||  STUDENT_NAME || STUDENT_CLASS  ||  STUDENT_SECTION  ||  PRESENT_DATE ||  STUDENT_PRESENT    ||COMMENT

231    ||  PRASHANT KUMAR  ||                    ||                      || 1/1/0001 12:00:00 AM      ||        ABSENT                || 

230    ||  JYOTI NANDA            ||                    ||                      || 1/1/0001 12:00:00 AM      ||            PRESENT        || 

229    ||  TARUN NANDA          ||                    ||                      || 1/1/0001 12:00:00 AM      ||            PRESENT        || 

228    ||  RAVI KUMAR            ||                    ||                      || 1/1/0001 12:00:00 AM      ||            PRESENT        || 

227    ||  RAJIV KUMAR            ||                    ||                      || 1/1/0001 12:00:00 AM      ||            PRESENT          ||

226    ||  PRASHANT KUMAR    ||                    ||                      || 1/1/0001 12:00:00 AM      ||            ABSENT          ||

225    ||  JYOTI NANDA            ||                    ||                      || 1/1/0001 12:00:00 AM      ||            PRESENT          ||

 

 

 

can you help me where i am writing wrong code

 

Hi

 

Do you want another column on screen that you want entered on the table? If so what is the column?

 

By the way, using Student_Name as the key is probably not a good idea, especially if you are not using mysql_real_escape_string. If one of your students was called something like ');DROP TABLE ATTENDANCE; it would destroy your database (look up SQL injection for more details).

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.