【问题标题】:How to validate if data already exist in php [duplicate]如何验证php中是否已经存在数据[重复]
【发布时间】:2020-06-21 02:08:39
【问题描述】:

这是代码有什么问题,它不能正常工作 注意::没有错误显示或重定向到 print.php

$conn=mysqli_connect("localhost","root","","degree");
$sql="select * from students where regno=$regno";
$res= mysqli_query($conn,$sql);
if(mysqli_num_rows($res) >0)
 {
    echo "<script>alert('Student Already Exists')</script)" ;


 }
 else{

    $query="INSERT into students (id,name,regno,program,fname,stcnic,phone,mobile,email,address,gender,gown,relname1,
    relation1,relcnic1,relname2,relation2,relcnic2,gust1,guestrel1,guestcnic1,guest2,guestrel2,guestcnic2) 
     values('','$name',$regno,'$program','$fname','$stcnic',$phone,$mobile,'$email','$address','$gender','$gown','$relname1',
    '$relation1','$relcnic1','$relname2','$relation2','$relcnic2','$guest1','$guestrel1','$guestcnic1','$guest2','$guestrel2','$guestcnic2')";

    $result=mysqli_query($conn,$query);
    if($result)
    {
        $_SESSION['id']=$regno;
        header("location:print.php");
    }
 }

【问题讨论】:

  • $phone,$mobile 周围没有引号。也许这些不仅仅是数字?
  • 您的查询未准备好(SQL 注入已获批准。),值为“”的第二个 id 不正确,您必须在 mysql 表自动增量中设置 ID,并且不要在查询中设置。
  • 如果在此会话检查时插入另一个会话会怎样?
  • 它工作正常,但是(警告不使用 javascript)——简单的 echo“用户已经存在”;正在工作,我想显示警报消息。

标签: php mysql validation insert


【解决方案1】:

你的查询是错误的:-

$sql="select * from students where regno=$regno";

正确:-

$sql="select * from students where regno='$regno'";

第二次查询

$query="INSERT into students (id,name,regno,program,fname,stcnic,phone,mobile,email,address,gender,gown,relname1,relation1,relcnic1,relname2,relation2,relcnic2,gust1,guestrel1,guestcnic1,guest2,guestrel2,guestcnic2) values('','$name','$regno','$program','$fname','$stcnic','$phone','$mobile','$email','$address','$gender','$gown','$relname1','$relation1','$relcnic1','$relname2','$relation2','$relcnic2','$guest1','$guestrel1','$guestcnic1','$guest2','$guestrel2','$guestcnic2')";

【讨论】:

  • 同样什么也没发生
猜你喜欢
  • 2019-03-11
  • 2021-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多