开发者

Add value to database query

I would like to add another value to an INSERT statement, but whatever I do, it causes various errors. Can someone show me the correct way to code this?

My original working code is this:

$sql = "INSERT INTO `act` (`department`) VALUES ('". implode("'),('", $dept) . "')";

I have tried amongst others:

$sql = "INSERT INTO `act` (`department`,`item`) VALUES ('". implode("'),('", $dept) . "','". implode("'),('", $box) . "')";

Perhaps I should post my code that produces the result:

$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-revalidat开发者_如何学Pythone" );
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);


you could try something like this

$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);


Multiple inserts should be in the format:

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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜