0

我在下面有一段代码,它执行几个查询,如果从前两个查询之一中找到一行,那么它将显示验证错误,如果从 2 个查询中没有找到行,那么它执行一个插入和另一个 SELECT 查询:

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

       $aliasquery = "SELECT StudentAlias FROM Student WHERE StudentAlias = ?";
       // prepare query
       $aliasstmt=$mysqli->prepare($aliasquery);
       // You only need to call bind_param once
       $aliasstmt->bind_param("s",$getalias);
       // execute query
       $aliasstmt->execute(); 
       // get result and assign variables (prefix with db)
       $aliasstmt->bind_result($dbStudentAlias);
       //get number of rows
       $aliasstmt->store_result();
       $aliasnumrows = $aliasstmt->num_rows();

    if ($aliasnumrows == 0){
       if ($numrows == 0){

              $formatdate = date("Y-m-d",strtotime($getdob));
              $studentpassword = md5(md5("93w".$studentpassword."ed0"));  


              $insertsql = "
            INSERT INTO Student
                (StudentForename, StudentSurname, StudentAlias, StudentUsername, StudentPassword, StudentDOB, Year, CourseId)
              VALUES
                (?, ?, ?, ?, ?, ?, ?, ?)
            ";
            if (!$insert = $mysqli->prepare($insertsql)) {
              // Handle errors with prepare operation here
            }                                           

            $insert->bind_param("ssssssis", $getfirstname, $getsurname, $getalias, $getusername, $studentpassword, $formatdate, $getyear, $getcourse);

            $insert->execute();

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

            $insert->close();

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

        }else{
        $errors['username'] = "<span style='color: red'>There is already a Student with that Username</span>";
        }

        }else{
        $errors['alias'] = "<span style='color: red'>There is already a Student with that Alias</span>";
        }
    }

我遇到的问题是,如果两个验证错误都满足,我想同时显示两个验证错误。目前它一次只显示一个验证错误。如果满足两个验证错误,我如何才能将两个验证错误一起显示?

下面是它在表单中存储验证错误的代码:

$error_alias= (!empty($errors['alias']))?$errors['alias']:"";
$error_username = (!empty($errors['username']))?$errors['username']:"";

$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
  <table>
  <tr>
  <td>Alias:</td>
  <td><input type='text' name='alias' value='$getalias' /><br/>".$error_alias."</td>
  </tr>
  <tr>
  <td>Username:</td>
  <td><input type='text' name='studentusername' value='$getusername' /><br/>".$error_username."</td>
  </tr>
  <tr>
  <td></td>
  <td><input type='submit' value='Register' name='registerbtn' /></td>
  </tr>
  </table>
  </form>";

  echo $form;
4

1 回答 1

1

您可以稍微更改 if 语句:

if ($aliasnumrows == 0 && $numrows == 0){
    //Do stuff
}else{
    if($aliasnumrows){
        $errors['alias'] = "<span style='color: red'>There is already a Student with that Alias</span>";
    }
    if($numrows){
        $errors['username'] = "<span style='color: red'>There is already a Student with that Username</span>";
    }
}
于 2012-12-10T00:20:00.363 回答