Assuming this is the way you want to handle things, & you do not want to loop through arrays or use prepared statements, I think this should work. I reformatted your code a bit but the overall logic is still intact. The logic I added is to basically create two separate arrays. One for numerical values. The other for text values:
// Test data.
$orderID = 'testorderid';
$userID = 'testuserid';
$price = 7.99;
$quantity = 2;
// Create the text values array.
$insertArrText[] = array('order_id' => $orderID, 'user_id' => $userID);
// Create the numerical values array.
$insertArrNumerical[] = array('price' => $price, 'quantity' => $quantity);
// Get the array keys & merge them into one combined array.
$array_keys = array_merge(array_keys($insertArrText[0]), array_keys($insertArrNumerical[0]));
// Set the column names.
$colNames = "( " . implode(", ", $array_keys) . " )";
// Loop through the '$insertArrText'
foreach ($insertArrText as $key => $row ) {
// Set the numerical values.
$numerical_values = implode(", ", $insertArrNumerical[$key]);
// Set the text values.
$text_values = "'" . implode("', '", $row) . "'";
// Set the column values array.
$colValuesArr[] = "(" . $text_values . "," . $numerical_values . ")";
}
// Set the column values string.
$colValues = implode(", ", $colValuesArr);
// Create the MySQL query.
$strsql = "INSERT"
. " INTO tbl_orders " . $colNames
. " VALUES " . $colValues
;
// Echo the output for testing.
echo $strsql;
The output of that script is:
INSERT INTO tbl_orders ( order_id, user_id, price, quantity ) VALUES ('testorderid', 'testuserid',7.99, 2)
EDIT Here is a reworking using gettype to detect string type. More flexible & robust. This is using test data of course, but should be easily adaptable to real world scenarios.
// Test data.
$orderID = 'testorderid';
$userID = 'testuserid';
$price = 7.99;
$quantity = 2;
// Create an array map based on strings & MySQL DB field values.
$array_map = array();
$array_map['orderID'] = 'order_id';
$array_map['userID'] = 'user_id';
$array_map['price'] = 'price';
$array_map['quantity'] = 'quantity';
// Create the text arrays.
$insertArrText = array();
$insertArrNumerical = array();
// Set arrays for text and numberical types.
$text_types = array('string');
$numerical_types = array('double','integer');
// Lopop through the array map & assign values based on type.
foreach ($array_map as $array_map_key => $array_map_value) {
if (in_array(gettype($$array_map_key), $text_types)) {
$insertArrText[0][$array_map_value] = $$array_map_key;
}
else if (in_array(gettype($$array_map_key), $numerical_types)) {
$insertArrNumerical[0][$array_map_value] = $$array_map_key;
}
}
// Get the array keys & merge them into one combined array.
$array_keys = array_merge(array_keys($insertArrText[0]), array_keys($insertArrNumerical[0]));
// Set the column names.
$colNames = "( " . implode(", ", $array_keys) . " )";
// Loop through the '$insertArrText'
foreach ($insertArrText as $key => $row ) {
// Set the numerical values.
$numerical_values = implode(", ", $insertArrNumerical[$key]);
// Set the text values.
$text_values = "'" . implode("', '", $row) . "'";
// Set the column values array.
$colValuesArr[] = "(" . $text_values . "," . $numerical_values . ")";
}
// Set the column values string.
$colValues = implode(", ", $colValuesArr);
// Create the MySQL query.
$strsql = "INSERT"
. " INTO tbl_orders " . $colNames
. " VALUES " . $colValues
;
// Echo the output for testing.
echo $strsql;
And the output of this code is:
INSERT INTO tbl_orders ( order_id, user_id, price, quantity ) VALUES ('testorderid', 'testuserid',7.99, 2)
$strsqlhas invalid SQL syntax. That could explain why the code is not working.