【发布时间】:2014-02-09 04:24:12
【问题描述】:
我需要从一个数组构建 INSERT 语句,但我被困住了,这是我目前所拥有的,但它显然是不正确的:
INSERT INTO company (, Name, Address, City, State, Zip, Country, Website, NOL
INSERT INTO additionallocations (, CompanyID, Address, City, State, Zip, Country
这是我的数组以及我如何构建语句:
$fieldsArray = array(
'company' => array(
'Name' => $Name,
'Address' => $Address,
'City' => $City,
'State' => $State,
'Zip' => $Zip,
'Country' => $Country,
'Website' => $Website,
'NOL' => $NOL,
),
'additionallocations' => array(
'CompanyID' => $CompanyID,
'Address' => $Address,
'City' => $City,
'State' => $State,
'Zip' => $Zip,
'Country' => $Country,
)
);
foreach($fieldsArray as $table => $rows) {
$sql = "INSERT INTO " . $table . " (";
foreach($rows as $column => $value){
$sql .= ", " .$column;
}
}
echo $sql;
这是我想要的结果:
INSERT INTO company (Name, Address, City, State, Zip, Country, Website, NOL) VALUES
('$Name', '$Address', '$City', '$State', '$Zip', '$Country', '$Website', '$NOL');
我需要找到一种方法来继续使用 VALUES 构建它,并去掉左括号后的第一个逗号。有人有什么建议吗?
【问题讨论】:
-
你说你需要这个值,你的值将如何传递?
-
什么意思?我想在 VALUES 部分中使用变量名称,例如
INSERT INTO company (Name, Address, City, State, Zip, Country, Website, NOL) VALUES ('$Name', '$Address', '$City', '$State', '$Zip', '$Country', '$Website', '$NOL');
标签: php mysql arrays multidimensional-array