My question is too long winded to find an answer. Please excuse me. Maybe I just don't know how to abbreviate my question. Well here is the problem and and the question. See the code below:
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$server = mysqli_connect('localhost', 'root', '', 'newsletter') or die("The Server Cannot Be Reached at this Time!");
//$dbc = mysqli_select_db($server, 'newsletter') or die("There is a problem accessing the databse!");
$query = mysqli_query($server, "INSERT INTO subscribers (name, email) VALUES('$name','$email')");
$name = strip_tags($name);
$email = strip_tags($email);
if(empty($name) || empty($email)) {
echo "All Fields Must Be Filled In!";
} else {
echo "You have been successfully subscribed";
}
}
mysqli_close($server);
?>
This code works. It gets the information to the database when the user fills out the form. What I noticed is that when the user only fills in one field, i.e. name and leaves the email field empty the if statement returns the line "All Fields Must Be Filled In". Then you return to the page and fill in the email field and everything is fine. But when I check the database I notice that there is an insertion of an id and name for the attempt where the email was not filled out on the form. Is this normal? Or what am I doing wrong. Thanks
change this
if(empty($name) || empty($email))
into this
if(empty($name) && empty($email))
|| pipe sign mean OR in your condition you are telling that either name is empty or email is empty but in && and sign we are telling that either name is empty and email is empty return error.
$name = strip_tags($_POST['name']);
following line should be written in else close
$query = mysqli_query($server, "INSERT INTO subscribers (name, email) VALUES('$name','$email')");
and use
if(empty($name) && empty($email))
not
if(empty($name) || empty($email))
You are checking for empty
after doing the insertion. Move the insertion code into else part
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$name = strip_tags($name);
$email = strip_tags($email);
if(empty($name) || empty($email)) {
echo "All Fields Must Be Filled In!";
} else {
$server = mysqli_connect('localhost', 'root', '', 'newsletter') or die("The Server Cannot Be Reached at this Time!");
//$dbc = mysqli_select_db($server, 'newsletter') or die("There is a problem accessing the databse!");
$query = mysqli_query($server, "INSERT INTO subscribers (name, email) VALUES('$name','$email')");
mysqli_close($server);
echo "You have been successfully subscribed";
}
}
?>
You want to move the insert of the user into the success branch of the if statement:
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$name = strip_tags($name);
$email = strip_tags($email);
if(empty($name) || empty($email)) {
echo "All Fields Must Be Filled In!";
} else {
$server = mysqli_connect('localhost', 'root', '', 'newsletter') or die("The Server Cannot Be Reached at this Time!");
//$dbc = mysqli_select_db($server, 'newsletter') or die("There is a problem accessing the databse!");
$query = mysqli_query($server, "INSERT INTO subscribers (name, email) VALUES('$name','$email')");
mysqli_close($server);
echo "You have been successfully subscribed";
}
}
?>
You should run the insert query in the else condition since here you are checking if a field is blank or not.
<?php
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$name = strip_tags($name);
$email = strip_tags($email);
if(empty($name) || empty($email)) {
echo "All Fields Must Be Filled In!";
} else {
$server = mysqli_connect('localhost', 'root', '', 'newsletter') or die("The Server Cannot Be Reached at this Time!");
$query = mysqli_query($server, "INSERT INTO subscribers (name, email) VALUES('$name','$email')");
mysqli_close($server);
echo "You have been successfully subscribed";
}
}
?>