8

I am having trouble finding a good resource on using the $wpdb function.

I am trying to delete a row from a custom table named: eLearning_progress

$removefromdb = $wpdb->query("DELETE FROM eLearning_progress WHERE ID = '$user_id' AND module_id = '$singlecomparearrays_remove'" );

The row I would like to delete has the ID of '$user_id' and the 'module_id' of '$singlecomparearrays_remove'.

I have also tried:

$removefromdb = $wpdb->query( "DELETE FROM eLearning_progress WHERE ID = ($user_id) AND module_id = ($singlecomparearrays_remove)" );

and then:

$removefromdb = $wpdb->query($wpdb->prepare("DELETE FROM eLearning_progress WHERE ID = %s AND module_id = %s", $user_id, $singlecomparearrays_remove));

Please try not to sigh too loudly at my attempts but I can't find a good guide on using the DELETE command with variables in there too. Any help is much appreciated.

Regards, Alex

  • 1
    Have you read the codex about delete()? – bueltge Jan 11 '17 at 19:55
  • Yes, but I don't understand it enough to get it to work with my variables. Would you be able to give me an example? – Alex Green Jan 12 '17 at 9:36
12

You should use the delete() function to remove a row.

A small example to delete the raw 'ID' in the custom table 'eLearning_progress'.

$id = 0815;
$table = 'eLearning_progress';
$wpdb->delete( $table, array( 'id' => $id ) );

But I Can't see which raw you will delete in your table 'eLearning_progress'? Maybe you enhance the question to understand it much better.

0

It still possible that your sql is creating trouble even use $wpdb->delete I found it useful using one of these approach:

  1. call $wpdb->show_errors(); before querying
  2. enable debug mode with define( 'WP_DEBUG', true ); on your wp-config.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.