So I have a form running on a page that I’m experimenting with. Atm I just have one input and that is email. Basically I want a register and if a person tries to add the same email again it will return an error msg and otherwise it will add it.
So this is the code I’ve been posting with:
<?php
$username="user";
$password="pass";
$database="test";
$email = mysql_real_escape_string( $_POST['email'] );
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM enroll WHERE email='$email' ";
$result = mysql_query($query) or die(mysql_error());
if (mysql_num_rows($result) ) {
print 'user is already in table';
}
else {
$query = "INSERT INTO enroll VALUES ('','$email')";
$result = mysql_query($query) or die(mysql_error());
print 'user added';
}
?>
That works fine! If I add an email that already exists it says “user is already in table” and if not it says “user added”.
Problem is that before I used to run another php script which validated the input and then upon submit it added it to the DB. Now it ALWAYS says “user is already in table” even before I’ve pressed Submit.
I would like to do something like this, but when posting it doesn’t work at all and I can’t understand what’s wrong.
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
// Required field names
$required = array('email');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo 'You forgot to fill in your email';
} else {
$username="user";
$password="pass";
$database="test";
$email = mysqli_real_escape_string( $_POST['email'] );
mysqli_connect(localhost,$username,$password);
mysqli_select_db($database) or die( "Unable to select database");
$query = "SELECT * FROM enroll WHERE email='$email' ";
$result = mysqli_query($query) or die(mysqli_error());
if (mysqli_num_rows($result) ) {
print 'user is already in table';
}
else {
$query = "INSERT INTO enroll VALUES ('','$email')";
$result = mysqli_query($query) or die(mysqli_error());
print 'user added';
}
}
}
?>
In my world, this checks if email is supplied, checks if it exists in db and if not adds it. If it exists it will come back with an error.
What am I doing wrong?
EDIT2: @ExpertSystem This is the code I’m going with now as per your recommendation:
<?php
if(isset($_POST['submit'])) {
// Required field names
$required = array('email');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo 'You forgot to fill in your email';
} else {
$username="root";
$password="root";
$host="localhost";
$database="test";
$email = mysqli_real_escape_string( $_POST['email'] );
$link = mysqli_connect(localhost, $username, $password);
mysqli_select_db($link, $database) or die("Unable to select database");
$query = "SELECT * FROM enroll WHERE email='$email'";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
if (mysqli_num_rows($result) ) {
print 'user is already in table';
}
else {
$query = "INSERT INTO enroll (email) VALUES ('$email')";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
print 'user added';
}
}
}
?>
Since you are using the procedural form of the
mysqli_
functions, you need to pass a mysqli link identifier as the first argument inmysqli_select_db()
,mysqli_real_escape_string()
,mysqli_query()
andmysqli_error()
(see also the documentation for more details). The aforementioned mysqli link identifier is the object returned bymysqli_connect()
.The modified code should look something like this:
My head says, “mysqli_num_rows($result);” returns an integer.
So you can’t do if(mysqli_num_rows($result)), it will always return an integer (e.g. 0 or 1)
So try this:
Just a thought.
thanks for all your help! Hopefully I’ve at least improved my bad code with your help.
I found out what’s causing the problem and it was this.
I changed that to
And of course now everything works. Thanks tho! 🙂