在表单中插入不起作用的查询

时间:2014-12-04 作者:lost_in_magento

很多人都问过同样的问题,我读过这些东西,而我的不同,

我试图从表单中在数据库中插入一些值,但没有插入

我在数据库中有一个表,有6列,只想在4列中插入一些值

Table Name: wp_contactus

6列id,这是仅在4列中插入的代码

4列第一名第二名电子邮件查询

  <div class="wrap">
      <form action="" method="post">
          FirstName <input type="text" name="firstNametxt" value="" /><br/>
          LastName   <input type="text" name="lastNametxt" value="" /><br/>
          email <input type="text" name="email" value="" /><br/>
          Query <input type="text" name="query" value="" /><br/>
          <input name="Submit" type="submit" value="Submit">
      </form>
      <form method="post">
          <?php   
          global $wpdb;
                  $firstName = $_POST["firstNametxt"];
                  $lastName = $_POST["lastNametxt"];
                  $email = $_POST["email"];
                  $query = $_POST["query"];

                  echo $firstName;
          $contactus_table = $wpdb->prefix."contactus";

          $sql = "INSERT INTO $contactus_table (id, firstname, lastname, email,                                               

            query, reg_date) VALUES (\'2\', $firstName, $lastName, $email, $query,

            CURRENT_TIMESTAMP);";                  

            $wpdb->query($sql)) 

     ?>
     </form>
     </div>

     <?php
          }
          add_shortcode( \'CONUS\', \'contactus_shortcode\' );

     ?>

2 个回复
最合适的回答,由SO网友:lost_in_magento 整理而成

我发现了我在查询中的错误,谢谢大家的回复,我只是给出了一段代码

  <?php
  global $wpdb;
  $contactus_table = $wpdb->prefix."contactus";
        //error with the query 
        $sql = "INSERT INTO $contactus_table (firstname, lastname, email, query, reg_date) VALUES (\'$firstName\', \'$lastName\', \'$email\', \'$query\', CURRENT_TIMESTAMP)";

               if($wpdb->query($sql)) 
               {
               $BlogName = get_bloginfo();
               echo $BlogName;
               }
   ?>

SO网友:Mayeenul Islam

你必须自己做。这只是一个至少应该是什么的框架。搜索更多内容并使用格式。以下内容未选中,因此轮到您了…:)

<form action="" method="post"  enctype="multipart/form-data">
  <label for="first-name-text">First name: </label><input type="text" id="first-name-text" name="firstNametxt" value="" /><br/>
  <label for="last-name-text">Last name: </label><input type="text" id="last-name-text" name="lastNametxt" value="" /><br/>
  <label for="email">Email: </label><input type="text" id="email" name="email" value="" /><br/>
  <label for="query">Query: </label><input type="text" id="query" name="query" value="" /><br/>
  <input name="Submit" type="submit" value="Submit">
</form>
<?php
if( isset($_POST[\'submit\']) ) {

  //get posted value from the form
  $firstName = $_POST["firstNametxt"];
  $lastName = $_POST["lastNametxt"];
  $email = $_POST["email"];
  $query = $_POST["query"];

  //deal with database in WordPress way
  global $wpdb;
  $contactus_table = $wpdb->prefix."contactus";
  $wpdb->insert( 
    $contactus_table, 
    array( 
        \'firstname\' => $firstName, 
        \'lastname\'  => $lastName,
        \'email\'     => $email,
        \'query\'     => $query,
        \'reg_date\'  => current_time( \'mysql\' ) // http://codex.wordpress.org/Function_Reference/current_time
    ), 
    array( 
      \'%s\', //data type is string
      \'%s\',
      \'%s\',
      \'%s\',
      \'%s\' 
    ) 
  );
}
?>

结束