dqx76962 2019-07-17 03:35
浏览 39
已采纳

防止重复的行数据条目

I had created a database which named student with ID, name, mat_number, specialty, age, and gender, in a PHP application.

I do not want the name or mat_number be taken in more than once.

I have done the connection to my database in a different page and called it in the add student page.

This following codes is for a faculty database collection


    <?php 
                if(isset($_POST['submit']))  {
                    $name = $_POST['name'];
                    $matNo = $_POST['mat_number'];
                    $age = $_POST['age'];
                    $specialty = $_POST['specialty'];
                    $gender = $_POST['gender'];

                    if(!empty($name) && !empty($matNo) && !empty($age) && 
                            !empty($specialty) && !empty($gender))
                {

                    $sql = "INSERT INTO `student`(`name`, `UB_number`, `age`, 
                                          `sex`, `specialty`)
                    VALUES ('$name', '$matNo', '$age', '$gender', '$specialty')";
                    $conn->query($sql);
                    header("Location: index.php");
                }
                else{
                    echo "Error: Complete all records";
                }          
                }

            ?>

I want to get an error message demanding for a change if the 2 fields already exist in the database.

  • 写回答

3条回答 默认 最新

  • douli0531 2019-07-17 03:54
    关注

    hope this may be helpfull to you. In here I asume that you are not using any framework. But if you use a framework there are plenty of easy methods to do this.In here I have checked only name field. You should update code as you wants. Also it it better if you could validate your inputs before check. Like trim(). Thanks

    <?php 
                if(isset($_POST['submit']))  {
    
                    $name = $_POST['name'];
                    $matNo = $_POST['mat_number'];
                    $age = $_POST['age'];
                    $specialty = $_POST['specialty'];
                    $gender = $_POST['gender'];
    
                    //after user click the submit button 
                    $sql_Select_Stundets = "SELECT * FROM student WHERE name = '$name' ";
                     // query the sql with db connection         
                    $result_sql_Select_Stundets = mysqli_query($conn,$sql_Select_Stundets);
                    //Now check the row count to verify the output if there is any match
                    $rowcount=mysqli_num_rows($result);
    
                    //Now write insert inside if condition
                     if( $rowcount >0 ) {
    
                        if(!empty($name) && !empty($matNo) && !empty($age) && 
                        !empty($specialty) && !empty($gender)) {
    
                        $sql = "INSERT INTO `student`(`name`, `UB_number`, `age`, 
                                      `sex`, `specialty`)
                        VALUES ('$name', '$matNo', '$age', '$gender', '$specialty')";
                        $conn->query($sql);
                        bheader("Location: index.php");
                    }else{
    
                        echo "Error: Complete all records";
                         }    
    
                    }else{
                           echo "<script>
                           alert('sorry this name is already available');
                            </script>";
                        }
    
                     }              
            ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测