将空值插入wordpress数据库

时间:2022-11-26 15:42:26

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:

如果我用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>

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

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

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.

我看到在数据库中有一行增加了id但在title和content列中有空值。

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

我试图把字符串而不是$ _POST变量,它完美地工作!

Is there an error with my code?

我的代码有错误吗?

1 个解决方案

#1


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

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

Try this really quick...

快点试试吧......

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

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

    $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 WPDB准备语句

Wordpress Data Validation

Wordpress数据验证

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

#1


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

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

Try this really quick...

快点试试吧......

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

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

    $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 WPDB准备语句

Wordpress Data Validation

Wordpress数据验证

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