Multi-line MySQL Query and reference Array

I am going to have a long line of code for a sql query and I’d like it to be on different lines to make it easier to read. How can I do this? Also… How can I reference Items in an Array in the query string? What I have doesn’t see to work.

[php]
function SaveNewOrder() {
//Add Array here for testing
$OrderData = array(
‘InvoiceNumber’ => ‘H4HF8RUG74R5G’,
‘OrderStatus’ => ‘Waiting’,
‘PaymentStatus’ => ‘Complete’,
‘OrderSubtotal’ => ‘124.99’);

foreach($OrderData as $key => $val)
{
$OrderData[$key] = mysql_real_escape_string(htmlentities(stripslashes($val), ENT_QUOTES));
}

$sql = "INSERT INTO `Orders` (`Invoice_Number`, `Order_Status`, `Payment_Status`, `Order_Subtotal`) VALUES ($OrderData['InvoiceNumber'],$OrderData['OrderSatus'],$OrderData['PaymentStatus'],$OrderData['OrderSubtotal'])";

}
[/php]

This what i mean by multi-line query string…

[php] $sql = "INSERT INTO Orders (Invoice_Number,
Order_Status,
Payment_Status,
Order_Subtotal)
VALUES (’$OrderData[‘OrderSatus’]’,
‘$OrderData[‘PaymentStatus’]’,
‘$OrderData[‘OrderSubtotal’]’)
";[/php]

I think I found a better way of doing this… It seems to work well. Anyone see any problems with this?
The following is applied before this function is called.
[php]$_POST[$key] = htmlentities(stripslashes($val), ENT_QUOTES);[/php]

[php]function SaveNewOrder($OrderData) {
$values = array_map(‘mysql_real_escape_string’, array_values($OrderData));
$keys = array_keys($OrderData);
return mysql_query(‘INSERT INTO Orders ('.implode(',', $keys).') VALUES (’’.implode(’’,’’, $values).’’)’);
}
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service