Jump to content


Photo

sending multiple data to mysql db


  • Please log in to reply
2 replies to this topic

#1 JazzyB

JazzyB
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 16 October 2006 - 03:19 PM

Hi,

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

<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>

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

#2 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 16 October 2006 - 03:28 PM

insert into table (column1, column2, column3) values ( 'value1','value2','value3') , ('value1','value2','value3'), etc.. ";
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 16 October 2006 - 08:15 PM

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
<?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')");
}
?>

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users