如何获得验证用户是否已经注册

时间:2019-01-01 10:56:38

标签: php mysql

伙计们,我是PHP新手,如今正在使用android应用程序,并且在此应用程序中,我已将我的应用程序与PHP MySQL DB连接,并且一切正常,但问题是我现在想检查用户cnic no是否可用在数据库中,所以查询给我返回,如果没有,那么它将被插入数据库中,请帮助我,我将非常感谢你们大家     谢谢

<?php
// Create connection
$conn = new mysqli($HostName, $HostUser, $HostPass, $DatabaseName);
 if($_SERVER['REQUEST_METHOD'] == 'POST')
 {
 $DefaultId = 0;

 $ImageData = $_POST['image_path'];

 $ImageName = $_POST['image_name'];

 $Fullname = $_POST['fullname'];

 $CNIC = $_POST['cnic'];

  $Mobile = $_POST['mobile'];

  $Address = $_POST['address'];

   $District = $_POST['district'];

   $Gender = $_POST['gender'];

   $Education = $_POST['education'];

   $Ward = $_POST['ward'];

   $Ps = $_POST['ps'];

   $Uc = $_POST['uc'];

   $Bjf = $_POST['bjf'];

   $By = $_POST['by'];

   $Dt = $_POST['dt'];

   $Email = $_POST['email'];

 $GetOldIdSQL ="SELECT id FROM information ORDER BY id ASC";

 $Query = mysqli_query($conn,$GetOldIdSQL);

 while($row = mysqli_fetch_array($Query)){

 $DefaultId = $row['id'];
 }

 $ImagePath = "images/$DefaultId.png";

 $ServerURL = "laserlightskincare.com/$ImagePath";

 //$InsertSQL = "insert into information (image_path,image_name,Fullname,CNIC,mobile,Address,District,ps,uc,ward,Gender,education,Doj,bjf,boy) values ('$ServerURL','$ImageName','$Fullname','$Cnic','$Mobile','$Address','$District','$PS','$UC','Ward','$Gender','Education','$Doj','$bjf','$boj','$boy')";
 $InsertSQL = "insert into information (image_path,image_name,Fullname,CNIC,mobile,Address,District,Gender,education,ward,ps,uc,bjf,boy,Doj,email) values ('$ServerURL','$ImageName','$Fullname','$CNIC','$Mobile','$Address','$District','$Gender','$Education','$Ward','$Ps','$Uc','$Bjf','$By','$Dt','$Email')";

 if(mysqli_query($conn, $InsertSQL)){

 file_put_contents($ImagePath,base64_decode($ImageData));

 echo "You have successfully registered";
 } 
 mysqli_close($conn);
 }else{
 echo "Not Uploaded";
 }

?>

2 个答案:

答案 0 :(得分:2)

首先使用select语句和for()循环将您的 cnic 值存储在数组中。

然后使用if()条件比较数据库中的 cnic 值并插入 cnic 值。如果找到该值,则打印一次使用 cnic ,否则使用insert查询插入该数据。

希望这对您有所帮助。

注意: 您应该使用比 cnic 值更合适的值来检查数据库中是否存在数据。我建议您比较用户的电子邮件地址或手机号码,因为对于每个注册输入其电子邮件地址和手机号码且不能再次使用的用户来说,它们是唯一的>用于注册过程。

答案 1 :(得分:0)

在插入查询之前;进行选择查询,并检查结果是否为空, 另一个解决方案是为cnic信息表创建唯一索引,这样就不能插入新记录