dropdown populate from db and update db

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gab
    New Member
    • Jul 2007
    • 5

    dropdown populate from db and update db

    Hi I am very new to this and is stuck trying to make this work... :-(

    I want a 2 dropdown box'es which is populated from a mysql db (that I can do, I think :-p )
    [PHP]
    $sql="SELECT X FROM table WHERE Y=Z";
    $result=mysql_q uery($sql);

    $options="";

    while ($row=mysql_fet ch_array($resul t)) {

    $X=$row["X"];
    $options.="<opt ion VALUE=\"$X\">". $X.'</option>';
    }
    ?>

    <SELECT NAME=something>
    <OPTION VALUE=0>choose
    <?php echo $options ?>
    </SELECT>[/PHP]

    I then want a button which runs a mysql update with the selected input in the dropdown boxes

    the DB table is:
    name
    choice1 (should be from dropdownbox1)
    choice2 (should be from dropdownbox2)

    also (if possible) I would like that the selected value is the one that is the one in the db.

    Really hope someone can help me :-)
  • pbmods
    Recognized Expert Expert
    • Apr 2007
    • 5821

    #2
    Heya, gab. Welcome to TSDN!

    For XHTML compliance, consider making your tags all lowercase.

    As for your question, Create a form around your select elements, and set its action to the page you want to use to update the form (generally, this should be a separate page):
    [code=php]
    <?php
    if(! empty($_GET['message']))
    echo "<div style=\"text-align:center;\" >$_GET[message]</div>";
    ?>
    <form action="dbi/updatePrefsOrWh atever.php" method="get">
    <select name="choice1">
    <?php
    $sql = "SELECT * FROM X ...";
    .
    .
    .
    ?>
    </select>

    <select name="choice2">
    <option value="0">Choos e:</option>
    .
    .
    .
    </select>
    </form>
    [/code]

    Then in updatePrefsOrWh atever.php:
    [code=php]
    If(empty($_GET['choice1']) && empty($_GET['choice2'])) {
    header('Locatio n: theOtherPage.ph p?message=Pleas e%20make%20a%20 choice');
    exit();
    }

    $name = ''; // Not sure what goes here.
    $choice1 = intval($_GET['choice1']);
    $choice2 = intval($_GET['choice2']);

    // connect to mysql db here.

    $sql = "INSERT INTO `X` (`name`, `choice1`, `choice2`) VALUES('$name', $choice1, $choice2)";
    if(mysql_query( $sql))
    $message = 'Success';
    else
    $message = urlencode(mysql _error());

    header("Locatio n: theOtherPage.ph p?message=$mess age");
    exit();
    [/code]

    Comment

    • gab
      New Member
      • Jul 2007
      • 5

      #3
      Thanks alot for taking time to help me :-)

      I have been trying to mess a little around with your supplied code, but I'm still nok getting there :-p (yes I am very green at this...)

      I think I maybe forgot to mention something before...
      I have a table which is supposed to fill the dropdowns (choice_tabel).

      Then there is a table (table) which is supposed to be like:
      username,choice ,choice2
      username2,choic e,choice2


      its should:
      -fill the dropdown boxes with different data from DB (can only fill the first dropdown with below code)
      -be able to update the DB with the selected choices (or make a new db entry if one's username doesn't have one already)
      -when it shows the dropdown boxes, if ones username is already in the table then ones previus choice should be selected in the dropdownbox.

      thx again :-)

      [PHP]<?php
      //index.php

      //connecting to DB

      $username = 'thisismyname'; //going to be dynamic later

      $sql="SELECT choice FROM choice_tabel WHERE something...";

      $result=mysql_q uery($sql);


      $options="";
      $options2="";

      while ($row=mysql_fet ch_array($resul t)) {

      $choice=$row["choice"];
      $options.="<opt ion VALUE=\"$choice \">".$choice .'</option>';
      }

      $sql2="SELECT choice FROM choice_tabel WHERE something else...";
      $result2=mysql_ query($sql2);
      while ($row2=mysql_fe tch_array($resu lt2)) {

      $choice2=$row2["choice"];
      $options2.="<op tion VALUE=\"$choice 2\">".$choice2. '</option>';
      }
      ?>


      <form action="update. php" method="get">
      <SELECT NAME=dropdown1>
      <OPTION VALUE=0>choose 1
      <?php echo $options ?>
      </SELECT>

      <SELECT NAME=dropdown2>
      <OPTION VALUE=0>choose 2
      <?php echo $options2 ?>
      </SELECT>
      ...
      <input type="Submit" name="submit" value="submit">
      </form>




      //update.php

      <?php

      If(empty($_GET['choice']) && empty($_GET['choice2'])) {
      header('Locatio n: index.php?messa ge=Please%20mak e%20a%20choice' );
      exit();
      }

      //$username = ''; // Not sure what goes here.
      $choice = intval($_GET['choice']);
      $choice2 = intval($_GET['choice2']);

      // connecting to DB here.

      $sql = "INSERT INTO `tabel` (`username`, `choice`, `choice2`) VALUES('$userna me', $choice, $choice2)";
      if(mysql_query( $sql))
      $message = 'Success';
      else
      $message = urlencode(mysql _error());

      header("Locatio n: index.php?messa ge=$message");
      exit();

      ?>[/PHP]

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, gab.

        Originally posted by gab
        [code=html]
        <SELECT NAME=dropdown1>
        [/code]
        [code=php]
        $choice = intval($_GET['choice']);
        [/code]
        You're gonna want these to match:

        [code=html]
        <select name="choice">
        [/code]
        [code=php]
        $choice = intval($_GET['choice']);
        [/code]

        Comment

        Working...