简体   繁体   English

防止重复插入数据 PHP MySQL

[英]PREVENT DUPLICATION for INSERTING DATA in PHP MySQL

Good Days I am new at programming and I have a problem I want to prevent duplication.美好的一天 我是编程新手,我遇到了一个问题,我想防止重复。 What do I need to Add in my code just to prevent duplication when inserting a data ( just only in the "NAME" on my table).我需要在我的代码中添加什么以防止在插入数据时重复(仅在我的表中的“NAME”中)。


    if(isset($_POST['text'])){

        $text =$_POST['text'];

        $sql = "INSERT INTO table_attendance(NAME,TIMEIN) VALUES('$text',NOW())";
        if ($conn->query($sql) ===TRUE) {
            $_SESSION['success'] = 'Action Done';
        }else{
            $_SESSION['error'] = $conn->error;
        }

        header("Location: index.php");
    }
    $conn->close();

What do I need to add in my code so it wont accept same NAME and prevent duplication我需要在我的代码中添加什么,这样它就不会接受相同的名称并防止重复

  1. Add a UNIQUE KEY in MySQL on the name column.name列的 MySQL 中添加一个 UNIQUE KEY。

  2. Change your INSERT INTO table_attendance... to INSERT IGNORE INTO table_attendance... .将您的INSERT INTO table_attendance...更改为INSERT IGNORE INTO table_attendance...

Please note that your code is currently susceptible to SQL injections.请注意,您的代码目前容易受到 SQL 注入的影响。

This is very dangerous.这是非常危险的。 Be sure to not deploy such code when launching an actual product.确保在启动实际产品时不要部署此类代码。

See more info here: How does the SQL injection from the "Bobby Tables" XKCD comic work?在此处查看更多信息: “Bobby Tables”XKCD 漫画中的 SQL 注入是如何工作的? . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM