0

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

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

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.