Php 始终更新wordpress 0
我为CRUD数据库wordpress制作了插件,但当我单击submitPhp 始终更新wordpress 0,php,database,wordpress,Php,Database,Wordpress,我为CRUD数据库wordpress制作了插件,但当我单击submitlink\u id时,总是返回0 错误信息 string(992) "UPDATE `wp_search` SET `id` = '1', `name` = 'Ini Vie', `location` = 'Seminyak', `price` = '160', `room` = '1', `des_id` = 'In
link\u 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\u 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);
}
函数自定义\表\示例\人员\表单\页面\处理程序()
{
全球$wpdb;
$table_name=$wpdb->prefix.'search';//不要忘记表前缀
$message='';
$通知='';
//这是默认的$项,将用于新记录
$default=数组(
“id”=>0,
'名称'=>'',
'位置'=>'',
'价格'=>'',
“房间”=>“,
“des_id'=>”,
“des_en”=>”,
'链接id'=>'',
“link_en'=>”,
“link_img'=>”,
'类型'=>'',
);
//在这里,我们正在验证此请求是否回发并具有正确的nonce
if(wp_verify_nonce($_REQUEST['nonce'],basename(__文件__))){
//将默认项与请求参数组合
$item=shortcode\u atts($default,$\u REQUEST);
//验证数据,如果一切正常,则将项目保存到数据库
//如果id为零,则插入,否则更新
$item\u valid=自定义表\示例\验证\人员($item);
如果($item_valid===true){
如果($item['id']==0){
$result=$wpdb->insert($table\u name,$item);
$item['id']=$wpdb->insert\u id;
如果($结果){
$message=uuuu('项目已成功保存','自定义表\示例');
}否则{
$notice=uuuu('保存项时出错','自定义表\示例');
}
}否则{
$result=$wpdb->update($table_name,$item,array('id'=>$item['id']);
如果($结果){
$message=uuuu('项目已成功更新','自定义表\示例');
}否则{
$notice=uuuu('更新项时出错','自定义_表_示例');
退出(var_dump($wpdb->last_query));
}
}
}否则{
//如果$item_valid非true,则它包含错误消息
$notice=$item\u有效;
}
}
否则{
//如果这不是回发,我们将加载要编辑的项目或创建新项目
$item=$default;
如果(isset($_请求['id'])){
$item=$wpdb->get_行($wpdb->prepare(“从$table_name中选择*,其中id=%d“,$_请求['id']),数组A);
如果(!$项目){
$item=$default;
$notice=uuuu('未找到项','自定义_表\示例');
}
}
}
//这里我们添加了我们的自定义元框
添加元数据框(“人员表单元数据框”、“人员数据”、“自定义表”示例“人员表单元数据框”处理程序、“人员”、“正常”、“默认”);
?>
我找到了解决方案,我更改了代码
$result = $wpdb->update($table_name, $item, array('id' => $item['id']));
到
我的问题解决了。我找到了解决方案,我更改了代码
$result = $wpdb->update($table_name, $item, array('id' => $item['id']));
到
我的问题解决了。不需要“SETid
=”1',-,只需使用SET name=…@jeff我按照你说的做,但仍然不工作,仍然返回0“SETid
=”1',-,不需要-只需使用SET name=…@jeff我按照你说的做,但仍然不工作,仍然返回0