Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am a PHP newbie having a problem inserting an array from a form into the database using foreach statement. I am trying to create a form that accepts Score, Grade and Comment for each subject. However,only the last subject gets inserted into the database. The list of subjects is loaded from previously registered subjects (I have no problem with that). My problem is getting all the subjects to save back to database. This is the form i use:

<legend>ENTER RESULTS:</legend>
<input type="hidden" name="results[]" value= "<?php foreach ($subjects as $subject):?>
<ul>
<li> <input type="text" name="subject_name" size="10" value ="<?php htmlout($subject['subject_name']); ?>"/>
          </label>
     <label for="subject_score">SCORE:</label>
    <input type="text" name="subject_score" size="5" value = "<?php
        htmlout($subject_score);?>"/> <label for="subject_score">GRADE:</label>
    <input type="text" name="subject_grade" size="5" value = "<?php
        htmlout($subject_grade);?>"/><label for="subject_grade">COMMENT:</label>
    <input type="text" name="subject_comment" size="30" value = "<?php
        htmlout($subject_comment);?>"/> </div></li>

    <?php endforeach; ?>
    <input type="hidden" name="student_id" value="<?php
        htmlout($student_id); ?>
  </fieldset>

This is the php code i use:

if (isset($_GET['result']))
{
try
{
  $sql = 'INSERT INTO results SET
      student_id = :student_id,
      subject_name = :subject_name,
      subject_grade = :subject_score,
      subject_grade = :subject_grade,
      subject_comment = :subject_comment';
  $s = $pdo->prepare($sql);

  foreach ($_POST['results'] as $result)
  {
  $s->bindValue(':student_id',$_POST['student_id']);
  $s->bindValue(':subject_name', $_POST['subject_name']);
  $s->bindValue(':subject_score', $_POST['subject_score']);
  $s->bindValue(':subject_grade', $_POST['subject_grade']);
  $s->bindValue(':subject_comment', $_POST['subject_comment']);
  $s->execute();
  }
  }
catch (PDOException $e)
{
  $error = 'Could not Register the Student for the Subjects, Please try again'.$e->GetMessage();
  include 'error.html.php';
  exit();
}
echo 'Success';
share|improve this question
 
This code makes no attempt to write any data to any database. What exactly is the problem? –  Mike W Nov 17 '13 at 7:53
 
Thanks, omitted the PHP code i use, edited. –  Whlthy Nov 17 '13 at 8:02
 
<input type="hidden" name="results[]" value= "<?php foreach ($subjects as $subject):?> has error –  timus2001 Nov 17 '13 at 8:09
 
@timus2001 thanks, removed that bit. –  Whlthy Nov 17 '13 at 8:21
add comment

1 Answer

up vote 0 down vote accepted

you can use form element in array like subject_score[]

your form should be like

foreach
<input type="text" name="subject_name[]" size="10" value ="<?php htmlout($subject['subject_name']); ?>"/>
          </label>
     <label for="subject_score">SCORE:</label>
    <input type="text" name="subject_score[]" size="5" value = "<?php
        htmlout($subject_score);?>"/> <label for="subject_score">GRADE:</label>
    <input type="text" name="subject_grade[]" size="5" value = "<?php
        htmlout($subject_grade);?>"/><label for="subject_grade">COMMENT:</label>
    <input type="text" name="subject_comment[]" size="30" value = "<?php
        htmlout($subject_comment);?>"/> </div></li>
endforeach

then you can collect those value like below

foreach($_POST['subject_name'] as $key=>$val){
  //val will hold subject name
} 
share|improve this answer
 
GOD BLESS YOU A MILLION TIMES OVER! Worked like a charm! Thanks alot! –  Whlthy Nov 17 '13 at 8:28
 
Pls how do i accept ur answer? would gladly do. –  Whlthy Nov 17 '13 at 8:32
 
click on tick mark around the answer and click up arrow –  timus2001 Nov 17 '13 at 8:33
 
OK, Done. You awesome dude, thanks. –  Whlthy Nov 17 '13 at 8:36
add comment

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.