By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,853 Members | 1,012 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,853 IT Pros & Developers. It's quick & easy.

How to use array to submit Multiple Values In Form?

P: 5
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):
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <table>
  4. <form method="post" action="array_post.php"
  5. <?php
  6.  
  7. $i=2;
  8. for($x = 1; $x <=$i; $x++)
  9. {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>';}
  10. ?>
  11. <tr><td colspan="7" align="center"><input type="submit" value="Submit Names"  />
  12. </td></tr></form>
  13. </table>
  14. </body>
  15. </html> 
..
for the code to insert it into the database..this is what i came up with:
Expand|Select|Wrap|Line Numbers
  1.  <?php
  2. include 'poly_connect.php'; 
  3. foreach ($_POST['reg_no'] as $row=>$reg_no)
  4. $id = mysqli_real_escape_string($link,$reg_no);
  5. $first_name = mysqli_real_escape_string($link,$_POST['first_name'][$row]);
  6. $last_name = mysqli_real_escape_string($link,$_POST['last_name'][$row]);
  7. $password=mysqli_real_escape_string($link,sha1($_POST['password'][$row]));
  8.  $sql = "INSERT INTO 
  9.                     students(reg_no,first_name,last_name) 
  10.                 VALUES('" . $reg_no . "','" . $first_name . "', 
  11.                 '" . $last_name . "','" . $password. "'
  12.  
  13.                        )";  
  14.  
  15.         $result = mysqli_query($link,$sql)  or die ("error".mysqli_error($link)); 
  16.  
  17.         ?>
...problem is its not inserting anything..i need help on getting these values inserted and also the "hashing" of the passwords..please help me out.Thanks
Jan 28 '11 #1

✓ answered by Markus

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:
Expand|Select|Wrap|Line Numbers
  1. // Only the expression immediately following the foreach is executed.
  2. // The square of each iteration is not displayed (until the loop exits).
  3. foreach( array(1, 2, 3) as $i )
  4.   echo "Current iteration's value: $i\n";
  5.   echo "Current iteration's value squared: ", ($i * $i), PHP_EOL;
  6.  
  7. // The loop's scope is defined using curly-braces here and works as expected
  8. foreach( array(1, 2, 3) as $i )
  9. {
  10.   echo "Current iteration's value: $i\n";
  11.   echo "Current iteration's value squared: ", ($i * $i), PHP_EOL;
  12. }
  13.  
Fix these issues, try again, and report any issues.

Share this Question
Share on Google+
4 Replies


P: 16
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.
Jan 28 '11 #2

P: 5
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?
Jan 28 '11 #3

Markus
Expert 5K+
P: 6,050
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:
Expand|Select|Wrap|Line Numbers
  1. // Only the expression immediately following the foreach is executed.
  2. // The square of each iteration is not displayed (until the loop exits).
  3. foreach( array(1, 2, 3) as $i )
  4.   echo "Current iteration's value: $i\n";
  5.   echo "Current iteration's value squared: ", ($i * $i), PHP_EOL;
  6.  
  7. // The loop's scope is defined using curly-braces here and works as expected
  8. foreach( array(1, 2, 3) as $i )
  9. {
  10.   echo "Current iteration's value: $i\n";
  11.   echo "Current iteration's value squared: ", ($i * $i), PHP_EOL;
  12. }
  13.  
Fix these issues, try again, and report any issues.
Jan 28 '11 #4

P: 5
Thank you Markus..it worked!!!....now 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
Jan 28 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.