【发布时间】:2016-09-23 20:30:56
【问题描述】:
感谢您抽出宝贵时间。我目前正在参加在线 PHP 课程,并试图找出课程中未涵盖的内容。 有一种形式,如果根据某些参数存在错误,则将它们放入数组中。目前,当向用户显示错误时,它会显示 $_POST 值,但我想显示自定义错误消息。我目前正在尝试使用 in_array 和字符串替换,但失败了。我没有收到任何 PHP 错误。 有谁知道我该怎么做? 这是我的控制器和视图:
<?php
// add database connection script
include_once 'resource/database.php';
include_once 'resource/utilities.php';
// process the form if the reset password button is clicked
if(isset($_POST['passwordResetBtn'])) {
// initialize an array to store any error message from the form
$form_errors = array();
// form validation
$required_fields = array('email', 'new_password', 'confirm_password');
// call the function to check empty fields and merge the return data into form_error array
$form_errors = array_merge($form_errors, check_empty_fields($required_fields));
// Fields that require checking for minimum length
$fields_to_check_length = array('new_password' => 6, 'confirm_password' => 6);
// call the function to check minimum required length and merge the return data into form_errors array
$form_errors = array_merge($form_errors, check_min_length($fields_to_check_length));
// email validation / merge the return data into the form_errors array
$form_errors = array_merge($form_errors, check_email($_POST));
// check if error array is empty, if yes process form data and insert record
if(empty($form_errors)) {
// collect form data and store in variables
$email = $_POST['email'];
$password1 = $_POST['new_password'];
$password2 = $_POST['confirm_password'];
// check if the new password and confirm password are the same
if($password1 != $password2) {
$result = "<p style='padding: 20px; border: 1px solid red; color: red;'>Passwords do not match, please do it over!</p>";
} else {
try {
// create sql select statement to verify if email address input exists in the database
$sqlQuery = "SELECT email FROM users WHERE email = :email";
$statement = $db->prepare($sqlQuery);
$statement->execute(array(':email' => $email));
// check if record exists
if($statement->rowCount() == 1) {
// hash the password
$hashed_password = password_hash($password1, PASSWORD_DEFAULT);
// SQL statement to update password
$sqlUpdate = "UPDATE users SET password = :password WHERE email = :email";
// sanitize the statement
$statement = $db->prepare($sqlUpdate);
// execute statement
$statement->execute(array(':password' => $hashed_password, ':email' => $email));
$result = "<p style='padding: 20px; border: 1px solid green; color: green;'>Password reset successfully</p>";
} else {
$result = "<p style='padding: 20px; border: 1px solid red; color: red;'>The email address provided does not exist in our database. Check your spelling or try another email address, por favor.</p>";
}
} catch (PDOException $ex) {
$result = "<p style='padding: 20px; border: 1px solid red; color: red;'>An error occurred: " . $ex->getMessage() . "</p>";
}
}
} else {
if(count($form_errors) == 1) {
$result = "<p style='color: red;'>There was one error in the form</p><br>";
} else {
$result = "<p style='color: red;'>There were " . count($form_errors) . " errors in the form</p><br>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Password Reset Page</title>
</head>
<body>
<?php if(isset($result)) echo $result; ?>
<?php if(!empty($form_errors)) echo show_errors($form_errors); ?>
<form method="post" action="">
<table>
<tr><td>Email:<td><input type="text" value="" name="email"></td></td></tr>
<tr><td>New Password<td><input type="password" value="" name="new_password"></td></td></tr>
<tr><td>Confirm Password:</td> <td><input type="password" value="" name="confirm_password"></td></tr>
<tr><td></td><td><input style="float: right;" type="submit" name="passwordResetBtn" value="Reset Password"></td></tr>
</table>
</form>
<p><a href="index.php">Back</a></p>
</body>
</html>
这里是来自 utility.php 的函数来检查字段的最小长度:
function check_min_length($fields_to_check_length) {
// initialize an array to store error messages
$form_errors = array();
foreach($fields_to_check_length as $name_of_field => $minimum_length_required) {
if(strlen(trim($_POST[$name_of_field])) < $minimum_length_required) {
$form_errors[] = $name_of_field . " is too short, must be {$minimum_length_required} characters long";
}
}
return $form_errors;
}
这是我尝试查找 $_POST['new_password'] 值并将其换成字符串“新密码”的蹩脚尝试
第一次尝试)
if(isset($_POST[$name_of_field]) && $_POST[$name_of_field] == $_POST['new_password']) {
$name_of_field = str_replace($name_of_field, "New password", $_POST['new_password']);
}
第二次尝试)
if(in_array($_POST['new_password'], $form_errors)) {
$_POST['new_password'] = str_replace($_POST['new_password'], "New Password", $form_errors);
}
我已经尝试将我的尝试放在主控制器和 check_min_length 函数中的几乎所有地方。我知道我的尝试是可笑的。我仍在尝试学习基本的 PHP 和一般编程。
感谢您的宝贵时间。
【问题讨论】:
-
您需要打开 php.ini 中的 display_errors 并重新启动 apache 或检查您的错误日志以找到您遇到的错误
-
谢谢你,利亚姆。我的显示错误在 MAMP 中默认开启。我假设我的 PHP 没有错误,但代码没有做任何事情,因为我基本上没有针对任何东西。再说一次,我还是新手,所以我对这个回复的信心很低。
-
如果您不确定,那么您可以随时检查您的错误日志并查看您的最后一个错误,就好像您收到一个空白屏幕一样,您的错误可能不会显示,但可以在顶部覆盖您的代码使用 ini_set () 并覆盖 ini 设置
-
由于某种原因,在我的 MAMP 中的 PHP 信息页面中,它说错误已打开,但在我的 ini 文件中却说它们没有打开。我一直收到错误,但不是我在您指示我访问的日志文件中看到的错误。非常感谢你带我去那里。
标签: php str-replace