admin管理员组

文章数量:1022874

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`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(); ?>
Share Improve this question edited Apr 18, 2019 at 13:45 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Apr 18, 2019 at 11:25 gaurav mishragaurav mishra 94 bronze badges 4
  • can you please set define('WP_DEBUG', true) in wp-config.php, also please add global $wpdb; above while create new table – Evince Development Commented Apr 18, 2019 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 Commented Apr 18, 2019 at 12:46
  • @mrben522 Please let me know how to do that? :-( – gaurav mishra Commented Apr 19, 2019 at 4:39
  • this post explains sanitization and validation really well – mrben522 Commented Apr 20, 2019 at 16:21
Add a comment  | 

1 Answer 1

Reset to default 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/q/334694/13418
 * Description: Create database table on activation
 * Version:     1.0
 * Author:      You
 * Author URI:  https://example/
 * License:     GPL2
 * License URI: https://www.gnu/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:

  • https://codex.wordpress/Class_Reference/wpdb
  • https://developer.wordpress/plugins/security/nonces/
  • https://developer.wordpress/themes/theme-security/using-nonces/
  • https://codex.wordpress/WordPress_Nonces
  • https://developer.wordpress/plugins/intro/

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`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(); ?>
Share Improve this question edited Apr 18, 2019 at 13:45 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Apr 18, 2019 at 11:25 gaurav mishragaurav mishra 94 bronze badges 4
  • can you please set define('WP_DEBUG', true) in wp-config.php, also please add global $wpdb; above while create new table – Evince Development Commented Apr 18, 2019 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 Commented Apr 18, 2019 at 12:46
  • @mrben522 Please let me know how to do that? :-( – gaurav mishra Commented Apr 19, 2019 at 4:39
  • this post explains sanitization and validation really well – mrben522 Commented Apr 20, 2019 at 16:21
Add a comment  | 

1 Answer 1

Reset to default 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/q/334694/13418
 * Description: Create database table on activation
 * Version:     1.0
 * Author:      You
 * Author URI:  https://example/
 * License:     GPL2
 * License URI: https://www.gnu/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:

  • https://codex.wordpress/Class_Reference/wpdb
  • https://developer.wordpress/plugins/security/nonces/
  • https://developer.wordpress/themes/theme-security/using-nonces/
  • https://codex.wordpress/WordPress_Nonces
  • https://developer.wordpress/plugins/intro/

本文标签: mysqlWordPress insert query is not workingShowing no Error