Post values selected from dropdown into mysql

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post values selected from dropdown into mysql

Post by JimiH »

Hello

I have a form which I use to populate a mysql DB with values entered into the form, I am
converting some fields into dropdown menus which are populated through a query on the DB
and the user selects the desired data.

My original HTML code for the field "Salesman" in the DB


Code: Select all

<td>Sales Man</td>
                <td><input name="Salesman" type="text" id="Salesman" size="20"></td>
              </tr>
              <tr>
My equivelent PHP code which displayes the list from the DB

Code: Select all

<td>Sales Man</td>
                <td>
<?


echo"<select name=\"emailsales\">"; 


while($row=mysql_fetch_assoc($my_query)) 
{ 

echo  "<option value=\"".$row['emailsales']."\">".$row['emailsales']."</option>"; 

} 

?>

</td>

The dropdown box works no problem and displays the values from the DB.

How do I pass the selected data to the DB using this method.

Thanks

Geoff

[/img]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The value you've associated with the option chosen will be in the submission data as the select's name.
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Thanks worked fine.


Code: Select all

<?
//echo "Salesman  ";	
echo"<select name=\"Salesman\">"; 
while($row=mysql_fetch_assoc($my_query)) 
{ 
echo  "<option value=\"".$row['emailsales']."\">".$row['emailsales']."</option>"; 
} 
?>
Geoff
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Right thats the "Add" part working how about the "Update" portion of the code
which currently stands as

Code: Select all

<td>Sales Man</td>
                <td>
                <input name="Salesman" type="text" id="Salesman" value="<? echo $Salesman; ?>" size="20"></td>
              </tr>
              <tr>
I give you some background there are two tables one with "Salesmen" and one is the "projects" they are working on.

So the "Add" gathers information from the form and the selection from the "Salesmen" table dropdown box. This information
is posted into the "Projects" table.

However when the update portion of the code the "Salesman" is picked from the "Projects" table and not the "Salesmen" Table.
I need it to display the correct Salesmen & allow me to select from the "Salesmen" table if I need to select a different value.

Hope this makes sense

Geoff
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

I suppose what I'm asking is how do I change this

Code: Select all

echo  "<option value=\"".$row['emailsales']."\">".$row['emailsales']."</option>";
into something to display the value selected originally when I entered the record from the Salesmen table.

Geoff
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Ok

I've been all over Google with no luck

here's the basic problem

Two Table's

TABLE1 & TABLE 2 (mysql)

I display a form with a dropdown box with values pulled from TABLE1, the user selects the desired option
& click "Submit" the value is posted into TABLE2


The user then decides to update there selection, so he clicks the update form.

Here's the problem

How can I get the update form to display the value the user posted into TABLE2 but offer him the selection values from TABLE1 again so he can change the selection & post it again into TABLE1

Thanks for you time.

Geoff
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

JimiH, can you possible edit your posts so that you don't continually reply to your posting? It gives the apearance of bumping, which is heavily frowned upon here.

As for your question, can you clarify it a little bit? You say you have a result set that populate the select list. You post the form containing the select list and you want to capture that value? Is that right?
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Sorry :oops:

The problem is I can post a value selected from a dropdown linked to mysql table1 into another mysql Table2. but I cant display the posted value from Table2 into my update form dropdown box along with the other options from Table1, so the user can change it if required.

I found this which seems to have the answer

http://phpbuilder.com/board/showthread. ... m+dropdown

Thanks

Geoff
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Ok, I am not trying to dumb this down, I swear. I just want to be able to understand what you are doing. Your first page runs a query that pulls all salesmen and populates a select list. Something like this...

Code: Select all

<?php
// Grab the people to make the list from
$sql = "SELECT person_id, person_name FROM people";
if (!$result = mysql_query($sql))
{
    die('Cannot get the person list: ' . mysql_error());
}

// Build a list now
echo '<select name="person">';
while ($row = mysql_fetch_array($result))
{
    echo '<option value="' . $row['person_id'] . '">' . $row['person_name'] . '</option>';
}
echo '</select>';
?>
Your post page then would have something like this to get the value...

Code: Select all

<?php
$person = 0;
if (isset($_POST['person_id']))
{
    $person = $_POST['person_id'];
}

if ($person != 0)
{
    // we have a passed value, lets do something
}
?>
What is it that you want to do on this page in addition to grabbing the passed form value?
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Hi Everah

Thanks for your time.

So ok we got the value selected, I then insert this into another table "projects"

Lets say the value passed needs updating.

How do I display the same selection box from the table "people" with the value passed previously
to the "projects" table?

The selection box would have to populated with all the values from "people" but also the selection
posted into "projects"

Thanks again

Geoff
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

There are two ways to do that... one, grab the information from the 'projects' table as it relates to that one row, and append that into the select list created by the information in 'people' (so essentially two different record sets put together in one output), or... 2) if the information that is in 'projects' already matches what is in 'people', then mark it as 'selected="selected"' by doing an if check while looping the result set from the 'projects' information.
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Thanks

Both options sound fine, I'll try the second option as I already have
the update form with the drop down & the posted value available
in a variable.

Drop down list code as follows

Code: Select all

<?
//echo "Salesman  ";	
echo"<select name=\"Salesman\">"; 
while($row=mysql_fetch_assoc($my_query)) 
{ 
echo  "<option value=\"".$row['emailsales']."\">".$row['emailsales']."</option>"; 
} 
?>
Previously posted value

Code: Select all

echo $Salesman
How would I match the value contained in Variable $Salesman with the dropdown list values

Thanks

Geoff
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Code: Select all

<?php
echo"<select name=\"Salesman\">";
while($row=mysql_fetch_assoc($my_query))
{
    $selected = ( $Salesman == $row['emailsales'] ) ? ' selected="selected"' : '';
    echo  '<option value="'.$row['emailsales'].'"' . $selected . '>'.$row['emailsales'].'</option>';
}
?>
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Thanks for all your help Everah.

I'm at home now so I'll try the code tomorrow & see how I get on.

Thanks again

Geoff
JimiH
Forum Commoner
Posts: 92
Joined: Thu Jun 15, 2006 6:10 am

Post by JimiH »

Hi

I think there may be something wrong with the formatting

When I type it in the page goes blank, i've checked it over and over

Geoff
Post Reply