dpbf62565 2017-04-09 14:19
浏览 40

用表单更新Mysql

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");
?>
  • 写回答

1条回答 默认 最新

  • dongzhang2150 2017-04-09 14:26
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法