Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

i want to insert data from a form which is declared in while loop and can be iterated 40 times (mean 40 row with 6 input fields each row).so what i need,i need to insert data from every input field (currently 6) to mysql row .so how can i accomplish that.please ask me for further detail if you need.

looking forward for best answer.i am stuck here and it's my degree final year project. thank you for help.

This is while loop with input fields code and name retrieved from database.

include'connection.php';
$res=mysql_query("SELECT * FROM `std_register` AS p1 ,`sims-register-course` AS p2 WHERE p2.semester=p1.std_semester and p2.department=p1.std_department and p2.id = '".$_GET['id']."'");
if ($res==FALSE) {
echo die(mysql_error());
    }
echo "<form method='POST' action= '".htmlspecialchars($_SERVER['PHP_SELF'])."'.'>'";
    while ($row=mysql_fetch_assoc($res)) {
    echo "<tr>";
    echo "<td>".$row["std_name"].'</td>';
    echo "<td><input type='text' id='attendence_marks' name='attendence_marks[]' class='input-field-size'></td>";
    echo "<td><input type='text' id='assignment_marks' name='assignment_marks[]' class='input-field-size'></td>";
    echo "<td><input type='text' id='presentation_marks' name='presentation_marks[]' class='input-field-size'></td>";
    echo "<td><input type='text' id='other_marks' name='other_marks[]' class='input-field-size'></td>";
    cho "<td><input type='text' id='mid_marks' name='mid_marks[]' class='input-field-size'></td>";
    echo "<td><input type='text' id='final_marks' name='final_marks[]' class='input-field-size'></td>";
    } 

I want to insert data from this form to database with single button click after fields are filled.and input all the rows of form into database at once.

This is the loop i have used to insert data but not working and display me some errors also.previously i have used for loop but that was also not working correctly.and insert only 0's to database.please checkout the loop code here and i think i am doing it wrong so please make me correct.

<?php
    $count = count($_POST);
    echo "count is :".$count ."<br>";
    if(isset($_POST['submit'])) {
    $attendence_marks=$_POST['attendence_marks'];
    $assignment_marks=$_POST['assignment_marks'];
    $presentation_marks=$_POST['presentation_marks'];
    $other_marks=$_POST['other_marks'];
    $mid_marks=$_POST['mid_marks'];
    $final_marks=$_POST['final_marks'];
    $total_marks = $attendence_marks+$assignment_marks+$presentation_marks+$other_marks+$mid_marks+$final_marks;
    foreach ($attendence_marks as $key1 => $att_marks) {
    foreach ($assignment_marks as $key2 => $assig_marks) {
    foreach ($presentation_marks as $key3 => $pre_marks) {
    foreach ($other_marks as $key4 => $o_marks) {
    foreach ($mid_marks as $key5 => $m_marks) {
    foreach ($final_marks as $key6 => $f_marks) {
    foreach ($total_marks as $key7 => $t_marks) {
    $i = "INSERT INTO `std_gradding` (attendence_marks,assignment_marks,presentation_marks,other_marks,mid_term_marks,final_marks,total_marks) VALUES ('$att_marks','$assig_marks','$pre_marks','$o_marks','$m_marks','$f_marks','$t_marks')";
    $result = mysql_query($i) or die(mysql_error());
    echo "Data Inserted.Thank you.";
    echo "$assignment_marks , $presentation_marks , $other_marks , $mid_marks , $final_marks , $total_marks" ;


       }
      }
     }
    }
   }
  }
}

}
?>
share|improve this question
2  
To which school are you going? I would quit immediatly if they teach you this sort of code. – Daan Aug 28 '14 at 14:40
    
Where exactly are you stuck? You don't know how to insert into the database, but you know how to select from one? – N.B. Aug 28 '14 at 14:40
    
they didn't teach me code exactly this is my own idea of creating code.if you wana tune the code than please reply me with answer.thank you for your precious time. – hassan gul Aug 28 '14 at 14:50
    
@N.B. i know how to insert to database.but i don't know how to insert from array to a database.it only insert the first row to database and not more than that. – hassan gul Aug 28 '14 at 15:19
    
Do you know how to loop an array? – N.B. Aug 28 '14 at 15:22

Have a look on this example

<?php
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">';
for($i=0;$i<5;$i++){
    echo '<input name="text['.$i.']" value="input '.$i.'"><br>';
}
echo '<input type="submit"><br>';
echo '</form>';
?>

And the post dump is:

array (
  'text' => 
  array (
    0 => 'input 0',
    1 => 'input 1',
    2 => 'input 2',
    3 => 'input 3',
    4 => 'input 4',
  ),
)
share|improve this answer
    
i have tried loop but it insert only 0 values to all my database fields.and for two input rows they insert upto 100 rows in database. – hassan gul Aug 28 '14 at 14:52
    
did not get what you mean – Zeusarm Aug 28 '14 at 15:09
    
i mean when i use loop in my code and press submit button.than it insert upto 100 rows into database table.without correct data.and these 100 rows are only for two rows of form. if the form contain 40 rows than it become more heavy and show me timeout.so can you please send me full code that i may implement and give it a try and will let you know after a try :) – hassan gul Aug 28 '14 at 15:15
    
so it means that you are missing some ID-s... – Zeusarm Aug 28 '14 at 15:16
    
ID-s? where i missed some. the input fields repeat 40 times if there are 40 students. and if there are 40 students i want to insert all that 40 students data at once to database.checkout this image.this is how my form look like. dl.dropboxusercontent.com/u/24290985/database.png and this is my database table look. dl.dropboxusercontent.com/u/24290985/database%202.png – hassan gul Aug 28 '14 at 15:24

The first problem I see is that your id's are not unique. You could number them.

id='attendence_marks_1'

for the first row. Then you can actually access the values, and they will get properly posted when you submit the form. The rest is basis stuff.

share|improve this answer
    
can you please also reply me about the insert into database statement.and thank you for valuable time and reply. – hassan gul Aug 28 '14 at 14:53
    
What can I say? I see a lot of nested 'foreach' loops, that cannot be good. But since my answer is voted down, and it's an assignment afterall, I see no reason to continue. – KIKO Software Aug 28 '14 at 19:17
    
you should have to help me..that's why i am here..if you don't and other also think like you than this is not good for any one or any reason.sometime help is more important than reputation,so please help me out. thank you. – hassan gul Aug 29 '14 at 0:12
    
You're right, this site is for helping people. There's already enough help here to get your project started again (id's and the loops). Use it. – KIKO Software Aug 29 '14 at 0:43
    
so you mean i use id='attendence_marks_1' id='assignment_marks_2' and so on. and is my for loop correct? i think not because it doesn't insert data to database and show error also at top most line. if i use for loop that will be appropriate and should work? @KIKO Software – hassan gul Aug 29 '14 at 8:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.