Very new to PHP so please bear with me. I am writing a very 'simple' web page for internal use only really, XAMPP server on my laptop etc., and I decided to make a simple login page which checks an email for format and if it's there.
So I got there with this code and it works:
**```
$e = "";
$emailErr = "";
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
/// blah blah blah her is connections and stuff but boring.
if (empty($_POST['email'])) {
$emailErr = "Email is required";
} else
$e = test_input($_POST['email']);
if (!filter_var($e, FILTER_VALIDATE_EMAIL)){
$emailErr = "Invalid email format";
}
```**
However I now want to get a bit smart and take the output into a variable and use that variable to populate the database field.
I thought this would work.
//Check for an email address:
if (empty($_POST['email'])) {
$emailErr = "Email is required";
} elseif
$e = test_input($_POST['email']);
if (!filter_var($e, FILTER_VALIDATE_EMAIL))
{$emailErr = "Invalid email format";
} else {
$e = mysqli_real_escape_string($conn, trim($_POST['email']));
}
```
Later I do this with the database...
$q = "INSERT INTO users_php ( email) VALUES ('$e')";
$r = @mysqli_query($conn, $q); // Run the query.
if ($r) { // If it ran OK.
However I can't seem to get the $e variable to hold the email address even when it's valid.
I get this error every time and I have tried so many attempts around the code;
Parse error: syntax error, unexpected variable "$e", expecting "(" in C:\xampp\htdocs\registrationformv2.php on line 29
I really want the actual email address to end up in $e so I can use it again later.
Many thanks in anticipation of any help
Cheers
Tried adding elseif, tried adding switch, tried changing the variable names the form everything always comes back to 'expecting error'
source https://stackoverflow.com/questions/76745132/multiple-checks-of-a-post-value-check-email-for-format-blank-and-keep-the-resu
No comments:
Post a Comment