首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >来自mysql数据库的php中的重复值验证

来自mysql数据库的php中的重复值验证
EN

Stack Overflow用户
提问于 2017-02-03 16:18:38
回答 1查看 45关注 0票数 0

我想检查reg_no是否在SQL数据库中是重复的,并显示错误消息“请输入唯一的注册表编号”。

下面是我的代码:

代码语言:javascript
复制
<?php
error_reporting( ~E_NOTICE );
require_once 'dbconfig.php';
if(isset($_POST['btnsave']))
{
    $name       = $_POST['name'];
    $reg_no     = $_POST['reg_no'];
    $imgFile    = $_FILES['photo']['name'];
    $tmp_dir    = $_FILES['photo']['tmp_name'];
    $imgSize    = $_FILES['photo']['size'];

    if(empty($name)){
        $errMSG = "Please Enter Student Name.";
    }
    else if(empty($reg_no)){
        $errMSG = "Please Enter Registration Number.";
    }
    else if(empty($imgFile)){
        $errMSG = "Please Select Image File.";
    }
    else
    {
        $upload_dir = 'photos/';
        $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
        $valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
        $photo = rand(1000,1000000).".".$imgExt;
        if(in_array($imgExt, $valid_extensions)){           
            if($imgSize < 5000000)              {
                move_uploaded_file($tmp_dir,$upload_dir.$photo);
            }
            else{
                $errMSG = "Sorry, your file is too large.";
            }
        }
        else{
            $errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";        
        }
    }
    if(!isset($errMSG))
    {
        $stmt = $DB_con->prepare('INSERT INTO tbl_users(name,reg_no,photo) VALUES(:name, :reg_no, :photo)');
        $stmt->bindParam(':name',$name);
        $stmt->bindParam(':reg_no',$reg_no);
        $stmt->bindParam(':photo',$photo);
        if($stmt->execute())
        {
            $successMSG = "new record succesfully inserted ...";
            header("refresh:5;index.php");
        }
        else
        {
            $errMSG = "error while inserting....";
        }
    }
}
?>
EN

回答 1

Stack Overflow用户

发布于 2017-02-03 16:33:18

以下是更新后的更改

代码语言:javascript
复制
$stmt = $DB_con->prepare('select count(*) as cnt from tbl_users where reg_no) VALUES(:reg_no);
$stmt->bindParam(':reg_no',$reg_no);
$stmt->execute();
$stmt->bind_result($cnt);
if($cnt>0){
  echo $errMSG = "Please enter unique Reg. No";
}
else{
  $stmt = $DB_con->prepare('INSERT INTO tbl_users(name,reg_no,photo) VALUES(:name, :reg_no, :photo)');
  $stmt->bindParam(':name',$name);
  $stmt->bindParam(':reg_no',$reg_no);
  $stmt->bindParam(':photo',$photo);
  if($stmt->execute())
  {
    $successMSG = "new record succesfully inserted ...";
    header("refresh:5;index.php");
  }
  else
  {
    $errMSG = "error while inserting....";
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42019685

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档