Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with multi-form elements into MySQL DB 1

Status
Not open for further replies.

rninja

Technical User
Apr 11, 2001
381
US
I am trying to create a scalable form in php. I have lets say about 30 items being retrieved from a MySQL database into a php form which can be resubmitted back to make modifications to the database entries.

What I am having trouble doing is getting those items (as many as there will be) to go back into the database as sets of information of the same type. Here's an example:

Here is a set of information in a html table by row-
id name quantity color

Let' say I have 30 of these rows on this form page, I want to press submit and send it to a php page which intern takes those sets by row (I've run a for loop to assign each row identical numbers such as:

id0 name0 quantity0 color0
id1 name1 quantity1 color1
id2 name2 quantity2 color2
ETC....

How do I write a script which will submit these elements dynamically into the database as sets?
I tried doing a while-each display of the $_POST variables and it's fine, but I have no idea how I will get the elements into a database without hardcoding each element...

I appreciate the feedback!



Rninja

sl_sm.jpg

 
Here's one way to do this.


First you have to output the form in the correct format:
Code:
<?
$q = "select * from mytable";
$rs = mysql_query($q);
while ($rw = mysql_fetch_assoc($rs) {
  echo '<input name=name[' . $rw['id'] . '] type=text value="' . $rw['name'] . '">';
  echo '<input name=quantity[' . $rw['id'] . '] type=text value="' . $rw['quantity'] . '">';
  echo '<input name=color[' . $rw['id'] .'] type=text value="' . $rw['quantity'] . '">';
  echo "<br>\n"; }
echo '<input type=submit name=submit value="Do It">';
?>
For the code that processes the input:
Code:
<?
if (isset($_POST['submit'])) {
$ids = array_keys($_POST['name']); // get which IDs are used
for ($i=0;$i<count($ids);$i++) {
   $tmp = array();
   $q = "select * from mytable where id='" . $ids[$i] . "'";
   $rs = mysql_query($q);
   $rw = mysql_fetch_assoc($rs);
   if ($_POST['name'][$ids[$i]] != $rw['name'])
       $tmp[] = "name='" . $_POST['name'][$ids[$i]] . "'";
   if ($_POST['color'][$ids[$i]] != $rw['color'])
       $tmp[] = "color='" . $_POST['color'][$ids[$i]] . "'";
   if ($_POST['quantity'][$ids[$i]] != $rw['quantity'])
       $tmp[] = "quantity='" . $_POST['quantity'][$ids[$i]] . "'";
   if (!empty($tmp)) {
       $qu = "update mytable set " . implode(',',$tmp) . "where id='" . $ids[$i] . "'";
       $rsu = mysql_query($qu); }
}
?>

Play with this code. It should be pretty close to what you are looking for.

Note: I have not tested this code for typing errors. I have not done any error checking or validation of the form input.

Ken
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top