1

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.

2 Answers 2

2

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:

require_once( dirname( dirname( dirname( dirname( __FILE__ )))) . '/wp-load.php' );

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:

Sign up to request clarification or add additional context in comments.

2 Comments

Perfect! I found this and used it. $absolute_path = explode('wp-content', $_SERVER['SCRIPT_FILENAME']); $wp_load = $absolute_path[0] . 'wp-load.php'; require_once($wp_load);
I just edited the answer to match what another plugin developer did, as shown on the linked "great explanation" page.
2

Incorrect backticks:

$wpdb->delete( $ffui_items, array( 'ID' => `$id` ) );
                                           ^---^--

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.

1 Comment

Thanks. Is there a way to include the wordpress libraries in my php file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.