Opened 6 years ago
Last modified 6 years ago
#45936 new defect (bug)
Insert empty record
Reported by: | maximeschoeni | Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | |
Component: | General | Keywords: | reporter-feedback |
Focuses: | Cc: |
Description
I fall upon a small bug when trying to insert an empty record in a custom table. To reproduce, just create a basic table 'my_table' and use this code:
$wpdb->insert( 'my_table' array(), array() );
which produce this mysql:
"INSERT INTO `my_table` (``) VALUES ()"
which throw an error. So instead I would like it produces this result:
"INSERT INTO `my_table` () VALUES ()"
Which would just achieve the expected result: create an empty record.
Now the fixing is very easy:
just replace this line (/wp-include/wp-db.php line 2087, _insert_replace_helper
function)
$fields = '`' . implode( '`, `', array_keys( $data ) ) . '`';
by
$fields = $data ? '`' . implode( '`, `', array_keys( $data ) ) . '`' : '';
Change History (2)
Note: See
TracTickets for help on using
tickets.
Your suggestion does fix the issue of inserting empty record.
But what could be a working situation where you might need to insert an empty record? An example would be good.
Also if a numeric
Primary Key
is present in the table and it is not auto incremental, trying to insert multiple empty records will throw duplicate primary key error.Because it will try to add
0
in the primary key.