RE: [PHP-DB] inserting mysql data using php array???
| From: | Jonathan Hilgeman | Date: | Tue, 27 Nov 2001 23:04:39 +0000 |
| Subject: | RE: [PHP-DB] inserting mysql data using php array??? | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-14630@lists.php.net to get a copy of this message | ||
I actually have an easy way to do INSERTs like this.
First I create the array:
$MyArray["field"] = value;
$MyArray["name"] = "Jonathan";
$MyArray["age"] = 123;
$MyArray["email"] = $email_address;
and then I define this function somewhere in a function library:
function Array2Query($dbArray)
{
foreach($dbArray as $dbField => $dbValue)
{
$dbQuery .= "$dbField='$dbValue',";
}
$dbQuery = substr($dbQuery,0,strlen($dbQuery)-1);
return $dbQuery;
}
and then I just create the query:
$Query = "INSERT INTO table SET " . Array2Query($MyArray);
$Result = mysql_query($Query);
Hope it helps.
- Jonathan
-----Original Message-----
From: justin gruenberg [mailto:justin@ice.kewlio.net]
Sent: Tuesday, November 27, 2001 2:59 PM
To: php-db@lists.php.net
Subject: Re: [PHP-DB] inserting mysql data using php array???
meaning
INSERT INTO table (first_name,middle_name,last_name,suffix)
VALUES($an_array)
no. that is not possible.
you do actually have to write it out..
INSERT INTO table (first_name,middle_name,last_name,suffix)
VALUES('$name[0]', '$name[1]', '$name[2]', $name[3])
I dont understand why you have these in arrays if they are only simple text
inputs. it'd be much easier to understand your code visually if you had the
name of each field actually descriptive of the content in it. It is also
not valid html to have [ ]'s in the name parameter, I believe.
----- Original Message -----
From: "Ubaidul Khan" <ubaidulkhan@hotmail.com>
To: <php-db@lists.php.net>
Sent: Tuesday, November 27, 2001 9:45 AM
Subject: [PHP-DB] inserting mysql data using php array???
Hi
I am running Apache 1.3.20 with MySQL 3.23.41, and PHP 4.0.6, on Linux
Mandrake 8.1. I am trying to access a MySQL database from a PHP script. I
have a html form, that takes input in an array (ie. name[0], name[1],
name[2], name[3], these fields represent first_name, middle_initial,
last_name, and suffix in the database). What I would really like to do is,
take this array of data and insert each element in the corresponding column,
in the database. Is it possible to insert each element of the array into
the database, using an array, instead of typing out each value?
--
Thanks
--
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