2016-09-23 173 views
1

並感謝您的時間。我目前正在參加一個在線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> 

這裏是utilities.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']值,將其交換字符串 「新密碼」

1日未遂)

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']); 
} 

第2次嘗試)

if(in_array($_POST['new_password'], $form_errors)) { 
    $_POST['new_password'] = str_replace($_POST['new_password'], "New Password", $form_errors); 
} 

我試圖把我的嘗試幾乎無處不在主控制器和check_min_length函數中。我知道我的嘗試是可笑的。我仍然在努力學習基本的PHP和編程。

謝謝你的時間。

+0

你需要在你的php.ini中打開display_errors並重啓apache或者檢查你的錯誤日誌以找到你正在得到的錯誤 –

+0

謝謝Liam。我的顯示錯誤在MAMP默認打開。我假設我的PHP沒有錯誤,但代碼沒有做任何事情,因爲我基本上沒有瞄準任何東西。然後再次,我還是新的,所以我的信心非常低。 – Collin512

+0

如果你不確定,那麼你總是可以檢查你的錯誤日誌,並看看你的最後一個錯誤,就像你收到一個空白屏幕一樣,你的錯誤不會顯示出來,但可以通過使用ini_set()在代碼頂部覆蓋。並覆蓋ini設置 –

回答

0

使用此:

$form_errors = str_replace('new_password', 'New Password', $form_errors); 

錯誤消息只包含輸入字段,不是輸入的值的名稱。並且您需要將結果返回給數組變量,因爲str_replace不會在適當位置修改數組,它會返回一個包含所有替換項的新數組。

+0

謝謝你,巴爾瑪。很棒。我非常感謝你的時間。我也很感謝你的解釋。 – Collin512

0

我想你應該做這樣的事情:

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) { 
     // check name of current field here: 
     if ($name_of_field != 'new_password') { 
     $form_errors[] = $name_of_field . " is too short, must be {$minimum_length_required} characters long"; 
     } else { 
     $form_errors[] = "New password is too short, must be {$minimum_length_required} characters long"; 
     } 
    } 
    } 
    return $form_errors; 
} 
+0

非常感謝!奇蹟般有效。 – Collin512