I am having trouble inserting the values “firstname”, “lastname”, “email” and “username” into my database. The only value that correctly gets inserted into the database is the hashed password.
I think the problem may have something to do with the for loop, but to me it seems like it should work.
[php]<?php
if (isset($_POST[‘submit’])){
require_once ‘config.php’;
$hashed_password = password_hash($_POST[“password”], PASSWORD_DEFAULT);
$fields = [‘firstname’, ‘lastname’, ‘email’, ‘username’];
$escaped_values = [];
foreach($fields as $field){
$escaped_values[$field] = mysqli_real_escape_string($connect, $_POST[’$field’]);
}
$sql = “INSERT INTO users (firstname, lastname, email, username, password) VALUES (’{$escaped_values[“firstname”]}’, ‘{$escaped_values[“lastname”]}’, ‘{$escaped_values[“email”]}’, ‘{$escaped_values[“username”]}’, ‘$hashed_password’)”;
mysqli_query($connect, $sql);
// send email
$emailRecipient = $_POST[“email”];
$subject = ‘Welcome!’;
$message_body = 'You have successfully created an account ’ . $_POST[“username”] . ‘! Welcome.’;
mail($emailRecipient, $subject, $message_body);
}
?>[/php]