dssj88098 2018-03-24 17:29
浏览 58
已采纳

如何防止在php / mysql中具有唯一id的特定用户的重复输入

I am working on result management system for secondary school.

I don't want to enter the same subject for a particular user using his/her registration number as unique id. i.e. if I enter maths for John, i must not enter maths again for John.

Below is my code:

<?php
    $msg = "";

    $connection = mysqli_connect("localhost", "root", "", "result_mg_db");
    if (isset($_POST['submit'])) {
        $regno = addslashes(strip_tags($_POST['regno'])); 
        $subj = addslashes(strip_tags($_POST['subject'])); 
        $examscore = addslashes(strip_tags($_POST['examscore'])); 
        $cascore = addslashes(strip_tags($_POST['cascore']));
        $date = date("Y-m-d h:i:sa"); 

        $confirm_subj = mysqli_query($connection, "SELECT subject FROM jss1_resulttbl WHERE regno = '$regno'");

        if ($confirm_subj == $_POST['subject']) {
            $msg = "This subject has been registered";
        } else {
            if ($cascore == ""|| $examscore == ""|| $subj == "" || $regno == "" ) {
                $msg = "Please fill all fields";
            } else {
                $reg_subj = mysqli_query($connection, "INSERT INTO jss1_resulttbl VALUES ('', '$regno', '$subj', '$examscore', '$cascore', '$date')");
                if (!$reg_subj) {
                    echo "Subject Registration failed ";
                }else {
                    $msg = "You have Successfully entered score for " . $subj . "<br/>";
                } 
            }

        }
    } 
?>
  • 写回答

1条回答 默认 最新

  • duanpo1498 2018-03-24 17:53
    关注

    Run a query first to check if database has already has a row with the registration number and subject. Then insert data to database.

    $result = mysqli_query("SELECT * FROM jss1_resulttbl WHERE regno='$regno' AND subject='$subject'");
    $num_rows = mysql_num_rows($result);
    
    if (!$num_rows) {
       // Here write query to insert data to database 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)