Support » Fixing WordPress » Delete rows not work wpdb

  • 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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    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.

    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

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    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

    I posted wrong here, already have the line

    View post on imgur.com

    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

    View post on imgur.com

    Moderator keesiemeijer

    (@keesiemeijer)

    moderator

    Try it with this:

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

    What about this?

    $table_name = $wpdb->prefix . 'seriados';
    
    $id = $post_id; // get post_id for POST or GET or Query. You know
    
    function delete_seriados() {
    	$wpdb->delete(
    		$table_name,
    		array(
    			'ID' => $id
    			) // End array
    		); // End delete
    } // End delete_seriados function
    
    delete_seriados();

    Look after that table’s name is capitalize or not.
    Luck

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Delete rows not work wpdb’ is closed to new replies.