wordpress insert data into custom table code example

Example 1: insert data to table in wordpress

if ( isset( $_POST['submit'] ) ){

         global $wpdb;
         $tablename = $wpdb->prefix.'post_job';

        $wpdb->insert( $tablename, array(
            'organizationname' => $_POST['organizationname'], 
            'post' => $_POST['post'],
            'publishfrom' => $_POST['publishfrom'], 
            'publishupto' => $_POST['publishupto'],
            'qualification1' => $_POST['qualification1'], 
            'qualification2' => $_POST['qualification2'], 
            'qualification3' => $_POST['qualification3'],
            'qualification4' => $_POST['qualification4'], 
            'experience1' => $_POST['experience1'], 
            'experience2' => $_POST['experience2'],
            'experience3' => $_POST['experience3'], 
            'training1' => $_POST['training1'], 
            'training2' => $_POST['training2'], 
            'training3' => $_POST['training3'],
            'training4' => $_POST['training4'], 
            'training5' => $_POST['training5'] ),
            array( '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s' ) 
        );
    }

Example 2: create table and insert data in wordpress plugin

<?php
function table_create()
{
global $wpdb;
$table_name = $wpdb->prefix . "wpactable"; 
$charset_collate = $wpdb->get_charset_collate();
 $sql = "CREATE TABLE IF NOT EXISTS $table_name (
  id mediumint(9) NOT NULL AUTO_INCREMENT,
  name tinytext NOT NULL,
  email tinytext NOT NULL,
  number tinytext NOT NULL,
  Website text NOT NULL,
  PRIMARY KEY  (id)
) $charset_collate";
require_once( ABSPATH .'wp-admin/includes/upgrade.php' );
dbDelta( $sql );
}
function inseruser()
{global $wpdb;
$table_name = $wpdb->prefix . "wpactable"; 
$wpdb->insert('wp_wpactable',
array('name'=>$name,
'email'=>$email,
'number'=>$number,
'Website'=>$Website),
array('%s','%s'));
register_activation_hook(__FILE__, 'table_create' );
register_activation_hook(__FILE__, 'inseruser' );
?>

Tags:

Php Example