[英]Check email exist with php mysqli
I am new to mysqli i wanted to check if email already exists in database with php and mysqli 我是mysqli的新手,我想使用php和mysqli检查数据库中是否已存在电子邮件
Here is what i have done so far: 到目前为止,这是我所做的:
ini.php ini.php
<?php
session_start();
require 'connect.php';
include 'user.func.php';
?>
connect.php connect.php
<?php
$con = new mysqli('host', 'user', 'password', 'db');
if($con->connect_errno > 0){
die('Sorry, We\'re experiencing some connection problems.');
}
?> ?>
register.php register.php
<?php
include 'ini.php';
?>
<form action="" method="post">
<p>Username:<br/><input type="text" name="reg_name" maxlength="50" ></p>
<p>Email:<br/><input type="text" name="reg_email" size="35" maxlength="50" ></p>
<p>Password:<br/><input type="password" name="reg_password" maxlength="50" ></p>
<p>Re-type Password:<br/><input type="password" name="reg_re_password" maxlength="20"></p>
<p><input type="submit" value="Register" ></p>
</form>
<?php
include 'validate.php';
?>
validate.php validate.php
<?php
if(isset ( $_POST['reg_name'],$_POST['reg_email'], $_POST['reg_password'] )) {
$errors = array();
$name = $_POST['reg_name'];
$email = $_POST['reg_email'];
$password = $_POST['reg_password'];
$re_password = $_POST['reg_re_password'];
if(empty($name) || empty($email) || empty($password) || empty($re_password)){
$errors[] = 'All fields are required';
} else {
if(strlen($name) > 50 || strlen($email) > 50 || strlen($password) > 50) {
$errors[] = 'One or more field has too long Characters';
}
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE && $password !== $re_password){
$errors[] = 'Enter valid email address and password do not match';
}else{
if(filter_var($email, FILTER_VALIDATE_EMAIL) === FALSE) {
$errors[] = 'Please enter a valid email address';
}
if($password !== $re_password){
$errors[] = 'Both passwords do not match';
}
if(user_exists($email) > 0){
$errors[] = 'That email is already been registered';
}
}
}
if (!empty($errors)) {
foreach ($errors as $errors) {
echo '<strong>',$errors,'</srtong><br />';
}
}else{
echo 'Registered';
}
}
?> user.func.php ?> user.func.php
<?php
function user_exists($email) {
$con = new mysqli('host', 'user', 'password', 'db');
$query = "SELECT * FROM users WHERE email = ?";
$stmt = $con->prepare($query);
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
return $stmt->num_rows;
}
return false;
}
?>
This code shows all the other errors but doesn't check if the email exist. 此代码显示所有其他错误,但不检查电子邮件是否存在。 Instead it shows registered even when i enter email that is already in database.
相反,即使我输入数据库中已经存在的电子邮件,它也会显示已注册。
I have database named 'test'. 我有名为“测试”的数据库。 And table 'users' with columns user_id, username, email and password.
并在表“用户”中添加列user_id,用户名,电子邮件和密码。
Can anyone tell how to code mysqli query for this? 谁能告诉我该如何为mysqli查询编写代码?
I used this code in mysql 我在mysql中使用了这段代码
function user_exists($email) {
$email = mysql_real_escape_string($email);
$query = mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'email'='$email'");
return (mysql_result($query, 0) == 1) ? true :false;
}
Use this condition, because you are returning the number of rows from your query. 使用此条件,因为您要从查询中返回行数。
if(user_exits($register_email) > 0){
Edit 编辑
function user_exists($email) {
$mysqli = new mysqli("host", "my_user", "my_password", "your_db");
$query = "SELECT COUNT(*) AS num_rows FROM users WHERE email = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("s", $email);
if ($stmt->execute()) {
return $stmt->num_rows;
}
return false;
}
I could tell how to do it with PDO 我可以告诉我如何使用PDO
<?php
function user_exists($email) {
$query = "SELECT 1 FROM " . USER_TABLE . " WHERE email = ?";
$stmt = $this->_db->prepare($query);
$stmt->execute(array($email));
return (bool)$stmt->fetchColumn();
}
Though I see no point in strict checking 虽然我认为严格检查毫无意义
if(user_exits($register_email))
would be more than enough 会绰绰有余
Also, I don't understand where did you get that $register_email
variable. 另外,我不知道您从哪里获得了
$register_email
变量。 Do you have error reporting on? 您是否有错误报告?
By the way, you need an established PDO connection for this. 顺便说一下,您需要为此建立一个PDO连接。 This code won't work with mysqli
该代码不适用于mysqli
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.