Form post data to database by keys

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
djdavedawson
Forum Newbie
Posts: 9
Joined: Tue Jul 29, 2008 10:13 am

Form post data to database by keys

Post by djdavedawson »

:?:
I have a form that generates using a for() loop. Here is part of the code:

Code: Select all

 
$result = "one,two,three";
$items = explode(",",$result);
$num = count($items);
 
echo " <form id=\"form1\" name=\"form1\" method=\"post\" action=\"testform2.php?action=process\">
        <table width=\"50%\">";
 
for ($c=0; $c < $num; $c++) 
 
{echo  "
<tr><Name<input type=\"text\" name=\"name$c\" id=\"\" /></td>
<tr>Email<input type=\"text\" name=\"email$c\" id=\"\" /></td>
<tr>City<input type=\"text\" name=\"city$c\" id=\"\" /></td>
 
 
 
 
So i end up with input rows like this:

name0, email0, city0
name1, email1, city1
name2, email2, city2

When the form is submitted, i need the data to write to my db, where each row in the db will be data from the corresponding keys.

TIA

DD
djdavedawson
Forum Newbie
Posts: 9
Joined: Tue Jul 29, 2008 10:13 am

Re: Form post data to database by keys

Post by djdavedawson »

I figured it out. :P :D

I had the $i in the wrong place

Here's what I did:

Code: Select all

  
for ($i=0; $i < $num; ++$i) 
{
$name = $_POST["name[b][color=#000000]$i[/color][/b]"];
$email = $_POST["email[b][color=#000000]$i[/color][/b]"];
$city = $_POST["city[b][color=#000000]$i[/color][/b]"];
 
$sql_data_array = array(
                  array('fieldName'=>'name', 'value'=>$name, 'type'=>'string'),
                  array('fieldName'=>'email', 'value'=>$email, 'type'=>'string'),
                  array('fieldName'=>'city', 'value'=>$city, 'type'=>'string'),
                   );
 
Thanks Everyone

DD
User avatar
Skara
Forum Regular
Posts: 703
Joined: Sat Mar 12, 2005 7:13 pm
Location: US

Re: Form post data to database by keys

Post by Skara »

Input tags can use arrays just fine, btw:

Code: Select all

<input type="text" name="name[$c]" id="" />

Code: Select all

$name = $_POST['name'][$i];
djdavedawson
Forum Newbie
Posts: 9
Joined: Tue Jul 29, 2008 10:13 am

Re: Form post data to database by keys

Post by djdavedawson »

Thanks, I like that better, much cleaner. :P
Post Reply