Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update database with value from listbox (function)

Status
Not open for further replies.

drmsolutions

Programmer
Joined
Mar 7, 2004
Messages
6
Location
AU
I have the following function to create a listbox which is used several times in my scripts for differnt listboxes:

Code:
function enhanced_list_box($options){

  $sql  = "select " . $options['id_field'];
  $sql .= ", " . $options['value_field'];
  $sql .= " from " . $options['table'];
     
  $result = mysql_query($sql)
            or die("error in SQL");

echo '<select name="'. $options['id_field']. '" size="1">';


while ($row = mysql_fetch_array($result, MYSQL_NUM))
{
   if($row[1] == $options['highlight_id']) {
    echo '<option value="'. $row[0]. '" SELECTED>'.
         $row[1]. '</option>';
  } else {
    echo '<option value="'. $row[0]. '">'.
         $row[1]. '</option>';
  }
}

echo '</select>';
}

My first instance of the listbox is for the user to select a title:

Code:
<? enhanced_list_box(array(
  				'table'        => 'ctitle',
  				'id_field'     => 'id',
  				'value_field'  => 'title',
  				'highlight_id' => $Title));
				?>

My question is how do I now update the value chosen by the user into the database?

I've tried:

Code:
"UPDATE customers SET "
                       ." Title=\"".$_POST.$options['title'][1]."\"";

But that just updates the database with "Array".
Any ideas?

Thanks in advance
 
It's probably your use of this expression:

$_POST.$options['title'][1]

$_POST is an array. Elements of that array are singleton values.

You may need to do something like:

$_POST[$options['title']]

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Thanks for your help sleipnir,

When I put
$_POST[$options['title']]

I got the error:

Parse error: parse error, unexpected '[' in update.php on line 501

Which makes me think I should be using
$_POST$options['title']

but that sets the value to 0 (seems $options['title] doesn't have a value outside the function?)
 
Actually it gives a value of 'array' too, not 0
 
You're right. $options will only exist within the function. But you're handing the function the values it uses the create $options. What's the source of that value?

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
The values are coming from a seperate table (ctitle) which contains a list of titles (Mr, Mrs, Miss etc). The function goes off and gets those and "selects" the one that matches the 'title' in the customer record.

So what I'm trying to do is allow the user to change the title listed in the customer record. It selects the one that's already in there, but what if the user changes it from Mr to Mrs. I can't work out what the variable name is?

As you can see I'm new to PHP!

Thanks for your help
 
The value of an input element in a POST-method form will be found in:

$_POST['name_of_input_element'];

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top