0

我想显示下面所有错误的列表,但它一次只显示一个错误,有谁知道为什么我的代码没有显示错误列表?下面是显示存储错误的数组的代码,说明没有错误时会发生什么的代码,以及说明有错误时会发生什么的代码。

    <?php 

  $getcourseid = (isset($_POST['courseid'])) ? $_POST['courseid'] : '';
  $getcoursename = (isset($_POST['coursename'])) ? $_POST['coursename'] : '';
  $getduration = (isset($_POST['duration'])) ? $_POST['duration'] : '';

        $errors = array();


                  if (!$getcourseid){
                      $errors[] = "You must enter in Course's ID";
              }else if (!$getcoursename){
                  $errors[] = "You must enter in Course's Name";
              }else if (!$getduration){
                      $errors[] = "You must select Course's Duration";
                  }   

          if(!$errors) {

         $insertsql = "
        INSERT INTO Course
            (CourseNo, CourseName, Duration)
          VALUES
            (?, ?, ?)
        ";
        if (!$insert = $mysqli->prepare($insertsql)) {
          // Handle errors with prepare operation here
        }                                           

        $insert->bind_param("sss", $getcourseid, $getcoursename, $getduration);

        $insert->execute();

        if ($insert->errno) {
          // Handle query error here
        }

        $insert->close();

         // don't use $mysqli->prepare here
    $query = "SELECT CourseNo FROM Course WHERE CourseNo = ?";
    // prepare query
    $stmt=$mysqli->prepare($query);
    // You only need to call bind_param once
    $stmt->bind_param("s",$getcourseid);
    // execute query
    $stmt->execute(); 
    // get result and assign variables (prefix with db)
    $stmt->bind_result($dbCourseId);
    //get number of rows
    $stmt->store_result();
    $numrows = $stmt->num_rows();  

              }  



            if(empty($errors)) {
                if ($numrows == 1){
               $errormsg = "<span style='color: green'>Course " . $getcourseid .  " - "  . $getcoursename . " has been Created</span>";
               $getcourseid = "";
               $getcoursename = "";
               $getduration = "";
            }else{
                $errormsg = "An error has occured, Course has not been Created";
            }
            } else {
                if (count($errors) > 0)
                {
                    foreach ($errors AS $Errors)
                    {
                        $errormsg = "{$Errors} <br>"; 
                    }
                }
            }     
        ?>

**ADDITIONAL QUESTION:**



    $form = "
    <form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
      <table>
      <tr>
      <td></td>
      <td id='errormsg'>$errormsg</td>
      </tr>
      <tr>
      <td>Course ID:</td>
      <td><input type='text' name='courseid' value='$getcourseid' /></td>
      </tr>
      <tr>
      <td>Course Name:</td>
      <td><input type='text' id='nameofcourse' name='coursename' value='$getcoursename' /></td>
      </tr>
      <tr>
      <td>Duration (Years):</td>
      <td>{$durationHTML}</td>
      </tr>
      <tr>
      <td></td>
      <td><input type='submit' value='Create Course' name='createbtn' /></td>
      </tr>
      </table>
      </form>";

      echo $form;

上面的表格位于此问题顶部的 php 代码下方。现在发生的事情是验证错误列表都显示在表单的顶部。但我真正想要的是每个验证错误都低于它所指的表单功能。

例如,如果我没有填写课程 ID 文本输入,那么我希望验证错误"You must enter in Course's ID"显示在课程 ID 文本输入下方。

如果我没有填写课程名称文本输入,那么我希望验证错误"You must enter in Course's Name"显示在课程名称文本输入下方。

等等。为了做到这一点,需要对代码进行哪些更改?

4

3 回答 3

1

尝试

      if (!$getcourseid){
          $errors[] = "You must enter in Course's ID";
       } 
     if (!$getcoursename){
         $errors[] = "You must enter in Course's Name";
      }
      if (!$getduration){
          $errors[] = "You must select Course's Duration";
      } 
于 2012-12-09T06:16:20.793 回答
1
if (!$getcourseid)
{
   $errors[] = "You must enter in Course's ID";
} 
if (!$getcoursename)
{
   $errors[] = "You must enter in Course's Name";
} 
if (!$getduration)
{
   $errors[] = "You must select Course's Duration";
}     
于 2012-12-09T06:17:41.667 回答
1

我还没有完成与持续时间相关的代码,您需要弄清楚我在这里为您提供了输入 courseid 和 coursename 的逻辑。

    <?php 


    $errors = array();
    $getcourseid    = "";
    $getcoursename  = "";
    $errormsg = "";
        if($_POST)
        {
            $getcourseid    = $_POST['courseid'];
            $getcoursename  = $_POST['coursename'];
            if (!$getcourseid){
                  $errors['course_id'] = "You must enter in Course's ID";
            }if (!$getcoursename){
              $errors['course_name'] = "You must enter in Course's Name";
            }
        }



      if(!$errors && $_POST) {

     $insertsql = "
    INSERT INTO Course
        (CourseNo, CourseName, Duration)
      VALUES
        (?, ?, ?)
    ";
    if (!$insert = $mysqli->prepare($insertsql)) {
      // Handle errors with prepare operation here
    }                                           

    $insert->bind_param("sss", $getcourseid, $getcoursename, $getduration);

    $insert->execute();

    if ($insert->errno) {
      // Handle query error here
    }

    $insert->close();

     // don't use $mysqli->prepare here
$query = "SELECT CourseNo FROM Course WHERE CourseNo = ?";
// prepare query
$stmt=$mysqli->prepare($query);
// You only need to call bind_param once
$stmt->bind_param("s",$getcourseid);
// execute query
$stmt->execute(); 
// get result and assign variables (prefix with db)
$stmt->bind_result($dbCourseId);
//get number of rows
$stmt->store_result();
$numrows = $stmt->num_rows();  

          }  

$getcourseid = (!empty($getcourseid))?$getcourseid:"";
$getcoursename = (!empty($getcoursename))?$getcoursename:"";
$durationHTML  = (!empty($durationHTML))?$durationHTML:"";


$error_c_id = (!empty($errors['course_id']))?$errors['course_id']:"";
$error_c_name = (!empty($errors['course_name']))?$errors['course_name']:"";

$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
  <table>
  <tr>
  <td>Course ID:</td>
  <td><input type='text' name='courseid' value='".$getcourseid."' />".$error_c_id."</td>
  </tr>
  <tr>
  <td>Course Name:</td>
  <td><input type='text' id='nameofcourse' name='coursename' value='".$getcoursename."' />".$error_c_name."</td>
  </tr>
  <tr>
  <td>Duration (Years):</td>
  <td>{$durationHTML}</td>
  </tr>
  <tr>
  <td></td>
  <td><input type='submit' value='Create Course' name='createbtn' /></td>
  </tr>
  </table>
  </form>";

  echo $form;
?>
于 2012-12-09T07:11:18.950 回答