0

I hope someone can help me figure this out as I'm new to PHP and MySQL. But basically, what I'm trying to do is to pull the data from the database and populate the dropdown box and textbox, and so I can update the value in the textbox.

This is the table structure:

id | hf_name | hf_price
------------------------
AI | test 1  | 123
AI | test 2  | 123

So let's say I wanna update "test 1" price, I would select "test 1" in the dropdown selection and update the price in the textbook.

This are my code:

<form class='data_form' action='data/food_update.inc.php' method='post'>
    <select>
        <?php while ($row = mysqli_fetch_array($result)):; ?>
            <option><?php echo $row[1];?></option>
        <?php endwhile;?>
    </select>
    <input type='number' name='hf_price' placeholder='$'>
    <button type='submit'>UPDATE</button>
</form>

Include:

<?php

    //CONNECTION TO MySQL   
    include '../dbh.php';

    //VARIABLES
    $hf_name = $_POST['hf_name'];
    $hf_price = $_POST['hf_price'];
    $by_user = $_SESSION['Fname'];
    $up_date = date('Y-m-d'); 

        $sql = "SELECT hf_name FROM hotfoods";
        $result = mysql_query($conn, $sql);

?>

Thanks!

  • 1
    **Stop** using deprecated `mysql_*` API. Use `mysqli_*` or `PDO` – Jens Apr 28 '17 at 09:35
  • 1
    In your `include` you have `mysql_query()` and in your actual code you have `mysqli_fetch_array()`. I'm pretty sure they can't be used together, perhaps you had a typo and meant `mysql_fetch_array()` without the **i**. But as Jens says, you should stop using mysql_* functions and switch over to something like PDO or mysqli. – Elijah Apr 28 '17 at 09:46
  • Also try using `` PHP arrays start at 0, you're attempting to get the result from index 1 which I would imagine wouldn't exist from your SQL query. – Elijah Apr 28 '17 at 09:50
  • I donna ansa questions that contain the word 'wanna'. Jus' sayin'. – Strawberry Apr 28 '17 at 09:52
  • Yes thank you for pointing that one out. It was a typo, but even if I use mysqli, it still gives me an empty dropdown box. – crispywisp Apr 28 '17 at 09:54
  • Please edit your question. – Strawberry Apr 28 '17 at 10:00

4 Answers4

0

Try changing the option to

<option value="<?php echo $row[2];?>"><?php echo $row[1];?></option>

Where I'm assuming $row[2] variable contains the hf_price value.

gaganshera
  • 2,629
  • 1
  • 14
  • 21
0

but for this you need to change your id datatype into int,

 $query = "select * from tablename";
   $result = mysqli_query();

     <select id="nameID" name="hf_name">
          <?php 
            while($row = mysqli_fetch_assoc($result){
              ?>
          <option value='<?php echo $row['id']; ?>'><?php echo $row['hf_name']; ?></option>
    <?php
     }
      ?>
   </select>

This will be fine when you fetch details but it does not update any value because for this you need to use Ajax or Jquery for update on Select, let me to try it.

Ajax

$("#nameID").onchange(function(){
    var id = $("#id").val();
    $.ajax(
        url: "update.php",
        type: "POST",
        data: {'id'=id},                   
        success: function(){
               alert("ok");                                    
         });
});

Try this it might work.

Salman Khan Sohoo
  • 97
  • 1
  • 1
  • 13
  • in the webpage I'm working on, I would like to have a dropdown box populated by the name of the food (hf_name) and a text box (hf_price) populated by the price depending on the name of the food selected. let's say I wanna update the price of a specific food, I would select the name of the food from the dropdown box, and change the price from the textbox. Hope this is clear? Thanks. – crispywisp Apr 28 '17 at 09:47
  • Update in database column? – Salman Khan Sohoo Apr 28 '17 at 09:54
  • yes. update the price (hf_price) in the database column. – crispywisp Apr 28 '17 at 09:59
  • I've updated the answer check and try it. but must change things according to your requirements. – Salman Khan Sohoo Apr 28 '17 at 10:02
0
     $query = "select * from tablename";
       $result = mysqli_query($query);

         <select>
              <?php 
                while($row = mysqli_fetch_assoc($result){
                  ?>
              <option value='<?php echo $row['hf_name']; ?>'><?php echo $row['hf_name']; ?></option>
        <?php
         }
          ?>
       </select>
rupesh
  • 277
  • 3
  • 15
0

You should assign a name attribute value (in this case, hf_name) on select element so that that become available in $_POST array.

<form class='data_form' action='data/food_update.inc.php' method='post'>
    <select name="hf_name">
        <?php while ($row = mysqli_fetch_array($result)):; ?>
            <option value="<?php echo $row['hf_name']?>"><?php echo $row['hf_name'];?></option>
        <?php endwhile;?>
    </select>
    <input type='number' name='hf_price' placeholder='$'>
    <button type='submit'>UPDATE</button>
</form>

Now this is on which the form will be submitted. Where you can take user's submitted data through $_POST array.

<?php

    //CONNECTION TO MySQL   
    include '../dbh.php';

    //VARIABLES
    $hf_name = $_POST['hf_name'];
    $hf_price = $_POST['hf_price'];

    $query = sprintf("UPDATE hotfoods SET hf_price='%d' WHERE hf_name='%s'",
        mysql_real_escape_string($hf_price),
        mysql_real_escape_string($hf_name));

    $result = mysql_query($conn, $query);
    if ($result) {
        echo 'Price updated!';
    } else {
        echo 'Problem while updating price';
    }

?>
unclexo
  • 3,691
  • 2
  • 18
  • 26