Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
Get value from MySQL database and populate dropdown box

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!

over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

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.

over 4 years ago · Santiago Trujillo Denunciar

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.

over 4 years ago · Santiago Trujillo Denunciar

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>
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda