RE: [PHP-DB] inserting mysql data using php array???
Interesting. A bit tiedious, I suppose. Seems like it'd be a bit more overhead than I'd care to have, but an interesting technique nonetheless. TIM -I date this girl for two years and then the nagging starts: "I wanna know your name." - Mike Binder > -Original Message- > From: Ubaidul Khan [mailto:[EMAIL PROTECTED]] > Sent: Wednesday, November 28, 2001 10:04 AM > To: Tim Foster > Cc: [EMAIL PROTECTED] > Subject: Re: [PHP-DB] inserting mysql data using php array??? > > > Actually, there is another way of doing it. The prerequisite for this > method, is that you have an auto_incremented column. If you have that, then > all you have to do is insert a value into one of the columns, then grab > mysql_insert_id, and update all columns, corresponding to that > auto_incremented value. That's what I ended up doing. > > > - Original Message - > From: "Tim Foster" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Wednesday, November 28, 2001 10:37 > Subject: RE: [PHP-DB] inserting mysql data using php array??? > > > > Very interesting. I didn't know you could use SET with an INSERT. I tried > this on MySQL > > and it worked. Dunno if it's standard SQL, but it didn't work on MSAccess > and MS SQLServer > > (the only other DBs I have at hand to test it with). I wonder what other > DBs it works on, > > because you could be painting yourself into a corner if you had to move to > another > > platform. > > > > TIM > > -What if the Hokey-Pokey really is what it's all about? > > > > > > > -Original Message- > > > From: Jonathan Hilgeman [mailto:[EMAIL PROTECTED]] > > > > > 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 > > > > > > -- > > PHP Database Mailing List (http://www.php.net/) > > To unsubscribe, e-mail: [EMAIL PROTECTED] > > For additional commands, e-mail: [EMAIL PROTECTED] > > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > > > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP-DB] inserting mysql data using php array???
Actually, there is another way of doing it. The prerequisite for this method, is that you have an auto_incremented column. If you have that, then all you have to do is insert a value into one of the columns, then grab mysql_insert_id, and update all columns, corresponding to that auto_incremented value. That's what I ended up doing. - Original Message - From: "Tim Foster" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Wednesday, November 28, 2001 10:37 Subject: RE: [PHP-DB] inserting mysql data using php array??? > Very interesting. I didn't know you could use SET with an INSERT. I tried this on MySQL > and it worked. Dunno if it's standard SQL, but it didn't work on MSAccess and MS SQLServer > (the only other DBs I have at hand to test it with). I wonder what other DBs it works on, > because you could be painting yourself into a corner if you had to move to another > platform. > > TIM > -What if the Hokey-Pokey really is what it's all about? > > > > -Original Message- > > From: Jonathan Hilgeman [mailto:[EMAIL PROTECTED]] > > > 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 > > > -- > PHP Database Mailing List (http://www.php.net/) > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > To contact the list administrators, e-mail: [EMAIL PROTECTED] > > -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP-DB] inserting mysql data using php array???
Very interesting. I didn't know you could use SET with an INSERT. I tried this on MySQL and it worked. Dunno if it's standard SQL, but it didn't work on MSAccess and MS SQLServer (the only other DBs I have at hand to test it with). I wonder what other DBs it works on, because you could be painting yourself into a corner if you had to move to another platform. TIM -What if the Hokey-Pokey really is what it's all about? > -Original Message- > From: Jonathan Hilgeman [mailto:[EMAIL PROTECTED]] > 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 -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
RE: [PHP-DB] inserting mysql data using php array???
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:[EMAIL PROTECTED]] Sent: Tuesday, November 27, 2001 2:59 PM To: [EMAIL PROTECTED] 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" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP Database Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
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" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]