mysql 检验email @,检查电子邮件是否存在于MySQL数据库中

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)

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');

}

Stick that in above the insert query

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值