0

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>
7
  • $row = mysql_fetch_array($sql), Ahh! no. where are you getting $sql from? this line should rather be like: $row = mysqli_fetch_assoc($rslt)
    – DirtyBit
    Sep 25, 2015 at 13:47
  • pls check updated one
    – Bangalore
    Sep 25, 2015 at 13:48
  • 1
    you're still mixing mysql API's with mysqli
    – DirtyBit
    Sep 25, 2015 at 13:49
  • please remove "echo $row;" inside select Sep 25, 2015 at 13:50
  • 1
    @HawasKaPujaari working now .thanks.... pls add ur comment as answer i can accpet that
    – Bangalore
    Sep 25, 2015 at 13:56

1 Answer 1

2

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

2
  • i will accept result in 3 minutes, meanwhile one more doubt i need to send this selected drop down list value to php file for insertion. so how could i get selected value from dropdown?
    – Bangalore
    Sep 25, 2015 at 13:59
  • @Bangalore this is exactly what I was going to comment next, you need to fetch the corresponding values from the database in a way like: while($row = mysqli_fetch_assoc($rslt)) { $db_cat_value = $row['id']; $db_cat_name = $row['code']; ?> <option value="<?php echo $db_cat_value; ?>" ><?php echo $db_cat_name;?></option> Where I have taken a dummy column id which in your case might be different.
    – DirtyBit
    Sep 25, 2015 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.