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
|
1 Answer
Reset to default 1There 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)
inwp-config.php
, also please addglobal $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
1 Answer
Reset to default 1There 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
版权声明:本文标题:mysql - WordPress insert query is not working : Showing no Error 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745575345a2156984.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
define('WP_DEBUG', true)
inwp-config.php
, also please addglobal $wpdb;
above while create new table – Evince Development Commented Apr 18, 2019 at 11:50