0

I test some code from end 2016 and discover that $wpdb->insert() doesnt work anymore. Maybe something todo with php7? I dont know.

Before below code did work normal. The data in $_POST() is correct filled.

if(isset($_POST['add_new_banner'])){    
 extract($_POST); // correct data
$wpdb->insert( 
$wpdb->prefix.'rdp_banners', 
    array( 
        'banner_id'     => $banner_id,  
        'rdp_banner'    => $rdp_banner,     
        'banner_type'   => $banner_type,            
        'customer_id'   => $customer_id,            
        'status'        => $status,             
        'company_name'  => $company_name,
        'picture'       => $picture,
        'register_date' => $register_date,
        'start_date'    => $start_date,
        'end_date'      => $end_date,   
        'price'         => $price,
        'banner_link'   => $banner_link,
    ), 
    array( '%d', '%d', '%d', '%d', '%d', '%s', '%s', '%s','%s', '%s', '%s', '%s',) 
);

Do I need to change somewhere the code to make it working? Hope someone can tell me what I do wrong now.

1
  • Please elaborate on "doesnt work anymore", any error messages in the logs? Otherwise I would avoid extract and add e.g. some validation. Make sure $wpdb isnit undefined.
    – birgire
    Commented Aug 16, 2017 at 10:38

1 Answer 1

1

examine below example and figure out what is wrong with your code.

 global $wpdb;
if(isset($_POST['submit']))
{
    $table_name = $wpdb->prefix.'employee';
    $wpdb->insert(
                    $table_name,
                    array(
                            'first_name'=>$fname,
                            'last_name'=>$lname),
                    array( '%s','%s' )
                 );

use global $wpdb;

global $wpdb;
$table_name = $wpdb->prefix.'rdp_banners';
$wpdb->insert(
            $table_name,
            array(
                'banner_id'     => $banner_id,  
                'rdp_banner'    => $rdp_banner,     
                'banner_type'   => $banner_type,            
                'customer_id'   => $customer_id,            
                'status'        => $status,             
                'company_name'  => $company_name,
                'picture'       => $picture,
                'register_date' => $register_date,
                'start_date'    => $start_date,
                'end_date'      => $end_date,   
                'price'         => $price,
                'banner_link'   => $banner_link),
      array( '%d', '%d', '%d',  '%d',   '%d',   '%s',   '%s', '%s', '%s',   '%s',   '%s',   '%s')
     );
6
  • use this variable global $wpdb;
    – DHL17
    Commented Aug 16, 2017 at 11:02
  • still no success with global $wpdb;
    – jtermaat
    Commented Aug 16, 2017 at 11:06
  • Can you show me MYSQL table fields & table name.
    – DHL17
    Commented Aug 16, 2017 at 11:10
  • see above code. simple insert new record data in a table.
    – jtermaat
    Commented Aug 16, 2017 at 11:11
  • enable database error display via $wpdb->show_errors() check what query is being formed and run via $wpdb->last_query
    – DHL17
    Commented Aug 16, 2017 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.