简体   繁体   中英

How can I get the ID as value and Name to display in dropdownlist php

i have a problem with that i need to get the value from the dropdown list to be a number and the name for a kategory to be the name that the user picks.

<select name="kategori">
<?php
    $query=mysql_query("SELECT KategoriID from Kategori");
    $second=mysql_query("SELECT KategoriNavn from Kategori");
    while($r=mysql_fetch_row($query) && $v=mysql_fetch_row($second)){
        echo "<option value='$r[0]>$v[0]</option>";
    }
?>

This is the code i have, but i cant make it to work. Im kinda new to PHP. Thanks!

There's no need to write two different queries. You could have written just a single one. I think mysql fetch_assoc is a tad easier to understand.

You can try something like this:

  <?php

     $query = mysql_query("SELECT KategoriID, KategoriNavn  from Kategori") or die(mysql_error()); // Debugging displays SQL syntax errors, if any.

     echo "<pre>";
     print_r($query);
     exit;              // Let me know what the array looks like.

     while ($r= mysql_fetch_assoc($query)) { ?>

   <option value=<?php echo $r['KategoriID']; ?> > 
      <?php echo $r['KategoriNavn']; ?>
   </option>

   <?php } ?>

   <?php 

         echo "<pre>";
         print_r($_POST); // Do this where you're checking your POST data
         exit;
     ?>

Assuming you want option value to be KategoriNavn and the option to display to be KategoriID.

Hope this helps.

Peace! xD

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM