• 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’, ‘[email protected]’ 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’,’[email protected]’)’ 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’,’[email protected]’)’ at line 3

Viewing 1 replies (of 1 total)
  • Thread Starter msky

    (@msky)

    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.