PHP - 函数不在isset中返回值

时间:2015-07-08 07:13:19

标签: php

我有一个函数调用find_student_by_id(),下面有一个arg完整代码。

function find_student_by_id($student_number){
  global $con;
  $safe_student_number = prep($student_number);

  $sql  = "SELECT * ";
  $sql .= "FROM studeprofile ";
  $sql .= "WHERE StudentNumber = '{$safe_student_number}'";
  $sql .= "LIMIT 1";
  $student_set = mysqli_query($con, $sql);
  confirm_query($student_set);
  if($student = mysqli_fetch_assoc($student_set)){
    return $student;
  } else {
    return null;
  }
}

如果我在页面的任何地方调用它,这个函数工作正常,但如果我在isset函数内使用它,则不返回任何value

$student = find_student_by_id('id-201');

如果在isset之外使用,它会给我价值echo $student['student_number'];

但如果我在isset内使用它,则不会返回任何值。

if(isset($_POST['submit'])){
  $student_number = $student['student_number'];
}

完整的代码 cor.php

<?php
  $student = find_student_by_id($_GET['student_number']);
  //if(!$student){
  //  redirect_to('home.php');
  //}
  //$_SESSION['sn'] = $student['StudentNumber'];
?>
<?php
  if(isset($_GET['subject_id'])){
    $subject = find_subject_id($_GET['subject_id']);
  }
  //if(!$subject){
  //  redirect_to('cor.php');
  //}

?>
<?php
  if(isset($_POST['submit'])){

  $subject_code = $subject['SubjectCode'];
  $description = $subject['Description'];
  $lec_unit = "";
  $lab_unit = "";
  $section = "";
  $labtime = "";
  $sched_time = "";
  $room = "";
  $instructor = "";
  $sem = "";
  $sy = "";
  $sn = $student['StudentNumber'];
  $term = "";
  $fn = $student['FirstName'];
  $mn = $student['MiddleName'];
  $ln = $student['LastName'];
  $course = $student['Course'];
  $yl = $student['YearLevel'];

  $sql  = "INSERT INTO registration(";
  $sql .= "SubjectCode, Description, LecUnit, LabUnit, ";
  $sql .= "Section, LabTime, SchedTime, Room, Instructor, ";
  $sql .= "Sem, SY, StudentNumber, Term, FirstName, MiddleName, ";
  $sql .= "LastName, Course, YearLevel";
  $sql .= ")VALUES(";
  $sql .= "'{$subject_code}', '{$description}', '{$lec_unit}', '{$lab_unit}', '{$section}', '{$labtime}', ";
  $sql .= "'{$sched_time}', '{$room}', '{$instructor}', '{$sem}', '{$sy}', '{$sn}', ";
  $sql .= "'{$term}', '{$fn}', '{$mn}', '{$ln}', '{$course}', '{$yl}'";
  $sql .= ")";
  $cor = mysqli_query($con, $sql);
  if($cor){
    redirect_to('cor.php?student_number='.$subject_code);
  } else {
    die(mysqli_error($con));
  }

  }
?>


<div>
  Student Number : <?php echo $student['StudentNumber']; ?><br />
  Name: <?php echo fullname($student); ?>

  <a class="popup" href="search-subject-cor.php">Add subject</a>

  <br /> <br />
  <p><?php echo $subject['SubjectCode']; ?></p>
  <p><?php echo $subject['description']; ?></p>

  <form method="post" action="cor.php">
    <input type="submit" name="submit" value="Add subject" />
  </form>

1 个答案:

答案 0 :(得分:1)

if(isset($_GET['subject_id'])){
         ^
    $subject = find_subject_id($_GET['subject_id']);
}

将其与

进行比较
<form method="post" action="cor.php">
              ^

并删除问题:)