doulan3966 2013-03-25 23:21
浏览 94

PHP变量返回null值

I've created a textbox so when the admin types a name and clicks submit, it will shows a list of retrieved data from the database.

    <form method="post" action="">
        <?php 
        $teacherName = $_POST['teacherName'];
        if ($_POST['submitted'] == 1) {
            if($teacherName != ""){
                $getName = mysql_query("SELECT name, user_id FROM members WHERE name = '$teacherName'");
                $teacherdetails = mysql_fetch_array($getName);
                $teachername = $teacherdetails['name'];
                $teacher_id = $teacherdetails['user_id'];

                if($teachername != ""){
                    print $teachername . "<br/>";
                } else {
                    print "Give a valid name <br/>";
                }     
            }
        }

        if ($teachername == ""){ ?>
        Teacher name:<input type="text" size="20" name="teacherName"><input type="hidden" name="submitted" value="1"><br/>
      <input type="submit" value="Submit" /> 


<?php $getModule = mysql_query("......"); 
          while ($row2 = mysql_fetch_array($getModule)) { ?>   
          <input type="checkbox" name="modules[]" value="<?php print $row2["module_id"]?>"/> <?php print $row2["module_name"] . '<br/>'; } ?>
          </div><br/> <?php } ?>
          <input type="submit" value="Submit" />
</form>

Below I wrote this code (in the same script):

<?php

$modules = $_POST['modules'];

for($i = 0; $i < count($modules); $i++){
    $module=mysql_query("INSERT INTO module (module_id,user_id) VALUES ('$modules[$i]','$teacher_id')"); 
}
?>

but for some reason when I call the variable "$teacher_id" (which is the value I retrieved before from the database. It works fine in the form) it returns nothing. It's null but I can't understand why.

Am I doing something wrong?

  • 写回答

1条回答 默认 最新

  • dounue6984 2013-03-25 23:29
    关注

    First off, put the PHP outside the form tags, at the top.

    Secondly, the data you are receiving could be an array; with more than one result set;

    do this just incase it it returned as that;

     foreach($teacherdetails AS $teacher) {
    //also set the values of the variables here
    echo $teacher['name'];
    echo $teacher['user_id'];
    
    } 
    

    Regarding the last bit, is the $teacher_id successfully printing a result?

    Also, where is the modules being input and posted from?

    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突