gpt4 book ai didi

php - Mysql插入行为异常

转载 作者:行者123 更新时间:2023-11-30 22:39:30 25 4
gpt4 key购买 nike

所以我使用 php 将一个 csv 文件上传到一个数组中,对其进行去重、清理并将数据插入到 mysql 表中。

表(inboundnew)有9个字段,1个date,1个varchar,7个integer。该脚本按要求处理文件,并构建一个“将命令插入字符串”,然后我运行它:

$result=  mysql_query($string) or die(mysql_error())

插入字符串输出为:

insert into inboundnew 
(ndate,branch,ans,unans,voice,unqueued,actioned,unactioned, total)
values
("2000-01-01","Name of Customer",0,0,0,0,0,0,0);

它给我以下错误

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

然而,当我通过 phpmyadmin 运行该字符串时,它会完美地插入。

如果有人知道这里可能出了什么问题,将不胜感激。

好的 - 经过两天的思考,我仍然遇到问题。

我已经在下面发布了我运行以构建然后在查询中运行的字符串的代码。它没有撇号,但我仍然收到 MySQL 错误。如果代码格式不正确,我深表歉意 - 我不确定如何在此处执行此操作。我已经上传并打开了 CSV 文件...

while(!feof($file)) { $lines[] = fgets($file); $clean=array();

$clean = array_unique($lines);//remove duplicates

$count=0;
$string="insert into inboundnew (date,branch,ans,unans,voice,unqueued,actioned,unactioned,total) values ";

//现在写入表 - 跳过前两行

enter code here

if ($count > 1) {


$parts=explode(',', $val);
$a=$parts[0];//ignore this field
$b=$parts[1];//ignore this field)
if ($b>""){ //ignore if blank line
$salon=$parts[2];
$ans=$parts[3];
$missed=$parts[4];
$voice=$parts[5];
$unq=$parts[6];
$act=$parts[7];
$unact=$parts[7];
$total=$parts[8];
$date=$date;
$string .= '("'.$date.'","'.$salon.'",'.$ans.','.$missed.','.$voice.','.$unq.','.$act.','.$unact.','.$total.'),'. PHP_EOL;
}
}
$count++;
}
//now remove last comma
$strlen= strlen($string);
$string= substr($string,0, $strlen-2);

这会构建在脚本中失败的字符串,但在 PhpMyAdmin 中有效并输出语法错误

最佳答案

使用 mysql_real_escape_string 转义字符串值。

例子:

$name = mysql_real_escape_string($name);
$sql = "insert into test (name) values ('$name')";

关于php - Mysql插入行为异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31498792/

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