php - Check if email exists in sql database and output thereafter (regarding running if inside if) -
so have form running on page i'm experimenting with. atm have 1 input , email. want register , if person tries add same email again return error msg , otherwise add it.
so 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 select database"); $query = "select * enroll email='$email' "; $result = mysql_query($query) or die(mysql_error()); if (mysql_num_rows($result) ) { print 'user in table'; } else { $query = "insert enroll values ('','$email')"; $result = mysql_query($query) or die(mysql_error()); print 'user added'; } ?>
that works fine! if add email exists says "user in table" , if not says "user added".
problem before used run php script validated input , upon submit added db. says "user in table" before i've pressed submit.
i this, when posting doesn't work @ , can't understand what's wrong.
<?php //if form submitted if(isset($_post['submit'])) { // required field names $required = array('email'); // loop on field names, make sure each 1 exists , not empty $error = false; foreach($required $field) { if (empty($_post[$field])) { $error = true; } } if ($error) { echo 'you forgot fill in 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 select database"); $query = "select * enroll email='$email' "; $result = mysqli_query($query) or die(mysqli_error()); if (mysqli_num_rows($result) ) { print 'user in table'; } else { $query = "insert enroll values ('','$email')"; $result = mysqli_query($query) or die(mysqli_error()); print 'user added'; } } }
?>
in world, checks if email supplied, checks if exists in db , if not adds it. if exists come error.
what doing wrong?
edit2: @expertsystem code i'm going per recommendation:
<?php if(isset($_post['submit'])) { // required field names $required = array('email'); // loop on field names, make sure each 1 exists , not empty $error = false; foreach($required $field) { if (empty($_post[$field])) { $error = true; } } if ($error) { echo 'you forgot fill in 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 select database"); $query = "select * enroll email='$email'"; $result = mysqli_query($link, $query) or die(mysqli_error($link)); if (mysqli_num_rows($result) ) { print 'user in table'; } else { $query = "insert enroll (email) values ('$email')"; $result = mysqli_query($link, $query) or die(mysqli_error($link)); print 'user added'; } } } ?>
since using procedural form of mysqli_
functions, need pass mysqli link identifier first argument in mysqli_select_db()
, mysqli_real_escape_string()
, mysqli_query()
, mysqli_error()
(see documentation more details). aforementioned mysqli link identifier object returned mysqli_connect()
.
modified code should this:
... $link = mysqli_connect(localhost, $username, $password); mysqli_select_db($link, $database) or die("unable select database"); $email = mysqli_real_escape_string($link, $_post["email"]); ... $result = mysqli_query($link, $query) or die(mysqli_error($link)); ... if... ... } else { ... $result = mysqli_query($link, $query) or die(mysqli_error($link)); ... ...
Comments
Post a Comment