开发者

How can we store page data into our custom table in wordpress

I have HTML Page with two text fields called first & last name, I have created table using wordpress codex with my plugin. Now, when I click submit in HTML form page, I need to store the data which I enter in tex开发者_如何转开发t fields into my own Table. Kindly give a simple example.


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));

}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜