dongyi1777 2014-02-07 03:44
浏览 39
已采纳

如何从帖子编辑的下拉列表中获取所选值

I have the database for categories where i have id and name rows

<select name="category_id">
    <option value="<?php if($category_id === 1) { echo 'selected';} ?>">Electronics</option>
    <option value="<?php if($category_id === 2) { echo 'selected';} ?>">Automotive</option>
    </select>

for some reason this does not show which one was selected when trying to edit the submitted post

and i do retrieve the category_id like so:

$res6 = mysql_query("SELECT * FROM `posts` WHERE `id` = '" . $id . "' LIMIT 1"); 

    if(mysql_num_rows($res6) > 0){
        while($row6 = mysql_fetch_assoc($res6)){
            $id = $row6['id'];
            $category_id = $row6['category_id'];
            $price = $row6['price'];
            $cover = $row6['cover'];
            $title = $row6['title'];
            $description = $row6['description'];
            }
    }   
  • 写回答

3条回答 默认 最新

  • doucongqian6644 2014-02-07 03:46
    关注

    I believe MySQL results are always returned as strings, so $category_id === 1 will be false.

    Either try $category_id === "1" or $category_id == 1

    Also, you need to echo the selected="selected outside the value attribute

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制