简体   繁体   中英

How to display value in <select> from mysql db using php?

I have a form in php where I need to select value from DB (phpmyadmin) using php. When I add my code its not working. Can anyone please check this code?

    <form role="form" action="test.php" enctype="multipart/form-data" method="post" accept-charset="utf-8">
    <div class="form-group">
      <label for="name">Name:</label>
      <input type="text" class="form-control" name="name" id="name" placeholder="Enter Category Name">
    </div>
    <div class="form-group">
      <label for="code">Code:</label>
       <select>
      <?php 
/* change character set to utf8 */
        if (!mysqli_set_charset($conn, "utf8")) {
            printf("Error loading character set utf8: %s\n", mysqli_error($conn));
            exit();
        } else {
            $rslt=mysqli_query($con,"SELECT * from category") or die(mysqli_error($conn));
        }

        while ($row = mysql_fetch_array($rslt)){
              echo $row;
                echo "<option value=\"owner1\">" . $row['code'] . "</option>";
        }
    ?>
        </select>
    </div>
     <div class="form-group">
      <label for="rank">Rank:</label>
      <input type="text" class="form-control" name="rank" id="rank" placeholder="Enter Category Rank ">
    </div>
    <div class="form-group">
      <label for="tag">Tags:</label>
      <input type="text" class="form-control" name="tag" id="tag" placeholder="Enter Category tag ">
    </div>

    <button type="submit" class="btn btn-default">Done</button>
  </form>

Moving forward from the comments, this is what was needed:

Instead of:

while($row = mysql_fetch_array($sql)){

Should have been:

while($row = mysqli_fetch_assoc($rslt){

Notice: There are a few other warnings too. But that's only if you care. cheers

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