How to insert data into WordPress database ?

WordPress has its own database. Sometimes we have to insert data in a custom table. You can create a custom table in WordPress database and can insert data into WordPress database.  In this example I have used $wpdb function. Here is simple script to insert data into WordPress database custom table .

<?php /* Template Name: db */
global $wpdb;
$name=’yourname’;
if($wpdb->insert(
‘dbtable’,
array(
‘name’ => $name
)
) == false) wp_die(‘Database Insertion failed’);
else echo ‘Database insertion successful<p />’;
?>

$wpdb is WordPress function to interact with database., dbtable  is the name if database  table.  In array name is inserted into name column of dbtable table.

Leave a Reply