【问题标题】:Form data not inserting in Wordpress database even though hard coded data gets inserted即使插入了硬编码数据,表单数据也没有插入到 Wordpress 数据库中
【发布时间】:2015-08-25 14:04:46
【问题描述】:

我正在尝试在 Wordpress 中构建一个表单并将表单中的数据插入到数据库中。当我使用硬编码数据时,它可以正常工作,但是如果我将其更改为使用表单中的变量,则根本不会插入数据。任何帮助将非常感激。

    <?php
/*
Template Name: My Form Page
*/
?>
<?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']) && is_array($_POST['selectedFood']) && count($_POST['selectedFood']) > 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 = "<div class='success'>{$message}</div>";

    else $response = "<div class='error'>{$message}</div>";

  }
    //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);

?>
<?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
}
?>
<?php get_header(); ?>

  <div id="primary" class="site-content">
    <div id="content" role="main">

      <?php while ( have_posts() ) : the_post(); ?>

          <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

            <header class="entry-header">
              <h1 class="entry-title"><?php the_title(); ?></h1>
            </header>

            <div class="entry-content">
              <?php the_content(); ?>

             <style type="text/css">
  .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;
  }
</style>

<div id="respond">
  <?php echo $response; ?>
  <form action="<?php the_permalink(); ?>" method="post">
    <p><label for="name">Name: <span>*</span> <br><input id= "name" type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"></label></p>
    <p><label for="message_email">Email: <span>*</span> <br><input id="email" type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label></p>
    <p>
        <input type="checkbox" name="selectedFood[]" value="Tea"><label for="type4">Tea</label><?php
if(isset($_POST['selectedFood']) && is_array($_POST['selectedFood']) && count($_POST['selectedFood']) > 0){ 
  $selectedFood = implode(', ', $_POST['selectedFood']);
    echo "   Chosen"; }
else { echo ""; }
?>  </br>
   <input type="checkbox" name="selectedFood[]" value="Bread"><label for="type1">Bread</label></br>
   <input type="checkbox" name="selectedFood[]" value="Cheese"><label for="type2">Cheese</label></br>
   <input type="checkbox" name="selectedFood[]" value="Cake"><label for="type3">Cake</label> </br>

    <p><label for="message_text">Message: <span>*</span> <br><textarea id="message" type="text" name="message_text"><?php echo esc_textarea($_POST['message_text']); ?></textarea></label></p>
    <p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>
    <input type="hidden" name="submitted" value="1">
    <p><input type="submit" />

</p>
  </form>

</div>


            </div><!-- .entry-content -->

          </article><!-- #post -->

      <?php endwhile; // end of the loop. ?>

    </div><!-- #content -->
  </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

【问题讨论】:

    标签: database wordpress forms insert


    【解决方案1】:

    尝试使用 wordpress 默认插入功能来插入值。

        <?php
    /*
    Template Name: My Form Page
    */
    ?>
    <?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']) && is_array($_POST['selectedFood']) && count($_POST['selectedFood']) > 0){
        $selectedFood = implode(', ', $_POST['selectedFood']);
    }
     //establish connection
    if(empty($errorMessage)) 
            {
                global $wpdb;
               $wpdb->insert('wp_picnic_guest',
                   array('name'=>$name,
                        'yourname'=>$email,
                         'moviename'=>$message
                            )   );
    
    
            }
    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 = "<div class='success'>{$message}</div>";
    
        else $response = "<div class='error'>{$message}</div>";
    
      }
        //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);
    
    ?>
    <?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
    }
    ?>
    <?php get_header(); ?>
    
      <div id="primary" class="site-content">
        <div id="content" role="main">
    
          <?php while ( have_posts() ) : the_post(); ?>
    
              <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    
                <header class="entry-header">
                  <h1 class="entry-title"><?php the_title(); ?></h1>
                </header>
    
                <div class="entry-content">
                  <?php the_content(); ?>
    
                 <style type="text/css">
      .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;
      }
    </style>
    
    <div id="respond">
      <?php echo $response; ?>
      <form action="<?php the_permalink(); ?>" method="post">
        <p><label for="name">Name: <span>*</span> <br><input id= "name" type="text" name="message_name" value="<?php echo esc_attr($_POST['message_name']); ?>"></label></p>
        <p><label for="message_email">Email: <span>*</span> <br><input id="email" type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>"></label></p>
        <p>
            <input type="checkbox" name="selectedFood[]" value="Tea"><label for="type4">Tea</label><?php
    if(isset($_POST['selectedFood']) && is_array($_POST['selectedFood']) && count($_POST['selectedFood']) > 0){ 
      $selectedFood = implode(', ', $_POST['selectedFood']);
        echo "   Chosen"; }
    else { echo ""; }
    ?>  </br>
       <input type="checkbox" name="selectedFood[]" value="Bread"><label for="type1">Bread</label></br>
       <input type="checkbox" name="selectedFood[]" value="Cheese"><label for="type2">Cheese</label></br>
       <input type="checkbox" name="selectedFood[]" value="Cake"><label for="type3">Cake</label> </br>
    
        <p><label for="message_text">Message: <span>*</span> <br><textarea id="message" type="text" name="message_text"><?php echo esc_textarea($_POST['message_text']); ?></textarea></label></p>
        <p><label for="message_human">Human Verification: <span>*</span> <br><input type="text" style="width: 60px;" name="message_human"> + 3 = 5</label></p>
        <input type="hidden" name="submitted" value="1">
        <p><input type="submit" />
    
    </p>
      </form>
    
    </div>
    
    
                </div><!-- .entry-content -->
    
              </article><!-- #post -->
    
          <?php endwhile; // end of the loop. ?>
    
        </div><!-- #content -->
      </div><!-- #primary -->
    
    <?php get_sidebar(); ?>
    <?php get_footer(); ?>
    

    【讨论】:

    • 非常感谢!它工作 100% 真的很感激!
    【解决方案2】:

    首先不需要连接数据库。它已经连接了。 所以删除连接数据库的代码行。

    试试

    $sql = "INSERT INTO wp_picnic_guest (name, yourname, moviename) VALUES ('$name', '$email', '$message')"; 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-22
      • 2021-01-24
      • 2012-06-27
      • 2017-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多