This is my first attempt at using Ajax and PHP to delete a row from MySQL. I’m getting the error:
PHP Fatal error: Call to a member function delete() on a non-object in /home/insightd/public_html/dev/wp-content/plugins/id-ffui/lib/delete.php on line 10
Everything is working as it should but the row is not deleting.
The delete part of the HTML form:
echo "<div class='ffui-media-item'>";
echo "<div class='ffui-delete' align='center'><a href='#' id='" . $media_item['ID'] . "' class='delbutton' title='Click To Delete'>X</a></div>";
The jQuery/Ajax part:
<script type="text/javascript">
$(function() {
$(".delbutton").click(function(){
//Save the link in a variable called element
var element = $(this);
//Find the id of the link that was clicked
var del_id = element.attr('id');
//Built a url to send
var info = {"id" : del_id };
if(confirm("Are you sure you want to delete this Record?")) {
$.ajax({
type: "POST",
url: "<?php echo plugins_url('id-ffui/lib/delete.php') ?>",
data: info,
success: function(){
}
});
$(this).parents(".ffui-media-item").animate({ backgroundColor: "#fbc7c7" }, "fast")
.animate({ opacity: "hide" }, "slow");
}
return false;
});
});
</script>
And the delete.php file:
global $wpdb;
global $ffui_db_version;
$ffui_items = $wpdb->prefix . "ffui_items";
if($_POST['id']) {
$id = $_POST['id'];
$wpdb->delete( $ffui_items, array( 'ID' => `$id` ) );
}
Any help would be greatly appreciated. Thanks in advance.
If the code you show is the entirety of delete.php, then $wpdb hasn’t been instantiated. You need to load the core WP files first:
The rest of your code comes after this. And if this is the case, you don’t need the “global” commands, since you’re not operating inside a function or object.
Here’s a great explanation of wp-load.php:
Incorrect backticks:
PHP does not use backticks in its code. It should be just
'ID' => $id
As for the actual error message, you don’t appear to have included the wordpress libraries, so
$wpdb
doesn’t actually exist.