gpt4 book ai didi

php - 如何在一些预检查后将值插入数据库

转载 作者:行者123 更新时间:2023-11-29 21:51:57 24 4
gpt4 key购买 nike

我想将一些值插入数据库,但在将它们插入之前,我想确保同一用户针对相同的offername当前日期<没有重复的条目

我正在使用此查询,并且对于 useriddate 工作正常

//checking for existing values
$sql11=$Db1->query("SELECT COUNT(userid) AS total FROM TABLE where userid='$userid' and date BETWEEN (NOW() - INTERVAL 1 DAY) AND NOW()");
$temp=$Db1->fetch_array($sql11);
$thismemberinfo['count']=$temp[total];
//
if ($thismemberinfo['count'] >= 1 )
{ WARN HERE for duplicate entry}
else
{ INSERT TO DB }

我也想在检查中包含offername类似的事情

SELECT COUNT(userid) AS total FROM TABLE where userid='$userid' and  offername= '$offername'  date BETWEEN (NOW() - INTERVAL 1 DAY) AND NOW() 

请提出解决此问题的建议

最佳答案

Note:

You should not use table name as TABLE and column name date since these two words were reserved word in query. Use another name.

In order to make your query more simpler, just check both column (user_id & date) before add.

查看我的代码,了解我会如何做。

//checking for existing values
$sql11=$Db1->query("SELECT COUNT(userid) AS total FROM TABLE where userid='$userid' AND offername= '$offername' AND date BETWEEN (NOW() - INTERVAL 1 DAY) AND NOW()");
$temp = $Db1->fetch_array($sql11);
$thismemberinfo['count']=$temp['total'];
if ($thismemberinfo['count'] >= 1) {
// Just do nothing
} else {
//INSERT TO DB
//Current date
$current_date = date('Y-m-d');

$Db1->query("INSERT INTO TABLE (userid,offername,date) VALUES ('$user_id','$offername','$current_date')");
//Table should be replace with suitable name since TABLE is a reserved word in query
}

关于php - 如何在一些预检查后将值插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33555661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com