Bind_param() Output

86 views Asked by At

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)");
1

There are 1 answers

1
MaThar Beevi On

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