0

我不明白为什么这段代码会被破坏。我将 jQuery 数组发送到 php 脚本。

jQuery:

var someArray = ["elementone", "elementtwo", "elementthree"];
$.post(addToDB.php, {thisVariable: someArray}, function (data) {alert data;});

PHP:

$someArray = $_POST['thisVariable'];
$query = 'INSERT INTO someTable SET someColumn = "' . $someArray. '"';

这是我的问题;我可以回显序列化数组:

echo serialize($someArray); 

但是如果我尝试使用serialize($someArray)而不是$someArray更新数据库,代码会中断并且没有任何更新。如果它尝试回显或更新$someArray,我得到:

Array

如果我回显 $someArray[0]我会得到适当的元素。

我错过了什么?非常感谢任何帮助。

4

1 回答 1

0

While you are going about building sql statements in an entirely inappropriate way, the real issue you're having trouble with is that you are trying to echo an array. that cannot be done. Alos, you cannot append an array to a string using the concatenation operator (.). You'll have to encode (serialize/unserialize) the array for storage; or normalize the database...

You could use print_r, var_dump, var_export or a foreach construct to do that though.

Really though, use parameterized queries; you'll be so immensely much better off.

于 2013-08-16T16:54:52.330 回答