Page 1 of 1

insert data from dropdown-menu list into mysql table

Posted: Mon Jun 11, 2007 3:15 pm
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

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

Posted: Mon Jun 11, 2007 4:01 pm
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?

Posted: Mon Jun 11, 2007 4:24 pm
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]

Posted: Mon Jun 11, 2007 4:32 pm
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.

Posted: Mon Jun 11, 2007 4:40 pm
by phpDVWeaver
No am not dealing with multiple selection at all here.

thanks for the hint I'll try it.

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

Posted: Tue Jun 12, 2007 7:07 pm
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)

Posted: Tue Jun 12, 2007 7:59 pm
by feyd
$projarea is a string.

Posted: Tue Jun 12, 2007 8:18 pm
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

Posted: Tue Jun 12, 2007 8:19 pm
by feyd
You don't need foreach at all.

Posted: Tue Jun 12, 2007 8:36 pm
by phpDVWeaver
ok, that good what should I do then.