0

I would like to update a sql table with a html form. I would like to select the element with a drop-down list and later update the values with the form.

Here is my code, the drop-down list and the form works, but I didn't know how to make that the php code get the element that I select.

HTML form:

 <?php
           require("conectarBD.php");

           $select = "SELECT id_serie, nombre FROM series";
           $result = $conectar->query($select);

           ?>

           Selecciona la serie que quieres modificar:

           <br>

           <select>

               <?php    
               while ( $row = $result->fetch_array() )    
               {
                   ?>

                   <option value=" <?php echo $row['id_serie'] ?> " >
                   <?php echo $row['nombre']; ?>
                   </option>

                   <?php
               }    
               ?>

           </select>
           <form action="modificar_serie.php" method="post">
           <p>
              Introduce los cambios a realizar:
           </p>
           <p>
            <label for="textfield">Nombre</label>
            <input type="text" name="nom" id="nom" />
            <label for="textarea"></label>
          </p>
          <p>
            <label for="textfield">Temporadas</label>
            <input type="number" name="temp" id="temp" />
            <label for="textarea"></label>
          </p>
          <p>
            <label for="textfield"> Año de estreno</label>
            <input type="text" name="est" id="est" />
            <label for="textarea"></label>
          </p>
           <input type="Submit" value="Actualizar">
           </form>

PHP:

    <?php
 require("conectarBD.php");

 $nombre = $_POST["nombre"];
 $temp = $_POST["temp"];
 $est = $_POST["est"];

 $query="UPDATE series SET nombre = '.$nombre.', temporadas = '.$temp.', estreno= '.$est.' WHERE nombre='$nombre'";


 mysqli_query($conectar,$query);

     if(mysqli_affected_rows()>=0){
    echo "<p>($nombre) Datos Actualizados<p>";
     }else{
    echo "<p>($nombre) No se ha podido actualizar en estos momentos<p>";
     }


    header("Location: ../index.php");
?>
David Marí
  • 21
  • 1
  • 3
  • I know this doesn't answer your question, but beware of SQL injection. [Here](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php#60496) is a good post that might help you securing your application. – mnme Apr 09 '17 at 14:24

1 Answers1

0

Your select element must be inside form and have a name. Then you will be able to get a value of it from $_POST.

ghostprgmr
  • 488
  • 2
  • 11