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

Process Multi Record Form

P: 1
Hi,

Iam pulling my hair for the below problem since many days!

1. Display a multi record block in a form through PHP

2. On click of submit button, form should collect the data from multi record block and store them in DB using mySQL

If this forum can help on this problem, I am eternally greatful.

Thanks
A PHP Lover
Jan 13 '12 #1
Share this Question
Share on Google+
1 Reply


dlite922
Expert 100+
P: 1,584
Usually you need to be more specific, give us code and tell us where you're stuck, but here's a quick run down in pseudo-code:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. // get the $records you want to display if any
  4.  
  5. if($_POST['save']) {
  6.    $db = <create_db_connection>;
  7.    $firstArr = $_POST['first']; 
  8.    $secondArr = $_POST['second'];
  9.  
  10.    // do your validation, if using mysql, use mysql_real_escape_string() function 
  11.    // if validation successfull, loop and save to DB
  12.  
  13.    for($i = 0; isset($firstArr[$i]); $i++) {
  14.        // save value to db
  15.        $db->query("INSERT INTO table SET first = '{$firstArr[$i]}', second = '{$secondArr[$i]}';");
  16.  
  17.    }
  18.  
  19.    // go to next page using header("Location: newpage?status=success");
  20.    // or just display a message
  21.  
  22.  
  23. }
  24. ?>
  25. .
  26. . HTML HEADERS
  27. <form method='post' action='' >
  28. <table>
  29. <?php 
  30.    // create record block (assuming $records is an array of objects)
  31.    foreach($records as $rec) {
  32.      echo "<tr><td>";
  33.      echo "<input type='text' name='first[]' value='$rec->first' />";
  34.      echo "</td><td>";
  35.      echo "<input type='text' name='second[]' value='$rec->second' />";
  36.      echo "</td></tr>"; 
  37.    }
  38. ?>
  39. </table>
  40. <input type="submit" name="save" value="Save Records" />
  41. </form>
  42. .
  43. .
  44. .
  45. END HTML footer
  46.  
Let me know if any questions,


Dan
Jan 13 '12 #2

Post your reply

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