dtczp02204 2014-10-02 16:46
浏览 53
已采纳

重复密钥更新在我的本地wordpress中工作,但在另一个站点中不起作用

I created a table to hold meta data about a custom taxonomy and everything works fine in my local instance (created with ampps) of wordpress when I go to insert using ON DUPLICATE KEY UPDATE it finds the row with the unique key group_id and does the update instead of the insert.

however, when I moved this code to my dev instance (instance which is live on the internet on a subdomain) ON DUPLICATE KEY UPDATE creates duplicates. no code has changed.

at this point i'm stumped. is there a mysql version limitation for ON DUPLICATE KEY UPDATE? are there plugins that can disrupt the default behavior of ON DUPLICATE KEY UPDATE on a global scale?

UPDATE: this code is what I'm using to create my table

function create_prg_team_grouping_table(){
global $wpdb;

//create the name of the table including the wordpress prefix (wp_ etc)
$search_table = $wpdb->prefix . "prg_team_grouping";
//$wpdb->show_errors(); 

//check if there are any tables of that name already
if($wpdb->get_var("show tables like '$search_table'") !== $search_table) 
{
    //create your sql
    $sql =  "CREATE TABLE ". $search_table . " (
                  stat_id mediumint(12) NOT NULL AUTO_INCREMENT,
                  use_in_grouping text,
                  group_name text,
                  group_id mediumint(12) NOT NULL,
                  group_order mediumint(9),
                  UNIQUE KEY (stat_id,group_id));";
}

//include the wordpress db functions
require_once(ABSPATH . 'wp-admin/upgrade-functions.php');
dbDelta($sql);

//register the new table with the wpdb object
if (!isset($wpdb->prg_team_grouping)) 
{
    $wpdb->prg_team_grouping = $search_table; 
    //add the shortcut so you can use $wpdb->prg_team_grouping
    $wpdb->tables[] = str_replace($wpdb->prefix, '', $search_table); 
}
}

//add to front and backend inits
add_action('after_switch_theme', 'create_prg_team_grouping_table');
  • 写回答

1条回答 默认 最新

  • doxzrb8721 2014-10-02 17:04
    关注

    I'd double-check that your index is unique / primary. That is, make sure you have a unique constraint on the value that should be triggering the update.

    If the only difference is in the database, chances are the schema is incomplete.

    MySQL INSERT IGNORE / ON DUPLICATE KEY UPDATE not detecting duplicates

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

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值