Loop through form to enter data into database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
colmtourque
Forum Newbie
Posts: 20
Joined: Wed Aug 25, 2004 11:59 am

Loop through form to enter data into database

Post by colmtourque »

If this is covered somewhere sorry, but I'm not even sure how to search on the problem.
(feel free to point)
I have got a database setup as such
(I'm simplifying the structure a little)
table1.userid, table1.name
table2.userid, table2.phonetype, table2.phonenumber
table3.userid, table2.addresstype, table3.address

I have a form that users can use to edit there data.

It pulls up data from each table and populates a form.
What I want to know is how do I let users edit multiple rows in a table at once. Say for instance they have a work and home address they want to update, how could they do this.
I have setup the form (using just the phone as example:

Code: Select all

$result3= mysql_query("SELECT IDNumber AS r3IDNumber, Type, Prefix, Number, Extension FROM Phones $where ORDER BY type",$db) or die("Query failed : " . mysql_error());	
if ($myrow3 = mysql_fetch_array($result3)) 
{
echo "<table>";
echo "<tr><td>Type</td><td>Prefix</td><td>Number</td><td>Extension</td><td>Delete</td></tr>\n";
do 
{
$phonecount=$phonecount+1;
echo "<tr><td><input type=text name=phonetype$phonecount value='".$myrow3["Type"]."' size=3></td>";
echo "<td><input type=text name=phoneprefix$phonecount value='".$myrow3["Prefix"]."' size=3></td>";
echo "<td><input type=text name=phonenumber$phonecount value='".$myrow3["Number"]."' size=8></td>";
echo "<td><input type=text name=phoneext$phonecount value='".$myrow3['Extension']."'  size=4></td>";
echo "<td><input type=checkbox name=daddr value=daddr></td></tr>";
} while ($myrow3 = mysql_fetch_array($result3));
echo "</table>";
I'm just not sure what to do once the onsubmit is clicked.
I figure a loop but not sure what to name the variables in the sql.

thanks
ColmTourque
colmtourque
Forum Newbie
Posts: 20
Joined: Wed Aug 25, 2004 11:59 am

Came up with something

Post by colmtourque »

This works!! Just had to use the right notation for an array on the form.
form page

Code: Select all

$result3= mysql_query("SELECT IDNumber AS r3IDNumber, Type, Prefix, Number, Extension FROM Phones $where ORDER BY type",$db) or die("Query failed : " . mysql_error());	
if ($myrow3 = mysql_fetch_array($result3)) 
{
 echo "<table>";
echo "<tr><td>Type</td><td>Prefix</td><td>Number</td><td>Extension</td><td>Delete</td></tr>\n";
do 
{
$phonecount=$phonecount+1;
 echo "<tr><td><input type=text name=ptypearray[$phonecount] value='".$myrow3["Type"]."' size=3></td>";
echo "<td><input type=text name=pprefeixarray[$phonecount] value='".$myrow3["Prefix"]."' size=3></td>";
echo "<td><input type=text name=pnumarray[$phonecount] value='".$myrow3["Number"]."' size=8></td>";
echo "<td><input type=text name=pextarray[$phonecount] value='".$myrow3['Extension']."'  size=4></td>";
echo "<td><input type=checkbox name=daddr value=daddr></td></tr>";
} while ($myrow3 = mysql_fetch_array($result3));
echo "</table>";
The onsubmit page

Code: Select all

for ($i= 1;$i<= $phonecount; $i++)
		{
echo $i." $ptypearray[$i]<br>";
$result3= mysql_query("UPDATE Phones SET Type='$ptypearray[$i]', Prefix='$pprefixarray[$i]', Number='$pnumarray[$i]', Extension='$pextarray[$i]' WHERE IDNumber='$IDNumber' AND type='$ptypearray[$i]'",$db) or die("Query failed : " . mysql_error());	
}
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

suggestion.. quote your html tag attributes.
Post Reply