Title: Using Form to Insert into Custom Fields
Last modified: August 19, 2016

---

# Using Form to Insert into Custom Fields

 *  [msky](https://wordpress.org/support/users/msky/)
 * (@msky)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/using-form-to-insert-into-custom-fields/)
 * My Post has a simple 2 field Form (Name, Email) as Content.
 * And I have two Custom Fields set up: Name and Email
 * So, when the user fills in the form it should Insert their name and email into
   the Post’s Custom Fields. The Form Action is: insert.php, which is located at
   the root level of the site.
    insert.php:
 *     ```
       <?php
   
       ini_set('display_errors', 1);
       error_reporting(E_ALL);
   
       require_once('wp-load.php');
   
       if($_POST['submit']) //If submit is hit
       {
         // fetch user input from the form
   
         $name = mysql_real_escape_string($_POST['Name']);
         $email = mysql_real_escape_string($_POST['Email']);
   
        // execute the SQL query
         $result=mysql_query("INSERT INTO wp_postmeta
           (post_id,meta_key,meta_value) VALUES
           ($post->ID,'Name','$name'),
           ($post->ID,'Email','$email')");  
   
           print mysql_affected_rows();
           print mysql_error();
   
         // check for errors
       if(!$result) die('Error: '.mysql_error());
       else echo 'Inserted row with id='.mysql_insert_id();
   
       }
       ?>
       ```
   
 * When it runs, after submitting ‘Michael Sky’, ‘sky@rockisland.com’ I get:
 * -1You have an error in your SQL syntax; check the manual that corresponds to 
   your MySQL server version for the right syntax to use near ”Name’,’Michael Sky’),(,’
   Email’,’sky@island.com’)’ at line 3Error: You have an error in your SQL syntax;
   check the manual that corresponds to your MySQL server version for the right 
   syntax to use near ”Name’,’Michael Sky’), (,’Email’,’sky@island.com’)’ at line
   3

Viewing 1 replies (of 1 total)

 *  Thread Starter [msky](https://wordpress.org/support/users/msky/)
 * (@msky)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/using-form-to-insert-into-custom-fields/#post-926336)
 * Just added single quotes around $post->ID and now it inserts into the wp_postmeta
   table, but with a post_ID of 0

Viewing 1 replies (of 1 total)

The topic ‘Using Form to Insert into Custom Fields’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)
 * [form](https://wordpress.org/support/topic-tag/form/)

 * 1 reply
 * 1 participant
 * Last reply from: [msky](https://wordpress.org/support/users/msky/)
 * Last activity: [17 years, 6 months ago](https://wordpress.org/support/topic/using-form-to-insert-into-custom-fields/#post-926336)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
