• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

multiple mySQL inserts from the one form

Im developing a web page for a  php/mySQL server. The page has a dynamic table that is built as the user adds items to a grid.  The table could have one row or hundreds of rows. The table fields all have a row number appended to them. The rows are in ascending order, but not sequential. I need to do multiple mySQL inserts from the one form to save the data.

Example Table:

Row1            width1            height1
Row2            width2            height2
Row5            width5            height5
Row7            width7            height7
 

I need to identify the data in the mySQL table, so I was thinking of using the first mySQL table row number as the identifier, and writing into a field on each insert.  After completing the insert I need to call another page with the identifier attached to the URL.



0
jws2bay
Asked:
jws2bay
  • 5
  • 5
1 Solution
 
Jason C. LevineNo oneCommented:
Hi jws2bay,

So what's the question?

If you want to do multiple inserts from a form, you need to store the form data in arrays and step through the array.

http://www.evolt.org/article/Storing_form_array_data_to_MySQL_using_PHP/18/60222/index.html
0
 
jws2bayAuthor Commented:
Hi jason,

Thanks for the link.
I'm so weak in php that I've never heard of serialize() & unserialize(). Let me try this approach in my application.
0
 
jws2bayAuthor Commented:
I think I have a problem using the serialize(), and unserialize() commands.  In the original post the author tells you to attach [] to the object id tags. Later under another post this is corrected to attaching the [] to the object name tag, not the id tag. My name tags are used in building and manipulation the dynamic table.  Will the serialize()  command work using the object id tags?

Here is a link to my current page so you can see what I need to save to mySQL.

http://www.glassdivider.com/W2-Partition-WS1B_temp5.php 


0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
Jason C. LevineNo oneCommented:
You should add the [] to the name part of the tag.

<input name="yourfield[]" type="text' />

If your table is being built on the fly, you might want to think about adding a little extra code so that you can fill in the array value on each row:

<input name="yourfield[0]" type="text' />
<input name="yourfield[1]" type="text' />
<input name="yourfield[2]" type="text' />

I'm not sure you even need serialize() to do this.  Below is a code snippet (very simple one) for running through an array and inserting the values as a loop.

<?php
mysql_select_db();
if( isset($_POST['Submit']) )
{

for( $i = 0; $i < count( $_POST["ArrayID"] ); $i++ ) /* I always use a hidden field in the repeat region and assign it the incrementing value to make this part a little easier */

         {      

mysql_query("INSERT INTO YourTable (Field1, Field2) VALUES (".$_POST["field1"]["$i"].",".$_POST["field2"]["$i"].")" or die("Update query error: " . mysql_error());

         }
}
?>
0
 
jws2bayAuthor Commented:
My dynamic table is not necessarily sequential. I know the id number of the last row, but there can be rows of the table that were deleted.   Do I need to check to see if the row exists before doing the insert?  How would you handle this in php?
0
 
Jason C. LevineNo oneCommented:
Does the dynamic table get posted each time you add or delete a row? If so, then I misunderstood you and there is a whole different path to go.

If not, then storing all values in an array takes care of it.  In that case, you would not sequentially number the array fields, just keep everything at [] and use the count() function to get the number of elements in the array.  That's what I'm doing in the example above.
0
 
jws2bayAuthor Commented:
The dynamic table only gets posted once as you leave the page.  My table objects names are similar to my first example.  A row value is appended to the object as the table is built, and is required to manipulate the table.  I cant append the [] to the name, so I think this precludes the use of the php sequence() command.

If I just count up until I reach the number of the last row I will be trying to POST deleted rows. I was thinking that I need to test to see if the row exists. If it does POST the row, if it doesnt increment the counter and test again until I reach the row value of the last row in the table

0
 
Jason C. LevineNo oneCommented:
>> I cant append the [] to the name

Why not?

<input name="yourfield[]" type="text" />
<input name="yourfield[]" type="text" />
<input name="yourfield[]" type="text" />

In any event, this is starting to sound less like a Dreamweaver problem and more like a PHP problem.  You may want to consider closing this question and reposting it to the PHP Zone(s) to get people far better with PHP than me looking at it.

0
 
jws2bayAuthor Commented:
Adding the [] to the table names interfers with some of my javascript functions.  I know I'm working outside the Dreamweaver spectrum.  I think I will take your advice repost to the PHP Zone.

Thanks for the help.
0
 
Jason C. LevineNo oneCommented:
Yes...you've run up against my PHP knowledge limits.  Sorry...wish I could have been of more help.
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now