2017-06-06 76 views
0

我正在嘗試製作一個可以在WooCommerce安裝(Wordpress)中插入產品的代碼。我不斷收到這個錯誤,即使在數據庫表(23列),查詢(23列)和值(也是23)中參數計數似乎都不錯。我猜想有些事情是錯的,但無論我檢查代碼多少次,對我來說一切似乎都很好。將數據插入Wordpress數據庫時出錯。 SQLSTATE [HY093]:無效的參數編號

$sqlinsert = "INSERT INTO wp_posts (ID,post_author,post_date,post_date_gmt,post_content,post_title,post_excerpt,post_status,comment_status,ping_status,post_password,post_name,to_ping,pinged,post_modified,post_modified_gmt,post_content_filtered,post_parent,guid,menu_order,post_type,post_mime_type,comment_count) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 
       $sth = $dbh->prepare($sqlinsert); 

       if($sth->execute(array(
       "ID" => $id, 
       "post_author" => $post_author, 
       "post_date" => "NOW()", 
       "post_date_gmt" => "NOW()", 
       "post_content" => $post_content, 
       "post_title" => $post_title, 
       "post_excerpt" => $post_excerpt, 
       "post_status" => $post_status, 
       "comment_status" => $comment_status, 
       "ping_status" => $ping_status, 
       "post_password" => $post_password, 
       "post_name" => $post_name, 
       "to_ping" => $to_ping, 
       "pinged" => $pinged, 
       "post_modified" => "NOW()", 
       "post_modified_gmt" => "NOW()", 
       "post_content_filtered" => $post_content_filtered, 
       "post_parent" => $post_parent, 
       "guid" => $guid, 
       "menu_order" => $menu_order, 
       "post_type" => $post_type, 
       "post_mime_type" => $post_mime_type, 
       "comment_count" => $comment_count 
      ))){ 
       $contadorinsert++; 
       }else{ 
       $contadorerror++; 
       } 

我已經檢查了幾次,沒有一個數值是NULL,因爲WordPress的wp_posts表中的所有字段設置爲需要NOT NULL值。這是我的$post陣列,它包含了所有的值,我發送到數據庫的var_dump

/var/www/fs/plugins/importador_woocommerce/controller/importador_woocommerce.php:115: array (size=18) 'post_author' => int 1 'post_title' => string 'SIME BRAVA SLIM 25BF ESTANCA NATURAL IONIZADA 8112500' (length=53) 'post_content' => string '' (length=0) 'post_excerpt' => string '' (length=0) 'post_status' => string 'publish' (length=7) 'comment_status' => string 'closed' (length=6) 'ping_status' => string 'closed' (length=6) 'post_password' => string '' (length=0) 'post_name' => string '' (length=0) 'to_ping' => string '' (length=0) 'pinged' => string '' (length=0) 'post_content_filtered' => string '' (length=0) 'post_parent' => int 0 'guid' => string 'http://fs.local/?post_type=product&p=' (length=37) 'menu_order' => int 0 'post_type' => string 'product' (length=7) 'post_mime_type' => string '' (length=0) 'comment_count' => int 0

WordPress的原wp_posts表的結構如下(與utf8mb4_unicode_520_ci編碼,而不是ut8_general_ci爲新版本而Wordpress):

我的表結構:

enter image description here

任何幫助非常感謝。提前致謝。

+0

不應該'ID'是AI? – Option

+0

和發帖日期應該是當前時間戳? –

+0

這種指定佔位符的作品方式 –

回答

0
$currenttime= current_time('mysql'); 

將這個$currenttime而非now()post_modified/post_modified_gmt

+0

感謝您的建議,但我執行我的代碼的地方不是Wordpress安裝。我嘗試從外部PHP應用程序插入數據到Wordpress數據庫,而不是Wordpress代碼。 –

+0

然後刪除從現在開始,現在只需要雙引號() –

+0

從NOW()中刪除引號會導致腳本崩潰,因爲它是MYSQL函數,而不是PHP一個,因此PHP說我的函數是未定義的。 –

相關問題