将空值插入wordpress数据库

时间:2015-05-08 22:21:05

标签: php mysql wordpress

我有这样的表格:

<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);
?>

如果我用CTRL+U显示html源代码,它看起来像这样:

<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>

点击保存按钮后,我想保存到我的wordpress数据库:

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

我在数据库中看到idtitle列中有一行增加content但空值。

我试图将字符串替换为$_POST个变量,它完美无缺!

我的代码是否有错误?

1 个答案:

答案 0 :(得分:3)

  

print_r( $_POST )。值是否到达脚本?

快点试试......

  

注意:我将%s添加到插入的末尾。

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

准备好的声明

查看使用预准备语句。 (无论如何你应该使用它们。)

Wordpress WPDB Prepared Statements

Wordpress Data Validation

准备好的声明示例......

$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 
) );