-1

I am trying to detect if the email address already exists in the database, and then if it doesn't add it. If it does, echo a message.

Can seem to get it to work though.

Do I have something wrong?

$db_connect = mysql_connect(H_DB_HOST, H_DB_USER, H_DB_PASSWORD);
if (!$db_connect) {}
$db_select = mysql_select_db(H_DB_NAME, $db_connect);
if (!$db_select) {}

$db_check_user_email = mysql_query("SELECT UserTABLE FROM UserEmail WHERE UserEmail = '$import_user_email'");

        if (!$db_check_user_email) {
          $sql = "INSERT INTO UserTABLE ( UserEmail ) VALUES ('$h_user_email') ";   
          if (!mysql_query($sql)) {} 
          mysql_close();
        }

if (mysql_num_rows($db_check_user_email) > 0) {
    echo "User id exists already.";
}
Nikk
  • 7,384
  • 8
  • 44
  • 90
  • Put a unique constraint on the table, attempt the insert, catch the violation of key constraint error...doing it like you're doing is error prone – developerwjk May 18 '15 at 19:38
  • Heh, Im a newb. No clue what all that means... – Nikk May 18 '15 at 19:39
  • 2
    [Your script is at risk for SQL Injection.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Please, [stop using `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). They are no longer maintained and are [officially deprecated](https://wiki.php.net/rfc/mysql_deprecation). Learn about [prepared statements](http://en.wikipedia.org/wiki/Prepared_statement) instead, and consider using PDO, [it's not as hard as you think](http://jayblanchard.net/demystifying_php_pdo.html). – Jay Blanchard May 18 '15 at 19:39

1 Answers1

0
SELECT UserTABLE FROM UserEmail

I think you need to flip-flop these

SELECT UserEmail FROM UserTABLE 
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
scubasteve623
  • 637
  • 4
  • 7
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/78127/discussion-on-answer-by-scubasteve623-mysql-check-if-email-exists-with-if-state). – Taryn May 18 '15 at 21:11