这是我的php文件,我正在其中检查电子邮件是否已经存在。
<?php
include_once("connection.php");
$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$pass=$_REQUEST['pass'];
$mobno=$_REQUEST['mobno'];
$checkemail="SELECT * FROM dhruv_users WHERE email= '$_REQUEST[email]'";
$checkmob="SELECT * FROM dhruv_users WHERE mobno= '$_REQUEST[mobno]'";
$rsemail = mysqli_query($conn,$checkemail);
$rsmob = mysqli_query($conn,$checkno);
$dataemail = mysqli_num_rows($rsemail);
$datamob = mysqli_num_rows($rsmob);
if($dataemail >= 1) {
echo "exists";
}
else if($datamob >= 1)
{
echo "exists";
}
else{
$select=mysqli_query($conn,"select max(id) as id from dhruv_users");
if($data=mysqli_fetch_array($select))
{
$id=$data['id'];
$id++;
}
else
{
$id=1;
}
$query=mysqli_query($conn,"insert into dhruv_users VALUES ('$id','$name','$email','$mobno','$pass')");
if($query)
{
echo "success";
}
else{
echo "unsuces";
}
}
?>
没有错误,但是数据成功输入而没有检查mob no(是否存在)。
一次输入相同的暴民一次又一次显示成功消息,而不是现有消息。
答案 0 :(得分:3)
为什么不使用mysqli_num_rows代替MYSQLI_NUM的mysqli_fetch_array。
尝试以下
$rs = mysqli_query($conn,$check);
$dataa = mysqli_num_rows($rs);
if($dataa > 1) {
echo "User Already in Exists<br/>";
}
答案 1 :(得分:-1)
您需要计算从数据库获得的结果。
代码中存在逻辑错误。请看下面的代码:-
您的代码
if($dataa[0] > 1) {
echo "User Already in Exists<br/>";
}
在上方替换为:
if(count($dataa) > 1) {
echo "User Already in Exists<br/>";
}
答案 2 :(得分:-1)
您需要使用正确的引号编写查询。无法识别$ _REQUEST的电子邮件索引。另外,使用mysqli_num_rows函数。
有关最佳实践,请参考以下代码:
$check = "SELECT * FROM dhruv_user WHERE email= '" . $_REQUEST['email'] . "'";
$rs = mysqli_query($conn,$check);
if ($rs) {
$rowcount = mysqli_num_rows($rs);
if ($rowcount) {
echo "User already exists<br/>";
}
}