admin管理员组

文章数量:1201974

How can I delete a field in my database under the posts table?

For example in my posts table where the ID is 800 I would like to delete the a field under the column product_rank.

Any help will be appreciated.

How can I delete a field in my database under the posts table?

For example in my posts table where the ID is 800 I would like to delete the a field under the column product_rank.

Any help will be appreciated.

Share Improve this question edited Jan 28, 2018 at 13:52 emily00p asked Jan 28, 2018 at 13:43 emily00pemily00p 34 bronze badges 4
  • product_rank isn't a standard column in the posts table. Are you sure it's a column of the posts table and not meta? – Jacob Peattie Commented Jan 28, 2018 at 13:45
  • It's a column I've created in my database. I'm able to update this column using this code however I cant figure out how I can delete fields in this column. $wpdb->update($wpdb->posts, array('product_rank' => $rank), array('ID' => $post_id)); – emily00p Commented Jan 28, 2018 at 13:48
  • @emily00p What do you mean by deleting? You can't delete a single field, however you can set it to NULL or similar – kero Commented Jan 28, 2018 at 13:53
  • Can I erase the data in that field or somehow set it to NULL using WPDB? Would it be the same as updating the entry to 'NULL'? – emily00p Commented Jan 28, 2018 at 13:54
Add a comment  | 

2 Answers 2

Reset to default 2

You can't "delete" a field in MySQL, this only works for complete rows.

However, you can unset values, meaning setting them to their original state, usually NULL or an empty string.

$wpdb->update($wpdb->posts, array(
    'product_rank' => NULL
), array(
    '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);

本文标签: databaseHow to delete field using WPDB