【发布时间】:2014-04-02 01:23:45
【问题描述】:
好吧...我在这里不知所措。转向这里寻求帮助。
我正在尝试将一个包含用户信息的 csv 文件插入到数据库中。我首先检查 UserID 是否存在,如果不存在,则使用简单的查询语句为每个用户插入数据。
问题是,它不是将所有用户数据插入一行,而是仅将名字插入行中的每个字段。
名字 *姓* 地址
乔治·乔治·乔治
我没有分配自动递增键,因为我已经有一个我想用作参考的 USERID。这段代码不应该做我想做的事吗?
$stmt2 = $con->query("INSERT INTO Persons
(First_Name, Middle_Initial, Last_Name, Address, Address2, City, State, Zip_Code,Daytime_Phone,Nighttime_Phone,Customer_ID)
VALUES ('$FirstName','$MiddleInitial','$LastName','$Address','$Address2','$City','$State','$Zip','$DayPhone','$NightPhone','$CustomerID')");
这就是我分配 php 变量的方式
if (($handle = fopen("data/" . $File, "r")) !== FALSE) {
// File is available
// retrieve one data line at a time
$row = 1;
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);
if ($row == 1) {
// These are the column names
$FormattedFields = str_replace(' ', "", $data);
preg_replace('/[^A-Za-z0-9-]/', '', $FormattedFields);
foreach ($FormattedFields as $Key => $UpperField) {
$LowerField = strtolower($UpperField);
$NewFields[$Key] = $LowerField;
}
$FNL = array_search('firstname', $NewFields);
$LNL = array_search('lastname', $NewFields);
$MIL = array_search('middleinitial', $NewFields);
$AL = array_search('middleinitial', $NewFields);
$A2L = array_search('middleinitial', $NewFields);
$CL = array_search('middleinitial', $NewFields);
$SL = array_search('middleinitial', $NewFields);
$ZL = array_search('middleinitial', $NewFields);
$DPL = array_search('middleinitial', $NewFields);
$NPL = array_search('middleinitial', $NewFields);
$CIDL = array_search('middleinitial', $NewFields);
} else {
$FirstName = $data[$FNL];
$MiddleInitial = $data[$MIL];
$LastName = $data[$LNL];
$Address = $data[$AL];
$Address2 = $data[$A2L];
$City = $data[$CL];
$State = $data[$SL];
$Zip = $data[$ZL];
$DayPhone = $data[$DPL];
$NightPhone = $data[$NPL];
$CustomerID = $data[$CIDL];
【问题讨论】:
-
你能展示你如何为你的 php 变量赋值的代码吗?
-
Eeeh....这是一个黑客工作...让我试试。
-
您显然正在以某种方式将
$FirstName、$LastName等设置为相同的值。 -
另外,根据您迄今为止发布的内容,您对SQL injection 持开放态度。
-
我不担心安全问题,我知道这很容易被注入,但感谢您的提示。这只是一个本地脚本。