douzuo0711 2017-10-30 05:17
浏览 59
已采纳

更新wordpress总是0

I made plugin for CRUD database wordpress, but when I click submit link_id always return 0

error information

string(992) "UPDATE `wp_search` SET `id` = '1', 
          `name` = 'Ini Vie', 
          `location` = 'Seminyak', 
          `price` = '160', 
          `room` = '1', 
          `des_id` = 'Ini Vie Villa is a stylish lifestyle boutique romantic villa which located in Legian, offers One Bedroom and Two Bedrooms Villa with private pool & Jacuzzi with perfection traditional personalize Balinese hospitality service, a quiet yet stylish area, a more sophisticated atmosphere than the hustle and bustle of nearby Kuta.', 
          `des_en` = 'Ini Vie Villa is a stylish lifestyle boutique romantic villa which located in Legian, offers One Bedroom and Two Bedrooms Villa with private pool & Jacuzzi with perfection traditional personalize Balinese hospitality service, a quiet yet stylish area, a more sophisticated atmosphere than the hustle and bustle of nearby Kuta.', 
          `link_id` = 0, 
          `link_en` = 'rumahvillabali.com/seminyak/ini-vie-villa',
          `link_img` = 'rumahvillabali.com/wp-content/uploads/2015/11/Ini-Vie-Villa-One-Bedroom-5.jpg',
          `type` = 'Villa' 
WHERE `id` = '1'"

as you seen link_id always 0, but actually it is not 0

my script wordpress

    function custom_table_example_persons_form_page_handler()
{
    global $wpdb;
    $table_name = $wpdb->prefix . 'search'; // do not forget about tables prefix

    $message = '';
    $notice = '';

    // this is default $item which will be used for new records
    $default = array(
        'id' => 0,
        'name' => '',
        'location' => '',
        'price' => '',
        'room' => '',
        'des_id' => '',
        'des_en' => '',
        'link_id' => '',
        'link_en' => '',
        'link_img' => '',
        'type' => '',
    );

    // here we are verifying does this request is post back and have correct nonce
    if (wp_verify_nonce($_REQUEST['nonce'], basename(__FILE__))) {
        // combine our default item with request params
        $item = shortcode_atts($default, $_REQUEST);
        // validate data, and if all ok save item to database
        // if id is zero insert otherwise update
        $item_valid = custom_table_example_validate_person($item);
        if ($item_valid === true) {
            if ($item['id'] == 0) {
                $result = $wpdb->insert($table_name, $item);
                $item['id'] = $wpdb->insert_id;
                if ($result) {
                    $message = __('Item was successfully saved', 'custom_table_example');
                } else {
                    $notice = __('There was an error while saving item', 'custom_table_example');
                }
            } else {
                $result = $wpdb->update($table_name, $item, array('id' => $item['id']));
                if ($result) {
                    $message = __('Item was successfully updated', 'custom_table_example');
                } else {
                    $notice = __('There was an error while updating item', 'custom_table_example');
                        exit( var_dump( $wpdb->last_query ) ); 
                }
            }
        } else {
            // if $item_valid not true it contains error message(s)
            $notice = $item_valid;
        }
    }
    else {
        // if this is not post back we load item to edit or give new one to create
        $item = $default;
        if (isset($_REQUEST['id'])) {
            $item = $wpdb->get_row($wpdb->prepare("SELECT * FROM $table_name WHERE id = %d", $_REQUEST['id']), ARRAY_A);
            if (!$item) {
                $item = $default;
                $notice = __('Item not found', 'custom_table_example');
            }
        }
    }

    // here we adding our custom meta box
    add_meta_box('persons_form_meta_box', 'Person data', 'custom_table_example_persons_form_meta_box_handler', 'person', 'normal', 'default');

    ?>
<div class="wrap">
    <div class="icon32 icon32-posts-post" id="icon-edit"><br></div>
    <h2><?php _e('Person', 'custom_table_example')?> <a class="add-new-h2"
                                href="<?php echo get_admin_url(get_current_blog_id(), 'admin.php?page=custom booking');?>"><?php _e('back to list', 'custom_table_example')?></a>
    </h2>

    <?php if (!empty($notice)): ?>
    <div id="notice" class="error"><p><?php echo $notice ?></p></div>
    <?php endif;?>
    <?php if (!empty($message)): ?>
    <div id="message" class="updated"><p><?php echo $message ?></p></div>
    <?php endif;?>

    <form id="form" method="POST" action="">
        <input type="hidden" name="nonce" value="<?php echo wp_create_nonce(basename(__FILE__))?>"/>
        <?php /* NOTICE: here we storing id to determine will be item added or updated */ ?>
        <input type="hidden" name="id" value="<?php echo $item['id'] ?>"/>

        <div class="metabox-holder" id="poststuff">
            <div id="post-body">
                <div id="post-body-content">
                    <?php /* And here we call our custom meta box */ ?>
                    <?php do_meta_boxes('person', 'normal', $item); ?>
                    <input type="submit" value="<?php _e('Save', 'custom_table_example')?>" id="submit" class="button-primary" name="submit">
                </div>
            </div>
        </div>
    </form>
</div>
<?php
}

/**
 * This function renders our custom meta box
 * $item is row
 *
 * @param $item
 */
function custom_table_example_persons_form_meta_box_handler($item)
{
    ?>

<table cellspacing="2" cellpadding="5" style="width: 100%;" class="form-table">
    <tbody>
    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="date"><?php _e('name', 'custom_table_example')?></label>
        </th>
        <td>
            <input id="name" name="name" type="text" style="width: 95%" value="<?php echo esc_attr($item['name'])?>"
                   size="50" class="code" placeholder="<?php _e('name', 'custom_table_example')?>" required>
        </td>
    </tr>
    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="boat"><?php _e('location', 'custom_table_example')?></label>
        </th>
        <td>
            <input id="location" name="location" type="text" style="width: 95%" value="<?php echo esc_attr($item['location'])?>"
                   size="50" class="code" placeholder="<?php _e('location', 'custom_table_example')?>" required>
        </td>
    </tr>
    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="price"><?php _e('price', 'custom_table_example')?></label>
        </th>
        <td>
            <input id="price" name="price" type="text" style="width: 95%" value="<?php echo esc_attr($item['price'])?>"
                   size="50" class="code" placeholder="<?php _e('price', 'custom_table_example')?>" required>
        </td>
    </tr>
    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="room"><?php _e('room', 'custom_table_example')?></label>
        </th>
        <td>
            <input id="room" name="room" type="text" style="width: 95%" value="<?php echo esc_attr($item['room'])?>"
                   size="50" class="code" placeholder="<?php _e('room', 'custom_table_example')?>" required>
        </td>
    </tr>
    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="des_id"><?php _e('des_id', 'custom_table_example')?></label>
        </th>
        <td>
            <textarea id="des_id" name="des_id" type="text" style="width: 95%"
                   size="50" rows="10" class="code" required><?php echo esc_attr($item['des_id'])?></textarea>
        </td>
    </tr>
    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="des_en"><?php _e('des_en', 'custom_table_example')?></label>
        </th>
        <td>
            <textarea id="des_en" name="des_en" type="text" style="width: 95%"
                   size="50" rows="10" class="code" required><?php echo esc_attr($item['des_en'])?></textarea>
        </td>
    </tr>
    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="link_id"><?php _e('link_id', 'custom_table_example')?></label>
        </th>
        <td>
            <input id="link_id" name="link_id" type="text" style="width: 95%" value="<?php echo esc_html($item['link_id'])?>"
                   size="50" class="code" placeholder="<?php _e('link_id', 'custom_table_example')?>" required>
        </td>
    </tr>
    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="link_en"><?php _e('link_en', 'custom_table_example')?></label>
        </th>
        <td>
            <input id="link_en" name="link_en" type="text" style="width: 95%" value="<?php echo esc_html($item['link_en'])?>"
                   size="50" class="code" placeholder="<?php _e('link_en', 'custom_table_example')?>" required>
        </td>
    </tr>

    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="link_img"><?php _e('link_img', 'custom_table_example')?></label>
        </th>
        <td>
            <input id="link_img" name="link_img" type="text" style="width: 95%" value="<?php echo esc_attr($item['link_img'])?>"
                   size="50" class="code" placeholder="<?php _e('link_img', 'custom_table_example')?>" required>
        </td>
    </tr>
    <tr class="form-field">
        <th valign="top" scope="row">
            <label for="type"><?php _e('type', 'custom_table_example')?></label>
        </th>
        <td>
            <input id="type" name="type" type="text" style="width: 95%" value="<?php echo esc_attr($item['type'])?>"
                   size="50" class="code" placeholder="<?php _e('type', 'custom_table_example')?>" required>
        </td>
    </tr>
    </tbody>
</table>
<?php
}

/**
 * Simple function that validates data and retrieve bool on success
 * and error message(s) on error
 *
 * @param $item
 * @return bool|string
 */
function custom_table_example_validate_person($item)
{
    $messages = array();

    if (empty($item['name'])) $messages[] = __('name is required', 'custom_table_example');
    if (empty($item['location'])) $messages[] = __('location is required', 'custom_table_example');
    if (empty($item['price'])) $messages[] = __('price is required', 'custom_table_example');
    if (empty($item['room'])) $messages[] = __('room is required', 'custom_table_example');
    if (empty($item['des_id'])) $messages[] = __('des_id is required', 'custom_table_example');
    if (empty($item['des_en'])) $messages[] = __('des_en is required', 'custom_table_example');
    if (empty($item['link_id'])) $messages[] = __('link_id is required', 'custom_table_example');
    if (empty($item['link_en'])) $messages[] = __('link_en is required', 'custom_table_example');
    if (empty($item['link_img'])) $messages[] = __('link_img is required', 'custom_table_example');
    if (empty($item['type'])) $messages[] = __('type is required', 'custom_table_example');


    if (empty($messages)) return true;
    return implode('<br />', $messages);
}

Thank you

  • 写回答

1条回答 默认 最新

  • doujia8801 2017-10-31 02:44
    关注

    I found solution, I change this code

    $result = $wpdb->update($table_name, $item, array('id' => $item['id']));
    

    to

    $sql ="UPDATE $table_name
                SET `name`= '".$item['name']."',
                `location` = '".$item['location']."',
                `price` = '".$item['price']."',
                `room` = '".$item['room']."',
                `des_id` = '".$item['des_id']."' ,
                `des_en` = '".$item['des_en']."',
                `link_id` = '".$item['link_id']."',
                `link_en` = '".$item['link_en']."',
                `link_img` = '".$item['link_img']."',
                `type` = '".$item['type']."'
            WHERE  `id` = '".$item['id']."'";
    
            $result = $wpdb->query($sql);
    

    and my problem is done.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划