I am creating a registration form, with fields for Name, Email and Phone Number. To check the validity of user input, I have a function validate_input()
that returns an array $arr
containing input entered by the user (if user input is valid). $arr
is then passed to a separate function that inserts the values in arr
into a MySQL table user
containing fields for name
, email
and phone
.
I initially tried the following:
$insert_query = "INSERT INTO user (name, email, phone)
VALUES ('$arr['name']',
'$arr['email']',
'$arr['phone']')";
$run_insert_query = mysqli_query($con, $insert_query);
But this didn't work. Some reading revealed that this is the wrong way to insert array values into the database. So I then tried the following (based on the accepted answer here):
$escaped_values = array_map('mysql_real_escape_string', array_values($arr));
$arr_values = implode(",", $escaped_values);
$insert_query = "INSERT INTO user (name, email, phone) VALUES ($arr_values)";
$run_query = mysqli_query($con, $insert_query);
But this didn't work either. Any ideas on how to make this work?