duanjiyun7391 2012-09-05 04:33
浏览 28
已采纳

在PHP中检索值

Hello having a bit of problem retrieving a value so i might use it. This is is where i should get my value.

<input type="text" class="typed" readonly="readonly" name="levele"
    value="<?php
    if($get_display->level== "Grade 2") {
        echo "Grade 4"; $lbl="Grade 4";
    }?>">

For a simple test i will use the value Grade 4 because i know what the outcome I'm suppose to have. This is the code that i want to retrieve the value of "levele" so it may perform a SQL statement. For example i have 5 sections that are for grade 4 so i was thinking on using this sql statement.

<select name="section_id">
    <option>SECTIONS</option>
    <?php 
        $result = mysql_query("SELECT * FROM tbl_section where level='".$_GET['levele']."'");
        while($row = mysql_fetch_array($result))
        {
            echo '<option value='.$row['section_id'].'>' .$row['section_name']. '</option>';
        }
    ?>  
</select>

So far i am unable to retrieve "levele" so it may be used it the sql, also tried $_POST and $_request but nothing and i check if levele does have the value Grade 4 and it does also in my database. Would appreciate any help on this. Because after i have retrieve the 5 sections I will try to automatically select one section where for example if the student has a grade of 90 the section should be "A" I'm going to do it with if else. so i would appreciate any help to solve these problems.

  • 写回答

2条回答 默认 最新

  • dongqiang4819 2012-09-05 04:43
    关注

    I see three main problems here that I think you should address:

    Firstly, in your code at the top:

    <input type="text" class="typed" readonly="readonly" name="levele" value="<?php if( 
         $get_display->level== "Grade 2"){ echo "Grade 4"; $lbl="Grade 4";}?>">
    

    Your code will only echo and set a value if $get_display->level is equal to 2. Did you mean to add in an else statement as well in case the value is something other than Grade 2 perhaps?

    <?php 
        if($get_display->level== "Grade 2")
        { 
            echo "Grade 2"; $lbl="Grade 2";
        }
        else
        {
            echo "Grade 4"; $lbl="Grade 4";
        }
    ?>
    <input type="text" class="typed" readonly="readonly" name="levele" value="<?php echo $lbl; ?>">
    

    Secondly, your form section of code seems to be missing quotes around the values:

    echo '<option value='.$row['section_id'].'>' .$row['section_name']. '</option>';
    

    I think you meant to use this:

    echo '<option value="'.$row['section_id'].'">' .$row['section_name']. '</option>';
    

    Lastly, your code is very vulnerable to injection attacks. You really should look at moving to PDO and prepared statements when dealing with form data.

    Edit: This is just an afterthought, but I can't see anywhere that you set $result to a valid connection, but I assume that this is somewhere else in your code.

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

报告相同问题?

悬赏问题

  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”