You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP存储商品描述时触发MariaDB SQL语法错误

商品描述存入MariaDB时SQL语法错误排查

我开发了一个小型应用,从API获取商品目录并存入自有数据库。除商品描述外,其余功能均正常。存入描述时出现以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 's handkerchiefs. Gradually, she became a true symbol of Italian pizzazz between ' at line 1

我猜测是SQL不接受标点符号,但不确定。

准备商品数据并传入DB.php的代码

$xml = simplexml_load_string(file_get_contents($url));
$xml->preserveWhiteSpace = false;
$xml->formatOutput = true;
$xml->saveXML();

$jsonList = json_decode(json_encode($xml));

//echo '<pre>'. print_r($xml->PRODUCT, true). '</pre>';
$length = count($xml);

$stack = array();
foreach($jsonList->PRODUCT as $product){
    if($product->ID == 117758){
        //var_dump($product);
        array_push($stack, $product);
    }
}

foreach ($stack as $products) {
    SaveProduct($products);
}

DB.php中存入数据库的代码

<?php
global $Connection;
function openConnection()
{
    global $Connection;
    $Connection = new mysqli("localhost", "root", "", "test");

    $Connection->select_db("test");

    if ($Connection->connect_error) {
        die("Connection failed: " . $Connection->connect_error);
    }
}

function SaveProduct($product){
    openConnection();
    global $Connection;

    if(is_string($product->DESCRIPTION)){
        $sql = "INSERT INTO product (`P_ID`,`Type`,`Manufacturer`,`Line`,`Name`,`Size`,`Variant`,`Variant_code`,`Variant_image`,`Sex`,`Image`,`Description`,`Price`,`Stock`,`Ean`,`Ean2`) VALUES ('".$product->ID."','".$product->TYPE."','".$product->MANUFACTURER."','".$product->LINE."','".$product->NAME."','".$product->SIZE."','NULL','NULL','NULL','".$product->SEX."','".$product->IMAGE."','".$product->DESCRIPTION."','".$product->PRICE."','".$product->STOCK."','".$product->EAN."','NULL')" or die("Error while adding product");
    }

    $result = $Connection->query($sql)or die($Connection->error);

    echo $result;
}

附$product的截图及报错信息。

若需更多信息,请告知,我会补充!:)

内容的提问来源于stack exchange,提问作者Emin Ozdogan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 03:51:40