我有一张名为tbl_favorite的表。有两个名为 user_id 和 quote_id 的字段。我在参数中传递这两个字段。我想检查 user_id 是否有包含该 quote_id 的行。如果它存在,我想要像“已经存在”这样的响应,否则我想将该数据插入表中。
<?php
header('Content-Type: application/json; Charset=UTF-8');
include("dbconnection.php");
$userid= $_GET['userid'];
$quoteid= $_GET['quoteid'];
// I am confused to write condition
$query= mysqli_query($conn,"SELECT fav_id FROM tbl_fav WHERE user_id='".$userid.'" AND fav_qu_id="'.$quoteid.'"");
if(mysqli_num_rows($query) > 0){
$response="already exist";
}
else{
$query= mysqli_query($conn,"INSERT INTO tbl_fav(user_id,fav_qu_id VALUE('".$userid."','".$quoteid."')");
$response['message']='success';
}
echo json_encode($response);
?>
我的网址如下
example.com/api.php?user_id=1&"e_id=1
我收到名为“解析错误”的错误:语法错误,意外的“”。如果有人纠正我,请告诉我。我是 Android 开发人员,不了解正确的 PHP。谢谢
最佳答案
$resource = "SELECT * FROM tbl_favorite WHERE user_id='{$userid}' AND quote_id='{$quoteid}'";
$result = if (mysqli_query($query)){
return true;
}
if ($result){
echo "that quote exists within the given user";
}
关于php - 比较 mysqli 查询中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46476625/