-2

I want to expand a query in a foreach loop. I'm doing it without bind_param(), which causes errors, probably because my values include commas which i do not want to remove. I want to Insert multiple rows with one Query. Is there a Way to use bind_param() in this context?

Code now looks like:

$finalquery = "INSERT INTO mytable (a,b) VALUES";
foreach($xml->entry) {
    $abc_a = $xml->schema;
    $abc_b = $xml->schema->a;
    if($count == 1){
         $finalquery .= "($abc_a,$abc_b)"
    }else($count == 1){
         $finalquery .= ",($abc_a,$abc_b)"
    }
    //COUNT UP
}
$result = $mysqli->query($finalquery) or die ("ERROR: ($finalquery)");
Dharman
  • 30,962
  • 25
  • 85
  • 135
PhilipB
  • 329
  • 2
  • 16

1 Answers1

1

Just keep in mind that when building a query like this, This way i am inserting multiple rows in my single table.

$values = array();
foreach ($_POST as $key => $value) {
    $qvalue = mysql_real_escape_string($value);
    $values[] = "($field1, $field2, $field3, $qvalue)"; // quoted value, not the raw value
}

$query_values = implode(',', $values);

$query = "INSERT INTO tablename(field1, field2, field3, field4) VALUES $query_values";
$result = mysql_query($query, $connection);
MaThar Beevi
  • 294
  • 1
  • 2
  • 10
  • First of all: thanks for you answer. so the query would be something like: "Insert into tablename(field1, field2, field3, field4) values (abc,def,ghi,jk),(acb,fed,ghgi,ik),(...,...,...,..)" right? Problem is, that my values have commas and i need to keep them. I thought that i could use bind_param() but i don't know how to use it for inserting multiple rows. – PhilipB Jun 22 '15 at 10:50