首页 > 解决方案 > How to validate if data already exist in php

问题描述

Here is code whats wrong with it not working Note:: No error showing or redirect to 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");
    }
 }

标签: phpmysqlvalidationinsert

解决方案


您的查询是错误的:-

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

推荐阅读