WordPress.org

Ready to get started?Download WordPress

Forums

Using Form to Insert into Custom Fields (2 posts)

  1. msky
    Member
    Posted 5 years ago #

    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

  2. msky
    Member
    Posted 5 years ago #

    Just added single quotes around $post->ID and now it inserts into the wp_postmeta table, but with a post_ID of 0

Topic Closed

This topic has been closed to new replies.

About this Topic