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 text fields into my own Table. Kindly give a simple example.

Related posts

Leave a Reply

1 comment

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

    }