2

I have my form like this:

<div id="titlewrap">
    <label class="" id="title-prompt-text" for="title">Enter title here</label>
    <input type="text" name="title" size="30" value="" id="title" autocomplete="off">
</div>
<br class="clear">
<?php
    $mva_content = '';
    $editor_id = 'editor';
    $settings = array('media_buttons' => false);
    wp_editor($mva_content, $editor_id, $settings);
?>

If I show html source code with CTRL+U it looks like this:

<form action="admin.php?page=list" method="post">
    <div id="poststuff">
        <div id="post-body">
            <div id="post-body-content">
                <div id="titlediv">
                    <div id="titlewrap">
                        <label class="" id="title-prompt-text" for="title">Enter title here</label>
                        <input type="text" name="title" size="30" value="" id="title" autocomplete="off">
                    </div>
                    <br class="clear">
                    <div id="wp-editor-wrap" class="wp-core-ui wp-editor-wrap html-active"><link rel='stylesheet' id='editor-buttons-css'  href='http://localhost/plug/wp-includes/css/editor.min.css?ver=4.0.5' type='text/css' media='all' />
<div id="wp-editor-editor-tools" class="wp-editor-tools hide-if-no-js"><div class="wp-editor-tabs"><a id="editor-html" class="wp-switch-editor switch-html" onclick="switchEditors.switchto(this);">Text</a>
<a id="editor-tmce" class="wp-switch-editor switch-tmce" onclick="switchEditors.switchto(this);">Visual</a>
</div>
</div>
<div id="wp-editor-editor-container" class="wp-editor-container"><textarea class="wp-editor-area" rows="20" autocomplete="off" cols="40" name="editor" id="editor"></textarea></div>
</div>

                    <p class="submit">
                        <input type="submit" name="create" id="create" class="button button-primary" value="Add New Video Ad">
                    </p>
                </div>
            </div>
        </div>
    </div>
</form>

I want to save to my wordpress database after click on a save button:

global $wpdb;
$title = $_POST["title"];
$content = $_POST["editor"];
$wpdb->insert("mytable", array(
   "title" => $title,
   "content" => $content,
)); 

I see that in the database there is a row with incremented id but empty value in title and content columns.

i have tried to put Strings instead of $_POST variables and it works perfectly!

Is there an error with my code?

2
  • PHP only runs on the server, while the form will be run in the user's browser. What does the HTML look like in the browser? ie, can you show the full <form> section as the browser would receive it (you can use "view page source" from the browser to see it without PHP). Commented May 8, 2015 at 22:51
  • @stvcisco: i have edited the post with the source code of the full html form. Actually, i dont think that is the root cause because if i use strings like "test title" instead of $_POST["title"] it works !! Commented May 8, 2015 at 23:07

1 Answer 1

3

print_r( $_POST ). Are the values getting to the script?

Try this really quick...

NOTE: I added the %s to the end of the insert.

    $wpdb->show_errors();
    $wpdb->insert("mytable", array(
       "title" => $title,
       "content" => $content,
    ), '%s');   
    $wpdb->print_error();

ON PREPARED STATEMENTS


Look into using prepared statements. (You should be using them anyway.)

Wordpress WPDB Prepared Statements

Wordpress Data Validation

Prepared Statement Example...

$metakey    = "Harriet's Adages";
$metavalue  = "WordPress' database interface is like Sunday Morning: Easy.";

$wpdb->query( $wpdb->prepare( 
    "
        INSERT INTO $wpdb->postmeta
        ( post_id, meta_key, meta_value )
        VALUES ( %d, %s, %s )
    ", 
        10, 
    $metakey, 
    $metavalue 
) );
Sign up to request clarification or add additional context in comments.

1 Comment

I retried to get the coma off, the issue is always here. But the %s added to the end of the insert works perfectly ! thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.