douju4278 2016-12-20 17:48
浏览 74

Wordpress检查重复并插入数据

I am trying to add data from another website to my Wordpress site. For that I have created a Plugin. In this Plugin I can get data from my other website, but i can't and it to the my Wordpress Website. Below is my code for that.

$mysqlis=mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

     $slug = str_replace(" ","-",$value);

     $checkdublicate = $mysqlis->query("SELECT * FROM `temp_wp_terms` WHERE `slug` = '.$slug.' AND `name`='.$value.'");

     if ($checkdublicate->num_rows == "0") 
     {
        if ($value != "." && $value != "..")
        {
         $query = "INSERT INTO `temp_wp_terms`(`name`, `slug`, `term_group`) VALUES ('.$value.', '.$slug.', '0')";
         $mysqlis->query($query);
            if(!$mysqlis)
            {
            echo "Error in insert";
            } 
        }
         else
         {
             mylog("173 insert failed NoRow: " .print_r($checkdublicate->num_rows,TRUE));

         }
     }
     $mysqlis->close();

Please give me any solution. Thank you

  • 写回答

1条回答 默认 最新

  • dongna2498 2016-12-20 22:09
    关注

    If you're using a plugin you should use the global $wpdb object rather than mysqli. $wpdb provides a way to prepare custom queries and automatically uses the database credentials in the wordpress config file.

    try something like this, (the replace query will avoid duplicates if you have proper indexes set.):

    global $wpdb;
    
    $query = $wpdb->prepare( "REPLACE INTO `temp_wp_terms`(`name`, `slug`, `term_group`) VALUES (%s, %s, %d)", $value, $slug, 0 );
    
    $wpdb->query( $query )
    

    Source: https://developer.wordpress.org/reference/classes/wpdb/

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度