database - Insert Query not working in the form

Many people has asked the same question, I have read those things and mine is different, I am trying to insert some valu

Many people has asked the same question, I have read those things and mine is different,

I am trying to insert some values inside the database from the form but it's not getting inserted

I have a table in the database having 6 columns wanted to insert some values inside only of 4 columns

Table Name: wp_contactus

6 Columns

  • id
  • firstname
  • lastname
  • email
  • query
  • reg_date

This is the code for inserting only in the 4 columns

4 Columns

  • firstname
  • secondname
  • email
  • query

      <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' );
    
         ?>
    

Many people has asked the same question, I have read those things and mine is different,

I am trying to insert some values inside the database from the form but it's not getting inserted

I have a table in the database having 6 columns wanted to insert some values inside only of 4 columns

Table Name: wp_contactus

6 Columns

  • id
  • firstname
  • lastname
  • email
  • query
  • reg_date

This is the code for inserting only in the 4 columns

4 Columns

  • firstname
  • secondname
  • email
  • query

      <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' );
    
         ?>
    
Share Improve this question edited Dec 4, 2014 at 7:38 Pieter Goosen 55.4k23 gold badges115 silver badges210 bronze badges asked Dec 4, 2014 at 5:39 lost_in_magentolost_in_magento 1651 gold badge3 silver badges11 bronze badges 10
  • Why are you sending ID? ID should be Auto Incremented in db. – Mayeenul Islam Commented Dec 4, 2014 at 5:59
  • yea I don't know, am sorry how should then the code be? can u reassign it accordingly.. – lost_in_magento Commented Dec 4, 2014 at 6:00
  • codex.wordpress/Class_Reference/wpdb#INSERT_rows – RRikesh Commented Dec 4, 2014 at 6:07
  • How to add with id and time stamp in the row can u tell me? – lost_in_magento Commented Dec 4, 2014 at 6:16
  • @rajesh delete the first item in VALUES (i.e. 2) and the id, after $contactus_table – Mayeenul Islam Commented Dec 4, 2014 at 6:43
 |  Show 5 more comments

2 Answers 2

Reset to default 3

I figured out my error in the query, Thank you all guys for responding, I just gave the piece of code

  <?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;
               }
   ?>

You have to do it yourself. It's just a frame of what it should be at least. Search more and use the format. The following is not checked, so it's your turn... :)

<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/Function_Reference/current_time
    ), 
    array( 
      '%s', //data type is string
      '%s',
      '%s',
      '%s',
      '%s' 
    ) 
  );
}
?>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742301309a4418082.html

相关推荐

  • database - Insert Query not working in the form

    Many people has asked the same question, I have read those things and mine is different, I am trying to insert some valu

    19小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信