I am borrowing a code from w3schools.com to validate a name. Unfortunately, the regex only allows letters and whitespace. How can I amend this code so that a name like O'Shaughnessy does not produce an error message? Also, another part of my general codes is causing a user input to appear on MySQL with a backslash which was not intended. I had used mysqli_real_escape_string to prevent SQL injection attacks, and I believe this is the reason why the backslash is appearing in the data. How do I get rid of it?
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$name = "";
$nameErr = "Only letters and white space allowed";
}
}
}