I have been creating a registration system, and my code will not insert the fields: firstname, lastname, email and username into my database, however it will insert the hashed password into my database. I think the problem might have something to do with the for loop, but to me it seems like it should work.
<?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);
}
?>
$_POST['$field']<-- probably not doing what you're expecting. As a side note, I can't for the life of me work out why this code isn't a syntax error...