Jump to content

sending multiple data to mysql db


JazzyB

Recommended Posts

Hi,

I am trying to send multiple data via one form. I have 4 tables each in the same form. Here is an example:

[code]
<form name="form1" id="form1" method="post" action="process.php">
  <table width="428" border="0" cellspacing="6" cellpadding="0">
    <tr>
      <td>Field 1: </td>
      <td><input type="text" name="field1" /></td>
    </tr>
    <tr>
      <td>Field 2:</td>
      <td><input type="text" name="field2" /></td>
    </tr>
    <tr>
      <td>Field 3:</td>
      <td><input type="text" name="field3" /></td>
    </tr>
    <tr>
      <td>Field 4:</td>
      <td><input type="text" name="field4" /></td>
    </tr>
  </table>
  <br />
  <table width="428" border="0" cellspacing="6" cellpadding="0">
    <tr>
      <td>Field 1: </td>
      <td><input type="text" name="field1" /></td>
    </tr>
    <tr>
      <td>Field 2:</td>
      <td><input type="text" name="field2" /></td>
    </tr>
    <tr>
      <td>Field 3:</td>
      <td><input type="text" name="field3" /></td>
    </tr>
    <tr>
      <td>Field 4:</td>
      <td><input type="text" name="field4" /></td>
    </tr>
  </table>
<br />
  <table width="428" border="0" cellspacing="6" cellpadding="0">
    <tr>
      <td>Field 1: </td>
      <td><input type="text" name="field1" /></td>
    </tr>
    <tr>
      <td>Field 2:</td>
      <td><input type="text" name="field2" /></td>
    </tr>
    <tr>
      <td>Field 3:</td>
      <td><input type="text" name="field3" /></td>
    </tr>
    <tr>
      <td>Field 4:</td>
      <td><input type="text" name="field4" /></td>
    </tr>
  </table>
  <br />
  <table width="428" border="0" cellspacing="6" cellpadding="0">
    <tr>
      <td>Field 1: </td>
      <td><input type="text" name="field1" /></td>
    </tr>
    <tr>
      <td>Field 2:</td>
      <td><input type="text" name="field2" /></td>
    </tr>
    <tr>
      <td>Field 3:</td>
      <td><input type="text" name="field3" /></td>
    </tr>
    <tr>
      <td>Field 4:</td>
      <td><input type="text" name="field4" /></td>
    </tr>
  </table>

  <p>
    <input type="submit" name="Submit" value="Submit" />
  </p>
</form>
[/code]

Is it possible to send all this data to a mysql database, so that 4 rows are inserted in one go? If so please could you provide me with any info that would help. It would help a lot if anyone can provide an example.

Many thanks in advance

Jaz
Link to comment
https://forums.phpfreaks.com/topic/24107-sending-multiple-data-to-mysql-db/
Share on other sites

field1 in the last section will override field1 in the previous sections etc so you only get the last set of values.

Rename the input fields as field1[], field2[] etc in all 4 sections then
[code]
<?php
for ($i = 0; $i < 4; $i++) {
    $f1 = get_magic_quotes_gpc() ? $_POST['field1'][$i] : addslashes($_POST['field1'][$i]);
    $f2 = get_magic_quotes_gpc() ? $_POST['field2'][$i] : addslashes($_POST['field2'][$i]);
    $f3 = get_magic_quotes_gpc() ? $_POST['field3'][$3] : addslashes($_POST['field3'][$i]);
    $f4 = get_magic_quotes_gpc() ? $_POST['field4'][$i] : addslashes($_POST['field4'][$i]);
    mysql_query("INSERT INTO tablename (col1, col2, col3, col4)
            VALUES ('$f1', '$f2', '$f3', '$f4')");
}
?>
[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.