我想在INSERT
语句中添加另一个值,但无论我做什么,都会导致各种错误。有人能告诉我正确的编码方式吗?
我原来的工作代码是:
$sql = "INSERT INTO `act` (`department`) VALUES ('". implode("'),('", $dept) . "')";
我曾尝试过其他人:
$sql = "INSERT INTO `act` (`department`,`item`) VALUES ('". implode("'),('", $dept) . "','". implode("'),('", $box) . "')";
也许我应该发布产生结果的代码:
$dept = array();
$box = array();
while ($row = mysql_fetch_array($result)) {
$dept[] = $row['department'];
$box[] = $row['custref'];
}
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
header("Cache-Control: no-cache, must-revalidate" );
header("Pragma: no-cache" );
header("Content-type: application/json");
$json = "";
$json .= "{\n";
$json .= "dept: [\"". implode('","', $dept). "\"],\n";
$json .= "box: [\"". implode('","', $box) ."\"]\n";
$json .= "}\n";
echo $json;
$sql = "INSERT INTO `act` (`department`) VALUES ('". implode("'),('", $dept) . "')";
$result = runSQL($sql);
答案 0 :(得分:1)
你可以尝试这样的事情
$sql="INSERT INTO `act`
(`department`,`box`)
VALUES
";
foreach($dept as $index => $value)
{
$sql.="
(
'".mysql_real_escape_string($value)."',
'".mysql_real_escape_string($box[$index])."'
),";
}
$sql=rtrim($sql,',') ;
$result = runSQL($sql);
答案 1 :(得分:0)
多个插入格式应为:
INSERT INTO `act` (`department`, `item`) VALUES ('dept1', 'item1'), ('dept2', 'item2'), ('dept1', 'item1'), ('dept3', 'item3');,
$insert = array();
for ($i=0; $i<sizeof($dept); $i++) {
$insert[] = '(\'' . $dept[$i] . '\',\'' . $box[$i] . '\')';
}
$sql = "INSERT INTO `act` (`department`,`item`) VALUES " . implode(',', $insert);