php - 无法允许人们更改表中的唯一条目

标签 php mysql

所以,我的表中有一堆代码,我不想表中出现多个相同的代码,所以我将其设置为“唯一”。 但是,与此同时,我希望他们能够每小时修改一次代码。

function some_more_custom_content() {

    $output="<BR>";

    ob_start();

    if ($_REQUEST['code'] != "") {
        $code = $_REQUEST['code'];
        $query="INSERT INTO `fc` (`code`,`datetime`) values ('" . mysql_real_escape_string($code) . "', now())";
        $result=mysql_query($query) or die(mysql_error());
        $seconds = time() - strtotime($fetch_array["datetime"]);
        if($sql){
            echo("Intserted " . htmlentities($code) ." into the top.");
        }else{
            if ($seconds < 60*60) {
                echo ("The code " . htmlentities($code) ." was updated less than an hour ago.");
         } else {
                $query="DELETE FROM `fc` (`code`,`datetime`) values ('" . mysql_real_escape_string($code) . "', now())";
                echo ("Inserted " . htmlentities($code) ." into the top.");
        }

    }}

现在,我尝试获取它,以便当代码工作时它会正常提交代码,我认为这是有效的。

现在,如果它获取的代码已经存在,我会收到重复错误“ key 1 的重复条目“Bob””

但是,我只想删除它找到的旧查询,并在距离上次提交超过一小时后重新提交。

知道我做错了什么吗?

最佳答案

您只需更新数据库中的日期/时间字段即可反射(reflect)“高峰”的时间。

if ($_REQUEST['code'] != "")
{
    $code = $_REQUEST['code'];

    $sql = "SELECT * FROM fc WHERE code = '" . mysql_real_escape_string($code) . "'";

    $result = mysql_query($sql);
    if (mysql_num_rows($result))
    {
        $row = mysql_fetch_array($result);
        $seconds = time() - strtotime($row["datetime"]);

        if ($seconds > 60*60)
        {
            $sql = "UPDATE fc SET datetime = NOW() WHERE code = '" . mysql_real_escape_string($code) . "'";

            mysql_query($sql);
            echo("Intserted " . htmlentities($code) ." into the top.");
        }
        else
        {
            echo ("The code " . htmlentities($code) ." was updated less than an hour ago.");
        }
    }
    else
    {
        $sql = "INSERT INTO fc (code, datetime) VALUES ('" . mysql_real_escape_string($code) . "', NOW())";

        mysql_query($sql);
        echo("Intserted " . htmlentities($code) ." into the top.");
    }
}

(我可能不应该为您重新编写代码,并且还可以进行一些改进,但这反射(reflect)了我可以通过最少的更改做出的最佳更改(这有什么意义吗? ?))

关于php - 无法允许人们更改表中的唯一条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1241649/

相关文章:

c# - 使用 LINQ 进行两个查询的完全外部联接的更好方法? C#

mysql - 保护行在 MySQL 中不被删除

c# - Mysql 和 Visual Studio 2019 for macOS 之间的连接不起作用

php - Javascript动态表单提交

php - 如何将 PHP 照片上传/图像转换为 PHP 视频上传/视频?

php - Ajax 发布带有数字但不带有字母的字符串

javascript - AJAX 使用复选框删除多条记录

php - Codeigniter JOIN - 不是唯一的表/别名问题

php - 在下拉菜单中列出递归文件夹,在 php 中

php - 在同一 MySQL 事务中删除后数据仍然可见