admin管理员组文章数量:1392086
I have created a PHP program that provides customised output based upon user input entered in HTML contact form. The HTML form & PHP output is working fine. However, when I am trying to save the data in WP database, I am not able to see any response. I have created 'wp_mealplanner' table in the database and when i am trying to manually save the data through PHPMYADMIN, it is working fine. Please let me know where am I going wrong. I am not getting any error message.
Please find the code below:
<?php
/* Template Name: CustomPageT1 */
$region = $_POST['Region'];
$name = $_POST['user_name'];
$email = $_POST['user_email'];
$phone = $_POST['user_phone'];
$Click_here_to_start_your_download = '
content/uploads/2019/09/Blood-Glucose-Chart-A4-1.pdf';
switch ($region )
{
case 'North':
echo "<a href='$Click_here_to_start_your_download'>Click here to start your
download</a>";
die;
case 'South':
echo "<a href='$Click_here_to_start_your_download'>Click here to start your
download</a>";
die;
case 'East':
echo "<a href='$Click_here_to_start_your_download'>Click here to start your
download</a>";
die;
case 'West':
echo "<a href='$Click_here_to_start_your_download'>Click here to start your
download</a>";
die;
}
?>
<?php
if ( isset( $_POST['SUBMIT'] ) )
{
global $wpdb;
$table ='wp_mealplanner';
$data = array('name' => $_POST['user_name'],'email' =>
$_POST['user_email'],'phone' =>
$_POST['user_phone']);
$format = array('%s','%s','%d');
$wpdb->insert( $table, $data, $format );
}
?>
<html>
<head>
<title> Meal Planner </title>
</head>
<body>
<form method='POST'>
<p>Name</p> <input type='text' name='user_name'>
<p>Email</p> <input type='text' name='user_email'>
<p>Phone</p> <input type='text' name='user_phone'>
<p>Dropdown Box</p>
<select name='Region' size='1'>
<option value='North'>North
<option value='South'>South
<option value='East'>East
<option value='West'>West
</select>
<br />
<input type='submit' name ='submit' value='SUBMIT'><input
type='reset'
value='CLEAR'>
</form>
</body>
</html>
I have created a PHP program that provides customised output based upon user input entered in HTML contact form. The HTML form & PHP output is working fine. However, when I am trying to save the data in WP database, I am not able to see any response. I have created 'wp_mealplanner' table in the database and when i am trying to manually save the data through PHPMYADMIN, it is working fine. Please let me know where am I going wrong. I am not getting any error message.
Please find the code below:
<?php
/* Template Name: CustomPageT1 */
$region = $_POST['Region'];
$name = $_POST['user_name'];
$email = $_POST['user_email'];
$phone = $_POST['user_phone'];
$Click_here_to_start_your_download = 'https://mydailysugar.info/wp-
content/uploads/2019/09/Blood-Glucose-Chart-A4-1.pdf';
switch ($region )
{
case 'North':
echo "<a href='$Click_here_to_start_your_download'>Click here to start your
download</a>";
die;
case 'South':
echo "<a href='$Click_here_to_start_your_download'>Click here to start your
download</a>";
die;
case 'East':
echo "<a href='$Click_here_to_start_your_download'>Click here to start your
download</a>";
die;
case 'West':
echo "<a href='$Click_here_to_start_your_download'>Click here to start your
download</a>";
die;
}
?>
<?php
if ( isset( $_POST['SUBMIT'] ) )
{
global $wpdb;
$table ='wp_mealplanner';
$data = array('name' => $_POST['user_name'],'email' =>
$_POST['user_email'],'phone' =>
$_POST['user_phone']);
$format = array('%s','%s','%d');
$wpdb->insert( $table, $data, $format );
}
?>
<html>
<head>
<title> Meal Planner </title>
</head>
<body>
<form method='POST'>
<p>Name</p> <input type='text' name='user_name'>
<p>Email</p> <input type='text' name='user_email'>
<p>Phone</p> <input type='text' name='user_phone'>
<p>Dropdown Box</p>
<select name='Region' size='1'>
<option value='North'>North
<option value='South'>South
<option value='East'>East
<option value='West'>West
</select>
<br />
<input type='submit' name ='submit' value='SUBMIT'><input
type='reset'
value='CLEAR'>
</form>
</body>
</html>
Share
Improve this question
edited Jan 23, 2020 at 7:17
Bot123
asked Jan 23, 2020 at 6:37
Bot123Bot123
11 silver badge2 bronze badges
1
- Just a note: You should validate and sanitize your user provided input data for security reasons. Else you will end up with lots of (maybe successful) hacks. – kaiser Commented Jan 23, 2020 at 11:04
2 Answers
Reset to default 1You're checking isset( $_POST['SUBMIT'] )
, but there's no field with that name. If you mean to be checking the submit button, you need to give it a name
attribute with the right value:
<input type='submit' name='SUBMIT' value='SUBMIT'>
Now isset( $_POST['SUBMIT']
will be true
, and your code will run.
Also, you don't seem to be using $wpdb->prefix
correctly. $wpdb->prefix
is based on the value entered by the user during installtion, and is used to prefix the database tables automatically created by WordPress, and can be used by plugins to name their tables as well. This prefix is usually wp_
, so if your table is named wp_mealplanner
, then the table name should be set like this:
$table = $wpdb->prefix . 'mealplanner';
However, if you manually created your table in PHPMyAdmin, then your table's name isn't actually based off $wpdb->prefix
, so you should just hard-code the name you gave it:
$table = 'wp_mealplanner';
You would only use $wpdb->prefix
to ensure you were querying a table that had been created using $wpdb->prefix
in the first place, such as during plugin activation.
Was able to resolve this. I had put the 'DIE' command before calling the database function. I shifted the 'DIE' command after the database command and was able to resolve the issue.
本文标签: phpSaving data from custom form in wordpress database
版权声明:本文标题:php - Saving data from custom form in wordpress database 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744779716a2624633.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论