这里我的代码中有什么我没看到我的代码有什么问题,但我无法弄明白为什么它不会保存在数据库上。
我的阵列详细信息
Array
(
[dates] => Array
(
[0] => 2012-01-18
[1] => 2012-02-18
[2] => 2012-03-18
[3] => 2012-04-18
[4] => 2012-05-18
)
[amount] => Array
(
[0] => 2000
[1] => 2000
[2] => 2000
[3] => 2000
[4] => 2000
)
[deposit] => Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
)
我的保存代码
$sql = "INSERT INTO transaction (id, details)
VALUES('$id', $serializeddetails)";
$query = mysql_query($sql) or die("Fatal error: ".mysql_error());
我收到了这个错误
Fatal error: 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 'a:3:{s:5:"dates";a:6:{i:0;s:10:"2012-01-18";i:1;s:10:"2012-02-18";i:2;s:10:"2012' at line 2
我使用longtext作为我的数据类型并尝试longblob但没有希望,
我也尝试使用base64_ecode();
$serializeddetails = base64_encode(serialize($detailsarray));
并收到此错误
Fatal error: 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 2
我的代码出了什么问题,为什么这不会存入数据库?
谢谢
答案 0 :(得分:2)
$sql = "INSERT INTO transaction (id, details)
VALUES('$id', '$serializeddetails')";
$ serializeddetails是一个字符串,你需要引号。还要确保序列化数据被转义。我想到了mysql_real_escape_string()或addslashes()。对于您的测试数据(序列化后不包含单引号),这不是必需的,但它可以与其他数据一起使用。