所以我有这个代码(PHP和jQuery),它应该将数据保存到WordPress数据库并检测数据库中是否已存在该帖子。如果帖子存在,则回显“已经保存”。
问题是,它可以保存一些物品,而不会保存其他物品。我不知道还有什么可以调试这个问题。
public static function wpnp_save_news(){
global $wpdb;
$table_name = $wpdb->prefix.'wpnp';
if (is_user_logged_in()) {
if (isset($_POST['delete'])) {
$id = $_POST['id'];
$wpdb->delete( $table_name, array( 'id' => $id ) );
echo "Article deleted";
}elseif (isset($_POST['savefav'])) {
$user = $_POST['user_id'];
$title = mysql_real_escape_string($_POST['title']);
$sql = "SELECT count(*) FROM $table_name WHERE user=$user and title='".$title."'";
$count = $wpdb->get_var($sql);
if($count <= 0 ){
$wpdb->insert(
$table_name,
array(
'user' => $_POST['user_id'],
'title' => $_POST['title'],
'summary' => $_POST['summary'],
'url' => $_POST['post_url'],
'img' => $_POST['img_url'],
'author' => $_POST['author'],
'source' => $_POST[ 'source']
)
);
echo "Article saved";
}else{
echo "Article already saved";
}
}else{
echo "Please login to save articles";
}
}
wp_die();
}
-----------------------------------------
$('.wpnp-delete').on('click',(e)=>{
e.preventDefault();
const url = wpnpObj.au;
const delete_data = {
action: wpnpObj.ff,
'delete': true,
'id': e.currentTarget.dataset.id
};
const success = data =>{
alert(data)
window.location.reload()
};
const err = data =>{
console.log(data);
}
$.post(url,delete_data,success).fail(err);
})