我正在尝试编写一个可以在 WooCommerce 安装 (Wordpress) 中插入产品的代码。我不断收到此错误,即使参数计数在数据库表(23 列)、查询(23 列)和值(也是 23)中看起来都不错。我猜想出了什么问题,但无论我检查代码多少次,一切对我来说似乎都很好。
$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:
数组(大小=18)
'post_author' => int 1
'post_title' => 字符串 'SIME BRAVA SLIM 25BF ESTANCA NATURAL IONIZADA 8112500'(长度=53)
'post_content' => 字符串 '' (长度=0)
'post_excerpt' => 字符串 '' (长度=0)
'post_status' => 字符串'发布'(长度=7)
'comment_status' => 字符串'已关闭'(长度=6)
'ping_status' => 字符串'已关闭'(长度=6)
'post_password' => 字符串 '' (长度=0)
'post_name' => 字符串 '' (长度=0)
'to_ping' => 字符串 '' (长度=0)
'pinged' => 字符串 '' (长度=0)
'post_content_filtered' => 字符串 '' (长度=0)
'post_parent' => int 0
'guid' => 字符串 'http://fs.local/?post_type=product&p=' (长度=37)
'menu_order' => int 0
'post_type' => 字符串'产品'(长度=7)
'post_mime_type' => 字符串 '' (长度=0)
'comment_count' => int 0
Wordpress 原始 wp_posts 表具有以下结构(对于较新版本的 Wordpress,使用 utf8mb4_unicode_520_ci 编码而不是 ut8_general_ci):
我的表结构:
非常感谢任何帮助。提前致谢。
最佳答案
$currenttime= current_time( 'mysql' );
将此 $currenttime
而不是 now()
放置在 post_modified/post_modified_gmt
关于php - 将数据插入 Wordpress 数据库时出错。 SQLSTATE[HY093] : Invalid parameter number,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44387726/