How to insert query in wordpress database

I have a email subscription to my site and I want to insert to my WordPress database so I can export the email listing. I already created table wp_email_subscription with 4 fields ID, name, email and date created. what will be the query for this? is there any wordpress database script to use?

Related posts

Leave a Reply

6 comments

  1. WordPress provides the $wpdb class of functions for interacting with the database.

    To insert an email address you could do something like:

    <?php 
    
      $wpdb->insert('wp_email_subscription', 
        array(
          'name'          => 'name',
          'address'       => 'name@email.com'
        ),
        array(
          '%s',
          '%s'
        ) 
      ); 
    
    ?> 
    

    More info on the WordPress Codex.

  2. $wpdb->query("INSERT INTO wp_email_subscription (name, email, date) VALUES ('$name', '$email', '$date')"  );
    

    This is if you want to insert values to your table. You do not have to use $wpdb->email_subscription for the prefix as it’s the table you created yourself, otherwise, if you were inserting values to default WordPress tables you would prefer doing
    $wpdb->users etc.

  3.  global $wpdb
    
     $wpdb->insert('wp', array(
                            'email' => $_POST['email'],
                            'city'  =>   $_POST['city'],
                            'state' =>$_POST['state'],
                            'phone' => $_POST['phone'],
                           'mobile' => $_POST['mobile'],
                           )
                 );
    
  4. global $wpdb;
    $table = $wpdb->prefix.'you_table_name';
    $data = array('column1' => 'data one', 'column2' => 123);
    $format = array('%s','%d');
    $wpdb->insert($table,$data,$format);
    

    All information you can read here