Thursday, December 15, 2011

How to insert page data into mysql table in wordpress

Insert  page data into mysql  table in wordpress

The following example creates a custom database table and inserts a row into the database. You can modify the INSERT query by replacing the variables with the values extracted from form fields. You can either store the INSERT SQL query in a string and use $wpdb -> query() function or directly use the $wpdb->insert() function to insert values into the table.
function initialize_custom_table(){
  global $wpdb;
  $table_name = "customtable";
  $sql = "CREATE TABLE IF NOT EXISTS `" . $table_name . "` (
                            `id` int(10) NOT NULL AUTO_INCREMENT,
                            `name` VARCHAR(25) NOT NULL,
                            `value` VARCHAR(45) NOT NULL,
                            PRIMARY KEY (`id`)
                            );" ;

  $wpdb->query($sql);

  $name = "Hello";
  $value = "World";

  $rows_affected = $wpdb->insert($table_name, array(
                   'name' => $name, 'value' => $value));
}

1 comment:

  1. I was using $wpdb->query($sql); but its showing error "7878787 cannot be find"





    Notice forms

    ReplyDelete