insert data from dropdown-menu list into mysql table

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
phpDVWeaver
Forum Commoner
Posts: 28
Joined: Sun Apr 29, 2007 11:00 pm

insert data from dropdown-menu list into mysql table

Post by phpDVWeaver »

can any one show me how to be able to insert all the values in a list (dropdown-menu list of size 5) into the table.

Thanks
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: insert data from dropdown-menu list into mysql table

Post by califdon »

phpDVWeaver wrote:can any one show me how to be able to insert all the values in a list (dropdown-menu list of size 5) into the table.
What table? What records? What fields? What are you trying to do?
phpDVWeaver
Forum Commoner
Posts: 28
Joined: Sun Apr 29, 2007 11:00 pm

Post by phpDVWeaver »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


sorry for not being that clear;

I do have a dropdown-menu that can show 5 values at the sametime, example:
[syntax="html"]
<select name="projarea" size="5">
   <option>Bionanotechnology</option>
   <option>Computational Nanotechnology</option>
   <option>Nanaocharacterization</option>
    <option>Nanoelectromechanical System</option>
    <option>Nanoelectronics</option>
    <option>Nanofabrication</option>
    <option>Nanomanufacturing</option>
     <option>Nanomaterials</option>
     <option>Nanophotonics</option>
</select>
I need to add these values to a MySql table called projArea that has the following definition:

Code: Select all

id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
area VARCHAR(100)
hope it's clearer now.


feyd | Please use[/syntax]

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

That code doesn't let you select multiple items...


Anyway, I'd imagine it'd be posted as an array, at which point you do a foreach loop. Try var_dump($_POST) after submitting the form and see what you get.
phpDVWeaver
Forum Commoner
Posts: 28
Joined: Sun Apr 29, 2007 11:00 pm

Post by phpDVWeaver »

No am not dealing with multiple selection at all here.

thanks for the hint I'll try it.
phpDVWeaver
Forum Commoner
Posts: 28
Joined: Sun Apr 29, 2007 11:00 pm

foreach does not take a dropdown menu as an array! any idea

Post by phpDVWeaver »

this is my 'list'

Code: Select all

<select name="projarea" size="5">
 <option>Bionanotechnology</option>
 <option>Computational Nanotechnology</option>
<option>Nanaocharacterization</option>
<option>Nanoelectromechanical System</option>
<option>Nanoelectronics</option>
 <option>Nanofabrication</option>
 <option>Nanomanufacturing</option>
 <option>Nanomaterials</option>
 <option>Nanophotonics</option>
</select>
here I assign the variables

Code: Select all

$projtitle = trim($_POST['projtitle']);
$projurl = trim($_POST['projurl']);
$projarea = trim($_POST['projarea']);
here I use the foreach to insert values in the table project.

Code: Select all

foreach($projarea as $tempprojarea)
{
  $query = "INSERT INTO project(projtitle, projurl,projarea) VALUES ('$projtitle', '$projurl','$tempprojarea')";
  mysql_query($query) or die("Query failed: " . mysql_error());
}
but this is the message I got.
Warning: Invalid argument supplied for foreach()
Can anyone drop an idea.
Again what I am trying to do is to insert ALL the data in a dropdown menu into a mysql table (I am not dedealing with only selected values here)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$projarea is a string.
phpDVWeaver
Forum Commoner
Posts: 28
Joined: Sun Apr 29, 2007 11:00 pm

Post by phpDVWeaver »

ok, is there any way to assign the values of the dropdown menu to a table so that I use that table in foreach
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You don't need foreach at all.
phpDVWeaver
Forum Commoner
Posts: 28
Joined: Sun Apr 29, 2007 11:00 pm

Post by phpDVWeaver »

ok, that good what should I do then.
Post Reply