WordPress.org

Ready to get started?Download WordPress

Forums

Delete rows not work wpdb (6 posts)

  1. brunogysin
    Member
    Posted 8 months ago #

    Hi
    I'm trying this way to delete row in table
    I used this to test, but nothing in the table is deleted

    $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->Seriados WHERE post_id = %d",$post_id));
    $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->Seriados WHERE post_id = %i",2499));

    it does not erase anything according to column : post_id

    what's wrong with that?

    this is the table structure:
    post_id
    season_id
    episode_id
    episode_value

    http://imm.io/1n0Ir

    sorry my bad english
    sorry if I posted this topic in the wrong place.

  2. keesiemeijer
    moderator
    Posted 8 months ago #

    Do you globalize $wpdb before using it?

    global $wpdb;
    $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->Seriados WHERE post_id = %d",$post_id));

    or without the wp_ prefix for your custom table
    http://codex.wordpress.org/Editing_wp-config.php#table_prefix

    global $wpdb;
    $wpdb->query($wpdb->prepare("DELETE FROM Seriados WHERE post_id = %d",$post_id));

    Another way to delete rows is the delete method:
    http://codex.wordpress.org/Class_Reference/wpdb#DELETE_Rows

    It's important to backup your database before using these functions.

  3. brunogysin
    Member
    Posted 8 months ago #

    add_action( 'save_post', 'SalvarDados' );
    
    function SalvarDados( $post_id ) {
    $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->Seriados WHERE post_id = %d",$post_id));
    }

    My table has the prefix:
    wp_Seriados

    but does not delete the row

  4. keesiemeijer
    moderator
    Posted 8 months ago #

    Try globalizing the $wpdb object:

    add_action( 'save_post', 'SalvarDados' );
    
    function SalvarDados( $post_id ) {
    global $wpdb;
    $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->Seriados WHERE post_id = %d",$post_id));
    }

    Try setting WP_DEBUG to true to see if any sql errors are displayed:
    http://codex.wordpress.org/WP_DEBUG

    See: http://codex.wordpress.org/Plugin_API/Action_Reference/save_post

  5. brunogysin
    Member
    Posted 8 months ago #

    I posted wrong here, already have the line

    http://i.imgur.com/rQITyGa.jpg

    I activated WP_DEBUG

    WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE post_id = 2822' at line 1]
    DELETE FROM WHERE post_id = 2822

    my table

    http://i.imgur.com/BUOwtdi.jpg

  6. keesiemeijer
    moderator
    Posted 8 months ago #

    Try it with this:

    $wpdb->query($wpdb->prepare("DELETE FROM $wpdb->Seriados WHERE $wpdb->Seriados.post_id = %d",$post_id));

Reply

You must log in to post.

About this Topic

Tags

No tags yet.