我有一個點擊最喜歡的明星,我想檢查數據庫,如果記錄存在它將刪除記錄,如果它不存在它將插入記錄,什麼是問題未插入記錄刪除記錄,如果它存在,否則插入
<?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";
}
?>
真的嗎?讓__sure__明白'mysqli_query'和'mysql_query'之間的區別,然後再來。 –
@u_mulder抱歉,現在呢? –
現在你必須明白這一行是什麼意思'$ query =($ con,「SELECT * from favorite where userid ='$ userid'AND L_ID ='$ landmarkid'」);' –