假設這是你想要處理的東西,&你不想循環數組或使用預處理語句,我認爲這應該工作。我重新格式化了您的代碼,但總體邏輯仍然完好無損。我添加的邏輯是基本上創建兩個單獨的數組。一個用於數值。其他文本值:
// 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;
該腳本的輸出是:
INSERT INTO tbl_orders (order_id, user_id, price, quantity) VALUES ('testorderid', 'testuserid',7.99, 2)
編輯下面是一個使用gettype
檢測字符串類型返工。更靈活&健壯。這當然是使用測試數據,但應該很容易適應真實世界的情況。
// 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;
而且這段代碼的輸出是:
INSERT INTO tbl_orders (order_id, user_id, price, quantity) VALUES ('testorderid', 'testuserid',7.99, 2)
變量'$ strsql'具有無效的SQL語法。這可以解釋爲什麼代碼無法正常工作。 –
MySQL引用的數字很好。如果你想保持簡單,只需引用一切。 (但請記住要逃避您的數據!)或者,查看準備好的語句;從長遠來看,他們會讓事情變得更容易。 – cHao
Gordon Linoff,$ strsql SQL語法有什麼問題?這對我來說似乎很好。我印了它,沒有看到有什麼不妥。你可以說得更詳細點嗎? – user2395238