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?

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?