RE: [PHP-DB] Variable Variables in PHP
| From: | Taylor, Stewart | Date: | Wed, 27 Jun 2001 09:52:27 +0000 |
| Subject: | RE: [PHP-DB] Variable Variables in PHP | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-9858@lists.php.net to get a copy of this message | ||
<?
You are allowed the have input fields with the same name on a form, the
browser creates an array of these fields,
e.g. to access them using javascript form.name[index].value.
You need to inform php that these fields are arrays by putting an '[]' after
the INPUT fields name. Then when the form is submitted php will create
arrays for the multiple input fields.
You can also use an index value to specify each fields location in the
array.
$result = mysql_query("SELECT * FROM name_and_email");
$num = mysql_numrows("$result");
if ($num!="0") {
$i = 0;
PRINT "<FORM action='test.php'>";
WHILE ($i < $num) {
$name = mysql_result($result,$i,name);
$email = mysql_result($result,$i,email);
$id = mysql_result($result,$i,id);
PRINT "<INPUT type='text' name='name[$id]'
value='$name'>";
<---- modified
PRINT "<INPUT type='text' name='email[$id]'
value='$email'><BR>";
<---- modified
$i++;
}
PRINT "<INPUT type='submit'></FORM>";
}
?>
To create the query you can simple loop through the arrays
e.g.
foreach ($name as $k=>$v)
{
$sql = sprintf("UPDATE ".
"name_and_email ".
"SET ".
"name = '%s',email = '%s' ".
"WHERE ".
"id = '%s'", $v, $email[$k], $k);
// run query etc..
}
-----Original Message-----
From: Morgan Tocker [mailto:morgan.tocker@choiceconnect.com.au]
Sent: 27 June 2001 03:04
To: php-db@lists.php.net
Subject: [PHP-DB] Variable Variables in PHP
I am having trouble with variable variables;
I want to create a form as follows :-
<?
$result = mysql_query("SELECT * FROM name_and_email");
$num = mysql_numrows("$result");
if ($num!="0") {
$i = 0;
PRINT "<FORM action='test.php'>";
WHILE ($i < $num) {
$name = mysql_result($result,$i,name);
$email = mysql_result($result,$i,email);
$id = mysql_result($result,$i,id);
PRINT "<INPUT type='text' name='name$id'
value='$name'>";
PRINT "<INPUT type='text' name='email$id'
value='$email'><BR>";
$i++;
}
PRINT "<INPUT type='submit'></FORM>";
}
?>
## THIS SHOULD SUBMIT SOMETHING LIKE THIS >>>>
test.php?name1=avalue&email1=avalueagain&name2=secondname&email2=secondemail
THE PROBLEM:
From PHP with variable variables, how do I change $name1 and $email1 into -
## for all $name.$x values do this......
$sql = "UPDATE name_and_email SET name = '$name1', email = '$email1' WHERE
id = '$id'";
Many thanks if someone can help,
Morgan Tocker.
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, e-mail: php-db-unsubscribe@lists.php.net
For additional commands, e-mail: php-db-help@lists.php.net
To contact the list administrators, e-mail: php-list-admin@lists.php.net