0

Can`t insert data of form into the table, I have also tried other ways. Below is the code.

<?php /* Template Name: The custom email */ ?>
<?php get_header(); ?>
<?php 
$sql =  "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix.credofy_contact_form. " (
                  id mediumint(12) NOT NULL AUTO_INCREMENT,
                  your_name VARCHAR(200) NOT NULL,
                  your_email VARCHAR(200) NOT NULL,
                  your_phone VARCHAR(200) NOT NULL,
                  your_hobby VARCHAR(200) NOT NULL,
                PRIMARY KEY (id));";
$wpdb->query($sql);
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
?>

<form action="" method="post"  enctype="multipart/form-data">
    <input type="text" name="cr-your-name" placeholder="Enter your Name"/>
    <input type="text" name="cr-your-email" placeholder="Enter your Email"/>
    <input type="text" name="cr-your-phone" placeholder="Enter your Phone"/>
    <input type="text" name="cr-your-hobby" placeholder="Enter your Hobby"/>
    <input type = "submit" name = "cr-submit" value = "Insert">
</form>

<?php
if(isset($_POST['cr-submit'])){
global $wpdb;
$table=$wpdb->prefix.'credofy_contact_form';
$post_data=array(
'yourName' => $_POST['cr-your-name'],
'yourEmail' => $_POST['cr-your-email'],
'yourPhone' => $_POST['cr-your-phone'],
'yourHobby' => $_POST['cr-your-hobby']
);
$wpdb->insert( $table, $post_data);
}
?>
<?php get_footer(); ?>
  • can you please set define('WP_DEBUG', true) in wp-config.php, also please add global $wpdb; above while create new table – Evince Development Apr 18 at 11:50
  • 2
    Please please please sanitize your inputs before inserting them into your database. If your current code did work it would be extremely insecure. – mrben522 Apr 18 at 12:46
  • @mrben522 Please let me know how to do that? :-( – gaurav mishra Apr 19 at 4:39
  • this post explains sanitization and validation really well – mrben522 Apr 20 at 16:21
1

There appears to be a syntax error on line 4.

                                                     string is not quoted
                                                     ↓         ↓         ↓
$sql =  "CREATE TABLE IF NOT EXISTS " . $wpdb->prefix.credofy_contact_form. " (

Instead, I would recommend moving your table creation logic to a plugin and having it run off an installation hook.

Below is an example:

<?php
/**
 * Plugin Name: WPSE 334694
 * Plugin URI:  https://wordpress.stackexchange.com/q/334694/13418
 * Description: Create database table on activation
 * Version:     1.0
 * Author:      You
 * Author URI:  https://example.com/
 * License:     GPL2
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

function wpse_334694_create_table() {
    global $wpdb;

    $charset_collate = $wpdb->get_charset_collate();

    $sql = "
        CREATE TABLE `{$wpdb->base_prefix}credofy_contact_form` (
        id mediumint(12) NOT NULL AUTO_INCREMENT,
        your_name VARCHAR(200) NOT NULL,
        your_email VARCHAR(200) NOT NULL,
        your_phone VARCHAR(200) NOT NULL,
        your_hobby VARCHAR(200) NOT NULL,
        PRIMARY KEY  (id) ) $charset_collate;
        ";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    dbDelta($sql);
}

register_activation_hook( __FILE__, 'wpse_334694_create_table' );

Simply copy this file into a directory name of your choosing in:

/plugins/{your_plugin_dir_name}/index.php

...then activate the plugin. Upon doing so the function wpse_334694_create_table will run once during activation and not anytime thereafter, unless you deactivate and reactivate the plugin.

Also modify your $wpdb->insert(...) call as per the following format:

$wpdb->insert( 
    'table', 
    array( 
        'column1' => 'value1', 
        'column2' => 123 
    ), 
    array( 
        '%s', // format of column 1
        '%d'  // format of column 2
    ) 
);

For further information concerning $wpdb->insert() please consult the document here which also provides other useful information on how to interact with your database via $wpdb global.

I would also recommend moving your insertion logic out into a plugin as well, firing off the back of a hook and adding nonce fields to your form for the purpose of validating the request, however that is a little beyond the scope of this question which primarily is focused on resolving your errors.

Recommended reading:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.