[wp-trac] [WordPress Trac] #45936: Insert empty record

WordPress Trac noreply at wordpress.org
Fri Jan 11 08:52:24 UTC 2019


#45936: Insert empty record
---------------------------+-----------------------------
 Reporter:  maximeschoeni  |      Owner:  (none)
     Type:  defect (bug)   |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  General        |    Version:
 Severity:  normal         |   Keywords:
  Focuses:                 |
---------------------------+-----------------------------
 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 ) ) . '`' :
 '';
 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/45936>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list