PHP mysqli_query - INSERT INTO不工作,错误不清楚

时间:2014-07-08 11:16:52

标签: php mysql sql

我有很多变量,我正在从我网站上的所有产品页面添加到数据库中。这是我的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。

出于某种原因,我收到了这个错误:

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个数组将所有值打印到相应的列中。

5 个答案:

答案 0 :(得分:3)

以下是(遗失:

$price')

应:

$price)')

答案 1 :(得分:2)

如果您使用wordpress,则无需创建新连接。 您可以帮助您:

global $wpdb;
$wpdb->query('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."')'); 

答案 2 :(得分:1)

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));

未公开的价值观()

答案 3 :(得分:1)

更新了查询

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));
}

答案 4 :(得分:1)

尝试将值变量放在单引号中,如'$qpID','$name'等,整个查询放在双引号中。

相关问题