php - 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");
}
}
解决方案
您的查询是错误的:-
$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')";
推荐阅读
- python - 如何在 GridSearchCV 中使用 TransformedTargetRegressor 应用两个转换?
- python - 并发旋转文件处理程序未在达到 maxByteSize 时创建新文件
- node.js - Node.js - 从 MongoDb 集合中获取唯一值并删除空格
- python - result = result.decode('utf-8') UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 321: invalid start byte
- javascript - 如何下载相册,其中包含使用 Streamsaver.js 的图像集
- django - 如何使用 inline_formsety 在模型/表单上的 clean() 函数中排除已删除的对象
- flutter - Flutter:writeAsString 不会创建任何文件
- python - CUPS-Add-Modify-Printer 无法在任何地方创建 IPP 打印机
- typescript - 返回值的断言函数?
- node.js - docker容器构建失败