Php 为数据库查询添加值

Php 为数据库查询添加值,php,mysql,Php,Mysql,我想为INSERT语句添加另一个值,但无论我做什么,它都会导致各种错误。有人能告诉我正确的编码方法吗 我的原始工作代码如下: $sql = "INSERT INTO `act` (`department`) VALUES ('". implode("'),('", $dept) . "')"; 我曾尝试过: $sql = "INSERT INTO `act` (`department`,`item`) VALUES ('". implode("'),('", $dept) . "','". i

我想为
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);

多个插入的格式应为:

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);
插入'act'('department','item`)值('dept1','item1'),('dept2','item2'),('dept1','item1'),('dept3','item3');,
$insert=array();

对于($i=0;$i你可以试试这样的东西

$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项的基础上,是否有一种方法可以像我的示例中那样只包含另一个字段而不会导致错误?谢谢