doujianwan7570 2015-04-13 21:36
浏览 37
已采纳

php:通过mysql获取选项选择值

I'm trying to have an altas.php file for various html forms. This code is a html form with some mysql columns:

<div><label for="categoria">Categoría principal</label><select type="text" name="categoria">
        <?php 
            $query = "select id, categoria from categorias";
            $result = mysqli_query($mysql,$query);
            if(!$result) echo 'Muy mal....';
            $num_filas = mysqli_num_rows($result);
            for ($i = 0; $i < $num_filas; $i++){
                $row = mysqli_fetch_array($result);
        ?>
        <option value "<?php echo $row['id']?>"><?php echo $row['categoria']?></option><?php
            }   
        ?>
        </select></div>

On the other hand, if I make this:

foreach($_POST as $campo => $valor){
        echo $campo ." = ". $valor ."
";
    }

I have no data in $row['categoria']. It should return the value, which is the categorias's id.

Another question:

How can I use the "foreach" whith an insert sentence?

For example:

 foreach($_POST as $campo => $valor){
$query ="INSERT INTO $tabla ($campo) VALUES ('$valor')";
}

$tabla is sent via $_GET. This code make a new row for each $campo. Any idea?

I have try some suggestions, but nothing works.

  • 写回答

1条回答 默认 最新

  • dongmi6102 2015-04-13 21:42
    关注

    You seem to be missing an = and closing semi-colons on this line:

    <option value "<?php echo $row['id']?>"><?php echo $row['categoria']?></option><?php
    

    Should be:

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

    EDIT: Also removed type from select list

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记