PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
I have this code I'm working with. I want it to validate each of the text fields, and if they all pas, then submit to the database. Otherwise, I want error messages to be revealed and not allow insertion into the database. For some reason the error messages are automatically showing up once I navigate to the page, and it is not validating correctly - allowing insertion of blank fields and such.
Can someone take a look and tell me what I'm doing wrong here?
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$emailaddy = $_POST['email'];
$doubleaddycheck = "SELECT * FROM emaillist WHERE email = $emailaddy";
$doubleaddycheck = mysql_query($doubleaddycheck);
$insertcontactdata = "INSERT INTO emaillist (email, firstname, lastname) VALUES ('$emailaddy2', '$firstname2', '$lastname2')";
$errors = array();
if (!empty($_POST['email'])) {
if (!preg_match('/^[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\]\x7f-\xff]+)+$/i', $emailaddy)) {
$errors[] = "Please enter a valid e-mail address.";
} elseif (mysql_num_rows($doubleaddycheck) > 0) {
$errors[] = "That e-mail address has already been added!";
} else {
$emailaddy2 = $_POST['email'];
}
} else {
$errors[] = "Please enter an e-mail address";
}
if (!empty($_POST['firstname'])) {
if (!preg_match("/^[a-z\\\'\-\s]+$/i", $firstname)) {
$errors[] = "Please enter a valid first name.";
} elseif (strlen($firstname) < 1) {
$errors[] = "Please enter a valid first name.";
} else {
$firstname2 = $_POST['firstname'];
}
} else {
$errors[] = "Please enter a first name.";
}
if (!empty($_POST['lastname'])){
if (!preg_match("/^[a-z\\\'\-\s]+$/i", $lastname)) {
$errors[] = "Please enter a valid last name.";
} elseif (strlen($lastname)< 1) {
$errors[] = "Please enter a valid last name.";
} else {
$lastname2 = $_POST['lastname'];
}
} else {
$errors[] = "Please Enter a last name.";
}
if (empty($errors)) { //if the array $errors is empty(everything passed) continue with the script and add the person.
mysql_query($insertcontactdata);
print "Welcome to the list<b> $firstname! </b><br />\n";
print "Your e-mail address: <b> $emailaddy </b>will be included in future e-mails.";
} else { //the array has an error in it, print the error.
foreach ($errors as $msg) {
echo " - $msg\n";
}
}
?>
Thanks. I don't think the quotes are necessary, since it has done successful inserts in the past without them. Also, I thought order didn't matter when defininig variables, since I don't actually call that variable until much later?
I suppose I can remove the regex for the names though...
KaneNY250 wrote:Thanks. I don't think the quotes are necessary, since it has done successful inserts in the past without them. Also, I thought order didn't matter when defininig variables, since I don't actually call that variable until much later?
I suppose I can remove the regex for the names though...
The order definitely matters. Try echoing $insertcontactdata and see what it says.
I print out the insert statement at the beginning and it is empty (as expected) but if I print out out at the end, it only contains the email address, but not the other two fields. So since I'm not calling the insert until later on, I don't think that's an issue. I have added the quotes around the '$emailaddy' now though. Still having issues getting it to loop correctly though...
I worked on my code a bit, and now everything validates correctly. However, the one last issue I'm having is that all the error messages show up immediately when I navigate to the page, and I only want them to show up if the submit button has been pressed. I tried nesting everything into another if ($_POST){ but that didn't change anything.