gpt4 book ai didi

php - 如果存在则删除该记录,否则插入

转载 作者:行者123 更新时间:2023-11-29 19:01:48 24 4
gpt4 key购买 nike

我有一个点击最喜欢的星星,我想检查数据库中是否有该记录,如果存在则删除该记录,如果不存在则插入该记录,记录未插入是什么问题

     <?php
include "config.php";
header('Content-Type: application/json');
$landmarkid = $_GET['landmarkid'];
$userid = $_GET['userid'];


try {
$query = mysqli_query($con,"SELECT * from favourite WHERE userid =$userid AND L_ID = $landmarkid");



if(mysqli_num_rows($query) > 0)
{
$q1 = mysqli_query($con,"DELETE from favourite WHERE userid =$userid AND L_ID = $landmarkid");

if($q1){
echo '{"Deleted":"true"}';
}
else {
echo '{"Deleted":"false"}';
}
}
else {
$q2 = mysqli_query($con,"INSERT INTO favourite (userid,L_ID) VALUES ( $userid, $landmarkid) ");

if($q2){
echo '{"inserted":"true"}';
}
else {
echo '{"inserted":"false"}';
}
}

} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
?>

最佳答案

尝试在插入语句中添加单引号,看看是否有效。更改此声明;

$q2 = mysqli_query($con,"INSERT INTO favourite (userid,L_ID) VALUES ( $userid, $landmarkid) ");

对此;

$q2 = mysqli_query($con,"INSERT INTO favourite (userid,L_ID) VALUES ( '$userid', '$landmarkid') ");

如果有帮助或者您发现问题,请告诉我。

关于php - 如果存在则删除该记录,否则插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43818083/

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