gpt4 book ai didi

php - SQL 查询每次都失败... PHP

转载 作者:行者123 更新时间:2023-11-29 19:01:26 25 4
gpt4 key购买 nike

我想将价格、SKU 和链接从各种 CSV 文件插入数据库。为此,我迭代每个 CSV 文件并使用值 ($sql) 堆叠一个字符串。但它不会被插入到我的数据库中......

这是我如何堆叠 $sql 字符串的示例:

    $sql = "INSERT INTO cron (sku, price, link) VALUES" . " \n";


if((stripos($inputFilename, "bok24"))){
// Loop through each row creating a <row> node with the correct data
while (($row = fgetcsv($inputFile, 0, $delimiter)) !== false) {
if(($row[0] != null) || ($row[0] != "")){
$sql .= "(\"" . $row[0] . "\",\"" . $row[4] . "\",\"" . $row[20] . "\"),";
}
}

... other CSV files

为了检查错误所在,我将 $sql 字符串打印到文件中。在他被 mysqli_real_escape(...)` 和后记转义之前,我将字符串打印到文件中...

    $myfile = fopen("data/sql.txt", "a");
fwrite($myfile, $sql . "\n\n");
fclose($myfile);

$sql = substr($sql, 0, -1) . ";";
$sql = $GLOBALS["database"]->real_escape_string($sql);

if($GLOBALS["database"]->query($sql) === TRUE){
echo "Successfully transfered to database!\n";
}else{
echo "ERROR by transferring to database!\n";
}

$myfile = fopen("data/sql.txt", "a");
fwrite($myfile, $sql . "\n\n");
fclose($myfile);

在此文件中,您可以找到转义和未转义的变体。但我没有看到我的错误...有人可以帮助我并告诉我错误在哪里吗?

Here you can find the sql.txt file (搜索\n 以获取文件中的下一个 sql 字符串)。

问候并谢谢您!

最佳答案

你可以这样做:

    if((stripos($inputFilename, "bok24"))){
// Loop through each row creating a <row> node with the correct data
$counter_inserted = 0;
while (($row = fgetcsv($inputFile, 0, $delimiter)) !== false) {
if(($row[0] != null) || ($row[0] != "")){
$sql = "INSERT INTO cron (sku, price, link) VALUES" . " \n";
$sql .= "(\"" . $GLOBALS["database"]->real_escape_string($row[0]) . "\",\"" . $GLOBALS["database"]->real_escape_string($row[4]) . "\",\"" . $GLOBALS["database"]->real_escape_string($row[20]) . "\"),";
if($GLOBALS["database"]->query($sql)){
$counter_inserted++;
}
}
}

echo $counter_inserted; 将显示插入了多少记录

关于php - SQL 查询每次都失败... PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43848651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com