Jump to content


This topic is now archived and is closed to further replies.


dynamic table help

Recommended Posts

I have a registrations form that selects a field from a 2nd table.

User table has a field called country in it.

The data is selected from the "id" field of the country table and posted to the user table's "country" field.

Country table has 2 fields

id and country_id

this all works howver....I now need for the client/user to update details one of which may be the country field in his/hers user information.

I cannot get his field to work as it needs to pull the country name from the coutry table..to give the name correctly... also it need to have the pull down menu still so the user can reselect a new country if needed.

Can anyone help....I have be rying things for hours.


Share this post

Link to post
Share on other sites
Maybe I was not very clear.

[u]Table User_detail[/u]

id(autoInc), username, password, address1, address2, zip, country(id from country table), email, phone.

[u]Table country[/u]

id(auto inc), country


User needs to update record and this need to pull the country data and then update if userchanges it.

This should be a pull down menu.

I can get some of it working but feel an easier way of doing it maybe out there.

So the User picks update details.

Form shows all fields that had been filled in before.

User updates them and presses submit.

Any Ideas.

Getting a little lost in php/mysql today.

Cannot see wood for the trees.


Share this post

Link to post
Share on other sites
i'm not quite sure i fully understand what you're asking, but is this right?

dropdown menus:
country [ blah ]
county [ blah ]

user picks a country and then list of counties are populated based on country. then what? you want the user to be able to go back and change country again?

or are you asking this?

user picks a country and county and it updates just fine. now you want them to be able to go and edit/update it later on, so if user goes to edit information, it shows the SAME thing, only the dropdowns by default show what they already have?

if that is the case, then :

i assume that what you are doing is running a while loop to populate your dropdown with selectable items. just insert an if statement inside that while statement something like this:

while ($country=mysql_fetch_array($result)) { //or however you have it
echo "<option name='blah' value='$country' ";
if ($country==$usercountry) { echo "selected"; }
echo ">". $country."</option>";

or something like that do you understand?

Share this post

Link to post
Share on other sites
at the moment I have got my product field semi working however it updates the product name but does not select it when going to edit the record.

functions.php code


function SelectProducts($selected = "", $i = "") {

  //write the query to pull all the images
  $sql = "SELECT * FROM products";

  //perform the query
  $result = mysql_query($sql);

  //if it doesn't work..
  if(!$result) {
    echo('Error retrieving data from db ' . mysql_error());

  //initialise $writeoption
  $writeoption = "";

  //start the select statement
  $startselect = '<select name="product_name' . $i . '"><option value=""></option>';

  //put the results into an array
  while($row = mysql_fetch_array($result)) {
  $count = mysql_num_rows($result);
        $pid             = $row['idp'];
        $selectProducts  = $row['product_name'];

    $writeoption .= ( '<option value="' . $pid . '" ' . (($pid == $selected ) ? 'selected="selected"' : '') . '>' . $selectProducts . '</option>');

  } //close the while loop

  //end select
  $endselect = '</select>';

  return($startselect . $writeoption . $endselect);


Called from edit.php
using this code functions.php is called by the file.

<?php echo SelectProducts(product_name); ?> [/code]


Share this post

Link to post
Share on other sites
so when you do this:

<?php echo SelectProducts(product_name); ?>

it actually makes the dropdown and it's populated but it doesn't default to the one in the database?

of hand i'd say you aren't passing variables to the function correctly.

Share this post

Link to post
Share on other sites
should I be creating a join...I am still learning. and finding it very steep at the moment....


Share this post

Link to post
Share on other sites
you're not passing variables correctly to your function. you call it, sending 1 variable but the function declares 2 vars. and you are also setting $selected and $i to = " " in your function declaration. this overwrites the variable that you are passing to it. and i have no idea what $count is for. you don't even use it.

also at no point in time do i see you actually getting the user's current selection. you are trying to pass 'product_name' to the function as $selected but that's not defined anywhere, and also it should have a $ in front of it, if you DID define it somewhere else.

ALSO, after all that being fixed, you don't seem to have any form tags anywhere, so your dropdown box is not going to work properly, even if you manage to get the right option tag to say selected.

look, here is a quick version i made that works. it's an all-inclusive script so your version probably won't look the same as this.

// connect to the database to get user's information
     $conn = @mysql_connect("localhost","dbusername","dbpassword") or die(mysql_error());
   $rs = @mysql_select_db("dbname",$conn) or die(mysql_error());

//select user's information    
     $sql = "select * from accounts where custname='Stan'";
     $query = mysql_query($sql,$conn);
     $custinfo = mysql_fetch_array($query);
//here is your function to build the dropdown box
     function dropdown($current) {
      global $conn; //just here so we can use the same connection
//get list of products from the table
            $sql = "select * from productlist";    
        $query = mysql_query($sql,$conn);
            $productbox = '<select name="product_name">';

            while($product = mysql_fetch_array($query)) {
        $pid             = $product['serialnum'];
        $pname  = $product['name'];
        $productbox .= '<option value="'.$pid.'"';

// insert 'selected' inside the option tag if the current product matches the user's currently selected product
                if ($pname == $current) { $productbox .= ' selected'; }

                $productbox .= '>'.$pname.'</option>';

      $productbox .= '</select>';

//return the entire dropdown box string
            return $productbox;

//build a quick form
     $form = '<form method = "post" action = "blah.php">';

//call the dropdown function, passing the customer's current selection to it
     $form .= dropdown($custinfo['productname']);
   $form .= '<input type = "submit" value = "update">';
     $form .= '</form>';
     echo $form;

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.