title: Form data not inserting in Wordpress database even though hard coded data gets inserted
body: <p>I'm trying to build a form in Wordpress and inserting data from the form into the database. When I use hard coded data it works fine but if I change that to use variables from the form it is not inserting data at all.  Any help would be much appreciated. </p>
  
  <pre><code>    &lt;?php
  /*
  Template Name: My Form Page
  */
  ?&gt;
  &lt;?php
  //user posted variables
  $name = $_POST['message_name'];
  $email = $_POST['message_email'];
  $message = $_POST['message_text'];
  $human = $_POST['message_human'];
  $selectedFood  = 'None';
  if(isset($_POST['selectedFood']) &amp;&amp; is_array($_POST['selectedFood']) &amp;&amp; count($_POST['selectedFood']) &gt; 0){
      $selectedFood = implode(', ', $_POST['selectedFood']);
  }
   //establish connection
  if(empty($errorMessage)) 
          {
              $db = mysql_connect("localhost","xxx","xxx");
              if(!$db) die("Error connecting to MySQL database.");
              mysql_select_db("xxx" ,$db);
              $sql = "INSERT INTO wp_picnic_guest (name, yourname, moviename) VALUES ($name, $email, $message)"; 
              mysql_query($sql);
  
  
          }
  function PrepSQL($value)
      {
          // Stripslashes
          if(get_magic_quotes_gpc()) 
          {
              $value = stripslashes($value);
          }
  
          // Quote
          $value = "'" . mysql_real_escape_string($value) . "'";
  
          return($value);
      }
  
  
    //response generation function
    $response = "";
     //function to generate response
    function my_contact_form_generate_response($type, $message){
       global $response;
  
      if($type == "success") $response = "&lt;div class='success'&gt;{$message}&lt;/div&gt;";
  
      else $response = "&lt;div class='error'&gt;{$message}&lt;/div&gt;";
  
    }
      //response messages
  $not_human       = "Human verification incorrect.";
  $missing_content = "Please supply all information.";
  $email_invalid   = "Email Address Invalid.";
  $message_unsent  = "Message was not sent. Try Again.";
  $message_sent    = "Thanks! Your message has been sent.";
  
  
  
  //php mailer variables
  $to = get_option('admin_email');
  $subject = "Picnic Food from ".get_bloginfo('name');
  $body =  "From: $name\n E-Mail: $email\n Message: $message\n Selected Food:\n $selectedFood";
  $headers = 'From: '. $email . "\r\n" .
    'Reply-To: ' . $email . "\r\n";
  if(!$human == 0){
    if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
    else {
  
      //validate email
  if(!filter_var($email, FILTER_VALIDATE_EMAIL))
    my_contact_form_generate_response("error", $email_invalid);
  else //email is valid
  {
    //validate presence of name and message
  if(empty($name) || empty($message)){
    my_contact_form_generate_response("error", $missing_content);
  }
  else //ready to go!
  {
    $sent = wp_mail($to, $subject, $body, strip_tags($message), $headers);
  if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
  
  else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
  }
  
  }
  
    }
  }
  else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
  
  ?&gt;
  &lt;?php 
  function clearform()
  {
      document.getElementById("name").reset(); //don't forget to set the textbox ID
      document.getElementById("email").reset(); //don't forget to set the textbox ID
      document.getElementById("message").reset(); //don't forget to set the textbox ID
  }
  ?&gt;
  &lt;?php get_header(); ?&gt;
  
    &lt;div id="primary" class="site-content"&gt;
      &lt;div id="content" role="main"&gt;
  
        &lt;?php while ( have_posts() ) : the_post(); ?&gt;
  
            &lt;article id="post-&lt;?php the_ID(); ?&gt;" &lt;?php post_class(); ?&gt;&gt;
  
              &lt;header class="entry-header"&gt;
                &lt;h1 class="entry-title"&gt;&lt;?php the_title(); ?&gt;&lt;/h1&gt;
              &lt;/header&gt;
  
              &lt;div class="entry-content"&gt;
                &lt;?php the_content(); ?&gt;
  
               &lt;style type="text/css"&gt;
    .error{
      padding: 5px 9px;
      border: 1px solid red;
      color: red;
      border-radius: 3px;
    }
  
    .success{
      padding: 5px 9px;
      border: 1px solid green;
      color: green;
      border-radius: 3px;
    }
  
    form span{
      color: red;
    }
  &lt;/style&gt;
  
  &lt;div id="respond"&gt;
    &lt;?php echo $response; ?&gt;
    &lt;form action="&lt;?php the_permalink(); ?&gt;" method="post"&gt;
      &lt;p&gt;&lt;label for="name"&gt;Name: &lt;span&gt;*&lt;/span&gt; &lt;br&gt;&lt;input id= "name" type="text" name="message_name" value="&lt;?php echo esc_attr($_POST['message_name']); ?&gt;"&gt;&lt;/label&gt;&lt;/p&gt;
      &lt;p&gt;&lt;label for="message_email"&gt;Email: &lt;span&gt;*&lt;/span&gt; &lt;br&gt;&lt;input id="email" type="text" name="message_email" value="&lt;?php echo esc_attr($_POST['message_email']); ?&gt;"&gt;&lt;/label&gt;&lt;/p&gt;
      &lt;p&gt;
          &lt;input type="checkbox" name="selectedFood[]" value="Tea"&gt;&lt;label for="type4"&gt;Tea&lt;/label&gt;&lt;?php
  if(isset($_POST['selectedFood']) &amp;&amp; is_array($_POST['selectedFood']) &amp;&amp; count($_POST['selectedFood']) &gt; 0){ 
    $selectedFood = implode(', ', $_POST['selectedFood']);
      echo "   Chosen"; }
  else { echo ""; }
  ?&gt;  &lt;/br&gt;
     &lt;input type="checkbox" name="selectedFood[]" value="Bread"&gt;&lt;label for="type1"&gt;Bread&lt;/label&gt;&lt;/br&gt;
     &lt;input type="checkbox" name="selectedFood[]" value="Cheese"&gt;&lt;label for="type2"&gt;Cheese&lt;/label&gt;&lt;/br&gt;
     &lt;input type="checkbox" name="selectedFood[]" value="Cake"&gt;&lt;label for="type3"&gt;Cake&lt;/label&gt; &lt;/br&gt;
  
      &lt;p&gt;&lt;label for="message_text"&gt;Message: &lt;span&gt;*&lt;/span&gt; &lt;br&gt;&lt;textarea id="message" type="text" name="message_text"&gt;&lt;?php echo esc_textarea($_POST['message_text']); ?&gt;&lt;/textarea&gt;&lt;/label&gt;&lt;/p&gt;
      &lt;p&gt;&lt;label for="message_human"&gt;Human Verification: &lt;span&gt;*&lt;/span&gt; &lt;br&gt;&lt;input type="text" style="width: 60px;" name="message_human"&gt; + 3 = 5&lt;/label&gt;&lt;/p&gt;
      &lt;input type="hidden" name="submitted" value="1"&gt;
      &lt;p&gt;&lt;input type="submit" /&gt;
  
  &lt;/p&gt;
    &lt;/form&gt;
  
  &lt;/div&gt;
  
  
              &lt;/div&gt;&lt;!-- .entry-content --&gt;
  
            &lt;/article&gt;&lt;!-- #post --&gt;
  
        &lt;?php endwhile; // end of the loop. ?&gt;
  
      &lt;/div&gt;&lt;!-- #content --&gt;
    &lt;/div&gt;&lt;!-- #primary --&gt;
  
  &lt;?php get_sidebar(); ?&gt;
  &lt;?php get_footer(); ?&gt;
  </code></pre>