Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress$wpdb->;在自定义表中为每个帖子插入两行_Php_Mysql_Wordpress - Fatal编程技术网

Php Wordpress$wpdb->;在自定义表中为每个帖子插入两行

Php Wordpress$wpdb->;在自定义表中为每个帖子插入两行,php,mysql,wordpress,Php,Mysql,Wordpress,我使用自定义字段将数据插入到名为“wp_multi_links”的自定义表中,我使用$wpdb->insert,问题是当我发布ID=62的新帖子时,两个ID添加了62和63,具有相同的自定义字段值,我想问题在于帖子状态 wp_posts表的屏幕显示: 我的自定义字段代码: <?php /** * Adds a box to the main column on the Post and Page edit screens. */ function myplugin_add_meta

我使用自定义字段将数据插入到名为“wp_multi_links”的自定义表中,我使用$wpdb->insert,问题是当我发布ID=62的新帖子时,两个ID添加了62和63,具有相同的自定义字段值,我想问题在于帖子状态

wp_posts表的屏幕显示:

我的自定义字段代码:

<?php

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function myplugin_add_meta_box() {

    $screens = array( 'post');

    foreach ( $screens as $screen ) {

        add_meta_box(
            'myplugin_sectionid',
            __( 'My Post Section Title', 'myplugin_textdomain' ),
            'myplugin_meta_box_callback',
            $screen
        );
    }
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function myplugin_meta_box_callback( $post ) {

    // Add an nonce field so we can check for it later.
    wp_nonce_field( 'myplugin_meta_box', 'myplugin_meta_box_nonce' );

    /*
     * Use get_post_meta() to retrieve an existing value
     * from the database and use the value for the form.
     */
    $value = get_post_meta( $post->ID, '_my_meta_value_key', true );

    echo '<label for="myplugin_new_field">';
    _e( 'Description for this field', 'myplugin_textdomain' );
    echo '</label> ';
    echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function myplugin_save_meta_box_data( $post_id ) {
    global $wpdb; 
    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    // Check if our nonce is set.
    if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_meta_box' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }


    // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && 'page' == $_POST['post_type'] ) {

        if ( ! current_user_can( 'edit_page', $post_id ) ) {
            return;
        }

    } else {

        if ( ! current_user_can( 'edit_post', $post_id ) ) {
            return;
        }
    }

    /* OK, it's safe for us to save the data now. */

    // Make sure that it is set.
    if ( ! isset( $_POST['myplugin_new_field'] ) ) {
        return;
    }

    // Sanitize user input.
    $my_data = sanitize_text_field( $_POST['myplugin_new_field'] );
    // Update the meta field in the database.
    update_post_meta( $post_id, '_my_meta_value_key', $my_data );

    // isert data to DB

    $wpdb->insert( 
    'wp_multi_links', 
    array( 
        'post_id' => $post_id, 
        'link_url' => $my_data 
    ), 
    array( 
        '%d', 
        '%s'
    ) 
);

}

add_action( 'save_post', 'myplugin_save_meta_box_data' );

检查ID为63的行上的
post\u type
字段,它可能是一个
版本

代码的问题在于每次保存帖子时,都会在
wp\u multi\u links
表中插入另一个条目

您应该检查是否已经存在
$post\u id
的条目,如果已经存在,则更新它

更新

这应该起作用:

$data         = array( 'post_id'  => $post_id, 'link_url' => $my_data );
$data_format  = array( '%d', '%s' );
$where        = array( 'post_id' => $post_id );
$where_format = array( '%d' );

$updated = $wpdb->update( 'wp_multi_links', $data, $data_format, $where, $where_format );

if( ! $updated )
    $wpdb->insert( 'wp_multi_links', $data, $data_format );

谢谢@d79我想这就是诀窍,但我怎么能做到呢?