10

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

2
  • 1
    Have you read the codex about delete()?
    – bueltge
    Commented Jan 11, 2017 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
    Commented Jan 12, 2017 at 9:36

4 Answers 4

34

The best WP API solution for this goal is to 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
0

Using This You can easily delete

$MailMetaKey = yourkey
$post_id = yourpostid

$wpdb->query($wpdb->prepare("DELETE * FROM f0_postmeta WHERE meta_key = $MailMetaKey' and post_id = '$post_id' "));
1
  • 1
    That's not a lot different to the OP's examples that didn't work, except that you're using prepare(). What does prepare() do if you're not using the function's argument substitution?
    – Rup
    Commented Dec 16, 2021 at 11:39
0

This code not working, show an error. I'm tried

Please look at this screenshot https://prnt.sc/M3DYH_EmHCtM

global $wpdb;

$id = $_REQUEST['delete'];

$table = 'custom_user_info';

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

But when I'm using this code, It's working perfectly

$db_config = mysqli_connect('localhost', 'root', '', 'develop');

$id = $_REQUEST['delete'];

$delete = "DELETE FROM custom_user_info WHERE id=$id";

$query = mysqli_query($db_config, $delete);
1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Apr 16, 2022 at 20:05

Your Answer

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

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