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 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大