检查电子邮件是否存在于MySQL数据库中

检查电子邮件是否存在于MySQL数据库中

本文介绍了检查电子邮件是否存在于MySQL数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下PHP代码将数据存储在我的MySQL数据库中,一切正常,但是我想进行检查.如果电子邮件已经存在于数据库中,则重定向到另一个页面. (就PHP而言,我是个菜鸟)

I am using the following PHP code to store data in my MySQL database, everything works perfect but I would like to build in a check; if the email already exists in the database then redirect to another page. (I'm a rookie when it comes to PHP)

<?php

  if(empty($_POST['name']))
  {} else {

  define('DB_NAME', 'dbname');
  define('DB_USER', 'dbuser');
  define('DB_PASSWORD', 'dbpass');
  define('DB_HOST', 'host');

  $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

  if (!$link) { die('could not connect: ' . mysql_error()); }

    $db_selected = mysql_select_db(DB_NAME, $link);

  if (!$db_selected) { die('can not use ' . DB_NAME . ': ' . mysql_error()); }

  $sql="INSERT INTO game (name, lastname, company, email, time) VALUES
('".$_POST['name']."','".$_POST['lastname']."','".$_POST['company']."','".$_POST['email']."','".$_POST['time']."')";

  if (!mysql_query($sql)) { die('Error: ' . mysql_error()); }

  mysql_close();

  }

?>

推荐答案

if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
    exit('Invalid email address'); // Use your own error handling ;)
}
$select = mysqli_query($connectionID, "SELECT `email` FROM `game` WHERE `email` = '".$_POST['email']."'") or exit(mysqli_error($connectionID));
if(mysqli_num_rows($select)) {
    exit('This email is already being used');
}

上方插入查询中插入

这篇关于检查电子邮件是否存在于MySQL数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 22:22