admin管理员组文章数量:1130349
I am trying to delete records from my custom table but it does not delete anything.
Here is my code:
<?php
global $wpdb;
$retrieve_data = $wpdb->get_results( "SELECT * FROM wp_paypal" );
// some code to display here...
?>
<form method="post" enctype="multipart/form-data">
<td><input type="submit" name="delete" value="Delete" /></td>
</form>
<?php
$myid= $retrieved_data->id;
if (isset($_POST['delete'])) {
//global $wpdb;
$wpdb->query(
'DELETE FROM $wpdb->paypal
WHERE id = "'.$myid.'"
'
);
}
}
I am trying to delete records from my custom table but it does not delete anything.
Here is my code:
<?php
global $wpdb;
$retrieve_data = $wpdb->get_results( "SELECT * FROM wp_paypal" );
// some code to display here...
?>
<form method="post" enctype="multipart/form-data">
<td><input type="submit" name="delete" value="Delete" /></td>
</form>
<?php
$myid= $retrieved_data->id;
if (isset($_POST['delete'])) {
//global $wpdb;
$wpdb->query(
'DELETE FROM $wpdb->paypal
WHERE id = "'.$myid.'"
'
);
}
}
Share
Improve this question
edited Nov 30, 2018 at 13:52
Maxime
1337 bronze badges
asked Jun 20, 2015 at 7:11
user3463054user3463054
311 gold badge1 silver badge3 bronze badges
1
- What problem are you facing? – sakibmoon Commented Jun 20, 2015 at 7:51
2 Answers
Reset to default 6Try to use $wpdb->prefix insted of $wpdb in Delete query.
Example:
$wpdb->query(
'DELETE FROM '.$wpdb->prefix.'paypal
WHERE id = "'.$myid.'"'
);
I know I'm late... but the main issue in your question is that you are using single quotes (') in your statement:
$wpdb->query(
'DELETE FROM $wpdb->paypal
WHERE id = "'.$myid.'"
'
);
This means that $wpdb->paypal is not producing the result you are expecting. First, you assume this displays the "paypal" table name. But it doesn't.
If $myid has a value of 4, your PHP code will produce this SQL statement :
DELETE FROM $wpdb->paypal WHERE id = 4
... instead of being:
DELETE FROM wp_paypal WHERE id = 4
To fix the issue, you must:
- Change your single quotes to double quotes. (Read why)
- Add the table prefix in front of the table name.
Like this:
$table_name = $wpdb->prefix . 'paypal';
$wpdb->query( "DELETE FROM {$table_name} WHERE id = '{$myid}'" );
Also, make sure you sanitize the $myid variable because if I submit the value 0 OR 1=1, this will produce this SQL statement:
DELETE FROM wp_paypal WHERE id = 0 OR 1=1
... and will delete every row in the table
I am trying to delete records from my custom table but it does not delete anything.
Here is my code:
<?php
global $wpdb;
$retrieve_data = $wpdb->get_results( "SELECT * FROM wp_paypal" );
// some code to display here...
?>
<form method="post" enctype="multipart/form-data">
<td><input type="submit" name="delete" value="Delete" /></td>
</form>
<?php
$myid= $retrieved_data->id;
if (isset($_POST['delete'])) {
//global $wpdb;
$wpdb->query(
'DELETE FROM $wpdb->paypal
WHERE id = "'.$myid.'"
'
);
}
}
I am trying to delete records from my custom table but it does not delete anything.
Here is my code:
<?php
global $wpdb;
$retrieve_data = $wpdb->get_results( "SELECT * FROM wp_paypal" );
// some code to display here...
?>
<form method="post" enctype="multipart/form-data">
<td><input type="submit" name="delete" value="Delete" /></td>
</form>
<?php
$myid= $retrieved_data->id;
if (isset($_POST['delete'])) {
//global $wpdb;
$wpdb->query(
'DELETE FROM $wpdb->paypal
WHERE id = "'.$myid.'"
'
);
}
}
Share
Improve this question
edited Nov 30, 2018 at 13:52
Maxime
1337 bronze badges
asked Jun 20, 2015 at 7:11
user3463054user3463054
311 gold badge1 silver badge3 bronze badges
1
- What problem are you facing? – sakibmoon Commented Jun 20, 2015 at 7:51
2 Answers
Reset to default 6Try to use $wpdb->prefix insted of $wpdb in Delete query.
Example:
$wpdb->query(
'DELETE FROM '.$wpdb->prefix.'paypal
WHERE id = "'.$myid.'"'
);
I know I'm late... but the main issue in your question is that you are using single quotes (') in your statement:
$wpdb->query(
'DELETE FROM $wpdb->paypal
WHERE id = "'.$myid.'"
'
);
This means that $wpdb->paypal is not producing the result you are expecting. First, you assume this displays the "paypal" table name. But it doesn't.
If $myid has a value of 4, your PHP code will produce this SQL statement :
DELETE FROM $wpdb->paypal WHERE id = 4
... instead of being:
DELETE FROM wp_paypal WHERE id = 4
To fix the issue, you must:
- Change your single quotes to double quotes. (Read why)
- Add the table prefix in front of the table name.
Like this:
$table_name = $wpdb->prefix . 'paypal';
$wpdb->query( "DELETE FROM {$table_name} WHERE id = '{$myid}'" );
Also, make sure you sanitize the $myid variable because if I submit the value 0 OR 1=1, this will produce this SQL statement:
DELETE FROM wp_paypal WHERE id = 0 OR 1=1
... and will delete every row in the table
本文标签: queryDeleting data from a custom table in WordPress
版权声明:本文标题:query - Deleting data from a custom table in WordPress 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749138292a2321748.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论