How to use array to submit Multiple Values In Form?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Akpo
    New Member
    • Jan 2011
    • 5

    How to use array to submit Multiple Values In Form?

    Hi all...i am working on a project to capture students' data for an educational institution. I want to have a form that allows multiple student records to be filled in at once..e.g(this is the form):
    Code:
    <html>
    <body>
    <table>
    <form method="post" action="array_post.php"
    <?php
    
    $i=2;
    for($x = 1; $x <=$i; $x++)
    {echo '<tr><td>Registration  Number:<td><input type=text name="reg_no[]"></td><td>First Name:</td><td><input type=text name="first_name[]"><td>Last Name:</td><td><input type=text name="last_name[]"></tr>';}
    ?>
    <tr><td colspan="7" align="center"><input type="submit" value="Submit Names"  />
    </td></tr></form>
    </table>
    </body>
    </html>
    ..
    for the code to insert it into the database..this is what i came up with:
    Code:
     <?php
    include 'poly_connect.php'; 
    foreach ($_POST['reg_no'] as $row=>$reg_no)
    $id = mysqli_real_escape_string($link,$reg_no);
    $first_name = mysqli_real_escape_string($link,$_POST['first_name'][$row]);
    $last_name = mysqli_real_escape_string($link,$_POST['last_name'][$row]);
    $password=mysqli_real_escape_string($link,sha1($_POST['password'][$row]));
     $sql = "INSERT INTO 
                        students(reg_no,first_name,last_name) 
                    VALUES('" . $reg_no . "','" . $first_name . "', 
    				'" . $last_name . "','" . $password. "'
                          
                           )";  
      
            $result = mysqli_query($link,$sql)  or die ("error".mysqli_error($link)); 
    		
    		?>
    ...problem is its not inserting anything..i need help on getting these values inserted and also the "hashing" of the passwords..plea se help me out.Thanks
  • dinesh1440
    New Member
    • May 2010
    • 16

    #2
    In case, You have your data in a well formatted file You can use file handling. Give the file in the first page. Then process it in the second page after submitting line by line. As each line gives information about a particular student you can store it directly using loops. I hope that did answer your question.

    Comment

    • Akpo
      New Member
      • Jan 2011
      • 5

      #3
      Er...well the main problem is the code i will have to write to do that...i surfed the net to get the info i pasted..but only the last line is being posted into the table...praps my coding of the array is wrong?

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Firstly, your opening form element is not closed. This will cause problems for the browser.

        Secondly, you have neglected to use curly-brackets with your foreach construct. This means that only the expression immediately following the foreach will be executed as the body of the loop. Consider the following:
        [code=php]
        // Only the expression immediately following the foreach is executed.
        // The square of each iteration is not displayed (until the loop exits).
        foreach( array(1, 2, 3) as $i )
        echo "Current iteration's value: $i\n";
        echo "Current iteration's value squared: ", ($i * $i), PHP_EOL;

        // The loop's scope is defined using curly-braces here and works as expected
        foreach( array(1, 2, 3) as $i )
        {
        echo "Current iteration's value: $i\n";
        echo "Current iteration's value squared: ", ($i * $i), PHP_EOL;
        }
        [/code]

        Fix these issues, try again, and report any issues.

        Comment

        • Akpo
          New Member
          • Jan 2011
          • 5

          #5
          Thank you Markus..it worked!!!....no w i ran into another problem....if any of the multiple rows is not filled..an error is generated (although the rows that have been filled will be submitted)is there any way i can prevent null values from being sent?..or how can i suppres the "duplicate value for key 1" error?. Thanx

          Comment

          Working...