我有很多变量要从我网站上的所有产品页面添加到数据库中。这是我的 PHP 代码:
$args = (array(
"post_type" => "page",
"meta_key" => "_wp_page_template",
"meta_value" => "products.php",
"posts_per_page" => 50
));
$queryPages = get_posts($args);
$con= mysqli_connect("localhost","pmdot","password","pmd_ot");
foreach($queryPages as $qp) {
$qpID = $qp->ID;
$name = get_post_meta($qpID, "pmd_name", true);
$code = get_post_meta($qpID, "pmd_sku", true);
$installation = get_post_meta($qpID, "pmd_installation", true);
$age = get_post_meta($qpID, "pmd_agefrom", true);
$length = get_post_meta($qpID, "pmd_length", true);
$width = get_post_meta($qpID, "pmd_width", true);
$height = get_post_meta($qpID, "pmd_height", true);
$areal = get_post_meta($qpID, "pmd_areal", true);
$areaw = get_post_meta($qpID, "pmd_areaw", true);
$cfh = get_post_meta($qpID, "pmd_cfh", true);
$weight = get_post_meta($qpID, "pmd_weight", true);
$type[] = get_post_meta($qpID, "pmd_playtype", true);
$accred[] = get_post_meta($qpID, "pmd_accreditation", true);
$descr = get_post_meta($qpID, "pmd_shortdesc", true);
$price = get_post_meta($qpID, "pmd_price", true);
mysqli_query($con, 'INSERT INTO pmd_productdata (Product_ID, Product_Name, Product_Code, Product_Installation, Age_Range, Product_Length, Product_Width, Product_Height, Minimum_Area_L, Minimum_Area_W, C_F_H, Product_Weight, Play_Type, Accreditations, Product_Desc, Product_Price)
VALUES ($qpID, $name, $code, $installation, $age, $length, $width, $height, $areal, $areaw, $cfh, $weight, "placeholder", "placeholder", $descr, $price') or die ('Error:' .mysqli_error($con));
}
它所做的是使用模板 products.php
获取所有帖子,然后使用所有自定义字段通过查询填充 SQL 表。
注意:您可以看到的数组当前未在查询中使用,因为我认为这是问题所在。如果有人可以帮助我获取该数组中的所有值以显示在相应的字段中,则加分。
我表中的所有列都以 varchar(255) 作为其 char 基数,以防万一有些不是 INT,而它们应该是 INT。
出于某种原因,我收到此错误:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1
现在,正如您所看到的,错误是相当不清楚的,如果有人能对这种情况有所了解,我将不胜感激。
我的问题是:为什么会出现该错误,这是什么意思?
编辑[已解决]
现在我收到一个新错误,我已经修复了 VALUES () 问题:
Unknown column '$qpID' in 'field list'
这是因为我需要将变量与查询分开:例如:
'VALUES (' . $qpID . ', ' . $name . ')' etc.;
编辑2
现在错误已经修复,数据在 SQL 数据库中,我现在需要的是弄清楚如何让 2 个数组将它们的所有值打印到各自的列中。
最佳答案
这是一个 (
缺失:
$price')
应该:
$price)')
关于PHP mysqli_query - INSERT INTO 不工作,错误不清楚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24630401/