我正在为我的网站构建一个简单的类似系统。问题是,jquery 帖子没有按照它应该做的工作。
数据库表上没有插入任何值,并且成功功能也不起作用..
所有数据库表都已设置好。
HTML:
<a href="javascript:like('85','17','product','1','3');" class="button like_click"><span><img src="wp-content/plugins/assets/images/icons/like-icon.png"> Like </span></a>
这是我的 jQuery。
function like(blog_id,object_id,object_type,user_id,default_count)
{
jQuery.ajax({
url: '/wp-content/plugins/assets/like.php',
type: 'post',
data: 'object_id=' + object_id + '&user_id=' + user_id + '&type=like&blog_id=' + blog_id + '&object_type=' + object_type,
dataType: json,
success: function(data)
{
jQuery('#' + object_id + '_count').html(data.total);
jQuery('.like_click').attr('href','javascript:unlike(\'' + blog_id + '\',\'' + object_id + '\',\'' + user_id + '\',\'' + object_type + '\',\'' + default_count + '\')');
jQuery('.like_click span').html('<img src="/wp-content/plugins/assets/images/icons/unlike-icon.png"> Unlike');
jQuery('.likes').html('You and <a href="#">' + default_count + ' others</a> like this.');
}
});
}
这like.php
<?php
// include wordpress functions
include( $_SERVER['DOCUMENT_ROOT'] . '/wp-load.php');
require_once( $_SERVER['DOCUMENT_ROOT'] . '/wp-admin/includes/plugin.php' );
global $wpdb;
$global_likes = $wpdb->base_prefix . "global_likes";
$global_table = $wpdb->base_prefix . "global_products_table";
$object_id = mysql_real_escape_string($_POST['object_id']);
$user_id = mysql_real_escape_string($_POST['user_id']);
$blog_id = mysql_real_escape_string($_POST['blog_id']);
$object_type = mysql_real_escape_string($_POST['object_type']);
$type = mysql_real_escape_string($_POST['type']);
$check_duplicate = $wpdb->get_row("SELECT object_id FROM ".$global_likes." WHERE object_type = '" .$object_type . "' and object_id = '".$object_id."' and blog_id = '" . $blog_id . "' and user_id = '" . $user_id . "' ");
if( empty($check_duplicate->object_id) && isset($_POST['object_id']) && isset($_POST['user_id']) && isset($_POST['blog_id']) && isset($_POST['object_type']) ) {
if( $type == "like" ) {
$wpdb->insert(
$global_likes,
array(
'user_id' => $user_id,
'blog_id' => $blog_id,
'object_id' => $object_id,
'object_type' => $object_type
)
);
$add_like = "UPDATE " . $global_table . " SET likes=likes+1 WHERE products_id = '".$object_id."' and blog_id = '" . $blog_id . "'";
$wpdb->query($add_like);
} elseif( $type == "unlike" ) {
$remove_like = "UPDATE " . $global_table . " SET likes=likes-1 WHERE products_id = '".$object_id."' and blog_id = '" . $blog_id . "'";
$delete_user_like = "DELETE FROM ".$global_likes." WHERE object_type = '" .$object_type . "' and object_id = '".$object_id."' and blog_id = '" . $blog_id . "' and user_id = '" . $user_id . "'";
$wpdb->query($remove_like);
$wpdb->query($delete_user_like);
}
$sql = mysql_fetch_assoc(mysql_query("SELECT likes FROM ".$global_table." WHERE products_id = '".$object_id."' and blog_id = '" . $blog_id . "'"));
echo json_encode(array('total' => $sql[0]['likes']));
} else {
wp_die('Error!');
}
?>
是否也可以只发布而不返回任何值,以便我可以删除以下脚本like.php
。
$sql = mysql_fetch_assoc(mysql_query("SELECT likes FROM ".$global_table." WHERE products_id = '".$object_id."' and blog_id = '" . $blog_id . "'"));
echo json_encode(array('total' => $sql[0]['likes']));
原因是,我已经有了一个自动获取点赞数的功能。
请帮忙。