我为CRUD数据库wordpress制作插件,但是当我点击提交link_id
时总是返回0
错误信息
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'"
正如您所见link_id
总是0,但实际上它不是0
我的脚本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);
}
谢谢
答案 0 :(得分:0)
我找到了解决方案,我更改了此代码
$result = $wpdb->update($table_name, $item, array('id' => $item['id']));
到
$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);
我的问题已经完成。