tags:

views:

53

answers:

3

hello

i would like to add another value to an insert statement, but whatever i do it cause various errors on whatever way i try it. can someone show me the correct way to code this. many thanks

my original working code is this:

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

and i have tried amongst others:

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

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);
Matt
on the basis that the code is working with the 1 item, is there a way to just include another field as in my example without causing error? thanks
Mr.Putersmit
@Mr.Putersmit: What is in the format of `$dept` and `$box`?
Matt
Matt. varchar. if it helps i posted the code above. thanks
Mr.Putersmit
A: 

perhaps i should post my code that produces the result. thanks

$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);
Mr.Putersmit
A: 

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