0

以下のコードでは、いくつかのクエリを実行します。最初の 2 つのクエリのいずれかから行が見つかった場合、検証エラーが表示されます。2 つのクエリから行が見つからない場合は、 insert と別の 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>";
        }
    }

私が抱えている問題は、両方の検証エラーが満たされた場合に両方の検証エラーを同時に表示したいということです。現時点では、検証エラーを 1 つずつ表示するだけです。両方の検証エラーが満たされた場合、両方の検証エラーを一緒に表示するにはどうすればよいですか?

以下は、検証エラーをフォームに格納するコードです。

$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 に答える