0

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>
  • 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 Jan 23 at 11:04
1

You'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.

| improve this answer | |
  • Hi, i have edited the script and hardcoded the table name and also corrected the name attribute...You can check the code in the file above..however, the data is still not getting saved....is there something else that needs to be corrected? – Bot123 Jan 23 at 7:19
  • $_POST is case-sensitive, Your input name and check don't match, like they do in my answer. Use either submit or SUBMIT. They need to be the same, – Jacob Peattie Jan 23 at 8:50
0

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.

| improve this answer | |

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.