所以我在页面上有一个项目列表。我试图添加功能,以便用户可以喜欢/不喜欢这些项目。当用户点击;收藏夹'链接它应该将moodmemo_id和user_id发布到数据库中的收藏表,但是它不起作用。
HTML:
<a href="javascript:;" id="button" name="button0" class="ajaxFavourite">
Javascript:
$(function(){
$(document).on('click','.ajaxFavourite',function(){
// Configure those variables as appropriate
var divid = 'status';
var favid= $(this).attr('id');
var url = 'addremove.php';
// The XMLHttpRequest object
var xmlHttp;
try{
xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
}
catch (e){
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
}
catch (e){
try{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e){
alert("Your browser does not support AJAX.");
return false;
}
}
}
// Generate timestamp for preventing IE caching the GET request
fetch_unix_timestamp = function()
{
return parseInt(new Date().getTime().toString().substring(0, 10))
}
var timestamp = fetch_unix_timestamp();
var nocacheurl = url+"?t="+timestamp;
// This code sends the variables through AJAX and gets the response
xmlHttp.onreadystatechange=function(){
if(xmlHttp.readyState!=4){
document.getElementById(divid).innerHTML='<img src="images/spinner.gif"> Wait...';
}
if(xmlHttp.readyState==4){
document.getElementById(divid).innerHTML=xmlHttp.responseText;
}
}
xmlHttp.open("GET",nocacheurl+"&favid="+favid,true);
xmlHttp.send(favid);
// Finally, some code for button toggle
var button = document.getElementById('button');
switch(button.name)
{
case 'button0':
button.text = 'Unfavourite';
button.name = 'button1';
break;
case 'button1':
button.text = 'Favourite';
button.name = 'button0';
break;
}
});
});
addremove.php
$favid = mysql_real_escape_string($_GET['favid']);
// Firstly, check if article is favourite or not
$query = mysqli_query($mysqli, "SELECT * FROM tbl_favourites WHERE user_id='$loggedInUser->user_id' AND moodmemo_id=$favid");
$matches = mysqli_num_rows($query);
// If it is not favourited, add as favourite
if($matches == '0'){
mysqli_query($mysqli,"INSERT INTO tbl_favourites (user_id, moodmemo_id) VALUES ($loggedInUser->user_id, '$favid')");
echo "<div class=\"green\">This is a favourite</div>";
}
// Instead, if it is favourited, then remove from favourites
if($matches != '0'){
mysqli_query($mysqli,"DELETE FROM tbl_favourites WHERE user_id='$loggedInUser->user_id' AND moodmemo_id=$favid");
echo "<div class=\"red\">This is NOT a favourite</div>";
}
addremove.php中的查询失败。与...有关:我认为$query = mysqli_query($mysqli, "SELECT * FROM tbl_favourites WHERE user_id='$loggedInUser->user_id' AND moodmemo_id=$favid");
。我得到警告:mysqli_num_rows()要求参数1为mysqli_result,布尔值为错误。
我可以帮忙吗? 感谢