gpt4 book ai didi

php - 如果特定行数据尚未在同一日期插入,如何检查和插入数据

转载 作者:行者123 更新时间:2023-11-29 17:58:33 26 4
gpt4 key购买 nike

我想将特定的行数据插入到我的数据库表中,但首先,我们必须检查数据库表中是否存在行数据。如果记录不存在,我会将 useridcompanyid 和当前日期插入到我的表中。这是列表:

list is given in link below

我的获取数据的代码:

<?php

$con=mysqli_connect("localhost" , "root" , "" , "accounts");
if (!$con) {
echo "connection failed";
}

$query= "
SELECT *
FROM company
WHERE catagory = 'A'
";
/*"SELECT * FROM `company` WHERE catagory='A'";*/
$result=mysqli_query($con,$query);
while ($row=mysqli_fetch_array($result)) {
?>
<tbody>
<tr>
<td><a href='/bw/gymdetail.php?id=<?=$row['id'];?>'><?=$row['id'];?></a></td>
<td><a href='/bw/gymdetail.php?id=<?=$row['id'];?>'><?=$row['first_name']?></a></td>
<td><?=$row['u_city'];?></td>
<td><?=$row['u_address'];?></td>
<td><a href='/bw/login-system/checkin.php?id=<?=$row['id'];?>&uid=<?=$_REQUEST['id'];?>'> Check In</a></td>
</tr>

</tbody>
<?php

}
?>

checkin.php页面

<?php

require 'db.php';

if (isset($_GET['uid'])) {

$id = $_GET['id'];
$uid = $_GET['uid'];
$now=new datetime();
$date = $now->format('m-d-y');

$sql = $mysqli->query("
SELECT *
FROM checkins
WHERE userid = '$id'
AND `dateis` = '$date'
");
$productcount = $sql->num_rows;
if($productcount == '1'){
$sql = "
INSERT INTO checkins (userid, companyid, dateis)
VALUES ('$uid','$id','$date')
";
$query = mysqli_query($mysqli,$sql);
}
else{

echo "already clicked";
}
}

?>

如果用户单击 checkin 按钮。我需要检查该行是否已在具有相同用户和相同日期的表中。如果不存在则插入数据,否则显示记录已存在的消息。

最佳答案

看看这个

https://chartio.com/resources/tutorials/how-to-insert-if-row-does-not-exist-upsert-in-mysql/#using-insert-ignore

<?php

require 'db.php';

if (isset($_GET['uid'])) {

$id = $_GET['id'];
$uid = $_GET['uid'];
$now=new datetime();
$date = $now->format('m-d-y');
$sql = "INSERT IGNORE INTO checkins (userid, companyid, dateis) "
. "VALUES ('$uid','$id','$date')";
$query = mysqli_query($mysqli,$sql);

if(!mysqli_affected_rows($mysqli)){
echo "already clicked";
}
}

?>

关于php - 如果特定行数据尚未在同一日期插入,如何检查和插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48595407/

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