Update database with value from listbox (function)

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
drmsolutions
Forum Newbie
Posts: 5
Joined: Sat Mar 06, 2004 11:03 pm

Update database with value from listbox (function)

Post by drmsolutions »

I have the following function to create a listbox which is used several times in my scripts for differnt listboxes:

Code: Select all

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: Select all

<? 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: Select all

"UPDATE customers SET "
                       ." Title="".$_POST.$options['title'].""";
But that just updates the database with "Array".
Any ideas?

Thanks in advance
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Looks like the select form element is $options['id_field'] ? So wouldn't you want, $_POST[$options['id_field']] ?
drmsolutions
Forum Newbie
Posts: 5
Joined: Sat Mar 06, 2004 11:03 pm

Post by drmsolutions »

Thanks for your help Mark,

The problem is $options['id_field'] doesn't have a value outside of the function. Is there a way I can do this?

Also I was using 'title' because this is an update form for customer details. The function fills the select box with values from another table called 'ctitle' and then 'selects' the value that matches the 'title' column in the customer details (i.e Mr, Mrs, Miss etc)

I want the user to be able to change the customer's title, but just can't work out how to post the new value if they select a new one?

Perhaps I'm approaching it in the wrong way?
(I'm quite new to PHP in case you hadn't noticed!)
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Well, the problem is the select name is dynamic, so outside of the function you don't know what it's called. So i think you have 2 choices.
1. Prefix the select name with a common prefix, like 'sel_<whatever>' then you can check the posted keys to see if it begins with sel_
2. Used a fixed select name.

I'd go for option 2 ;) Any fields/values you need to do the form processing (other than those displayed) i'd just put it hidden form fields. Without seeing the full code this is as far as my thinking takes me, but i hope it makes some sorta sense :o
Post Reply