728x90
3
I'm trying to delete a record from the database programmatically. When I have it hardcoded like this it does delete a record from the database:
$wpdb->delete( $table_name, array( 'user_id' => 1, 'timeMin' => 10), array('%d', '%d') );
However, when I try to do it in a dynamic manner with variables, it doesn't work. I even tried casting the variables to int to make sure they are they right type.
$id = (int) wp_get_current_user()->ID;
$time = (int) $_POST['umjp_time'];
$wpdb->delete( $table_name, array( 'user_id' => $id, 'timeMin' => $time), array('%d','%d'));
Why doesn't the dynamic code using variables work and how do I fix this?
Follow
asked May 16, 2018 at 12:27
Willem van der Veen
27.8k15 gold badges162 silver badges139 bronze badges
-
1why not just use $wpdb->query($wpdb->prepare())?– treyBakeMay 16, 2018 at 12:30
-
1I'll try, I thought this was the recommended way but it's not working– Willem van der VeenMay 16, 2018 at 12:31
-
with Wordpress I find that their standards/best practices are to avoid haha– treyBakeMay 16, 2018 at 13:17
-
First: do use $wpdb->delete. It handles a lot of the prepare, etc. behind the scenes. Second: what do you get when you var_dump($id)? or var_dump($time)? The problem is almost certainly related to the values in those variables.– random_user_nameMay 16, 2018 at 13:22
3 Answers
Sorted by:Reset to default
Highest score (default) Trending (recent votes count more) Date modified (newest first) Date created (oldest first)
1
I tried like this and it's working for me.
global $wpdb;
$id = (int) wp_get_current_user()->ID;
$time = (int) '4';
$table_name = 'testtable';
$wpdb->show_errors();
$wpdb->delete( $table_name, array( 'user_id' => $id, 'timeMin' => $time), array('%d','%d'));
$wpdb->print_error();
What errors you are getting please can you explain? You can print errors by using show_errors() and print_error() methods.
Follow
-
1This is probably the most correct solution. Those using DELETE FROM will work, but that isn't how wpdb is intended to work. A little explanation would work - the critical piece being the user of array('%d') which converts the integer appropriately.– Brian CJul 14, 2021 at 12:27
2
this is how I would recommend doing it:
function vendor_module_remove_dealer($data)
{
global $wpdb;
$sql = 'DELETE FROM `'. $wpdb->prefix .'my_table` WHERE `primary_id` = %d;';
try {
$wpdb->query($wpdb->prepare($sql, array($data['primary-id'])));
return true;
} catch (Exception $e) {
return 'Error! '. $wpdb->last_error;
}
}
this will prevent SQL Injection and delete your record safely, if it fails an error msg will be returned :)
-2
Please use below code i think it will work.
global $wpdb;
$id = (int) wp_get_current_user()->ID;
$time = (int) $_POST['umjp_time'];
$table_name = $wpdb->prefix . 'table_name';
if (!empty($id))
{
$wpdb->query($wpdb->prepare("DELETE FROM $table_name WHERE user_id IN($id)"));
}
Follow
-
1
-
Code-only answers are not useful. Why does this work, where OP's code doesn't? Further, this is wide open to SQL injection. Don't post answers that are open to this sort of security attack. May 16, 2018 at 13:21
-
-
1
-
1You need to look into how to use $wpdb->prepare. It's not being used properly in your code. May 16, 2018 at 13:47
'WEB > WP(WordPress)' 카테고리의 다른 글
call_user_func_array() (0) | 2022.07.04 |
---|---|
WooCommerce Plugin Developer Handbook (0) | 2021.06.11 |
Managing Orders And Woocommerce Status (0) | 2021.06.11 |
Woocommerce Payment Gateway (0) | 2021.06.11 |
Creating A Custom Plugin For WooCommerce Using WC_Integration Class in Distributed tab (0) | 2021.06.11 |