奇怪的语法错误 MySQL 使用 PHP 时出错

Strange Syntax Error MySQL Error when using PHP

我在这段代码的第 1 行附近遇到语法错误:

//...
$nr = 126;
$expl = "'test'";
$da = "'2003-12-01'";
$tax = 2.5;
$cost = 100;

//Here is the error
$sql = "INSERT INTO tbl (type, nr, explan, date, tax, cost) VALUES('Example'," + $nr + ",'"+ $expl + "','" + $da + "'," + $tax + "," + $cost + ");";

这是错误日志:

Error: 103.5
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 '103.5' at line 1

我使用的是 PHP 版本 5.6.3 和 MySQL 版本 5.6.21

我想不通,问题是什么:(

您正在使用 + 连接字符串,这在 PHP 中不起作用。你必须使用 ..

可能有一些奇怪的算术运算产生了 103.5,比如 2.5 + "," + 100。其中 "," 转换为 int(1) 或其他内容。