admin管理员组

文章数量:1128472

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

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

Share Improve this question asked Jan 11, 2017 at 18:53 Alex GreenAlex Green 1111 gold badge1 silver badge5 bronze badges 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
Add a comment  | 

4 Answers 4

Reset to default 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.

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

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' "));

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);

本文标签: sqlHow to use wpdb to delete in a custom table