我在使用php在sql server中插入多个记录时遇到问题。
这是我的查询: -
INSERT INTO [BP].[users](id,username,email,contact,status) VALUES
('12','sujata','td@hh.com','8588841506','0'),
('13','sonali','th@wq.com','7894561231','0'),
('14','khushboo','tr@lp.com','7894561230','0')
它始终显示此错误消息: -
Msg 103010,Level 16,State 1,Line 20
在行处解析错误:2,列:49:','附近的语法不正确。
这是我在php文件中的查询: -
$query = "INSERT INTO users(id,username,email,contact,status,num_update) VALUES $import_data ;
这是导入数据返回的内容
('12','sujata','td@hh.com','8588841506','0'),('13','sonali','th@wq.com','7894561231','0'),('14','khushboo','tr@lp.com','7894561230','0')
当我打印查询时,它会显示: -
INSERT INTO users(id,username,email,contact,status) VALUES ('12','sujata','td@hh.com','8588841506','0'),('13','sonali','th@wq.com','7894561231','0'),('14','khushboo','tr@lp.com','7894561230','0')
当我打印Sql Error时,它会显示: -
SQLSTATE: 42000
code: 103010
message: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Parse error at line: 1, column: 104: Incorrect syntax near ','.
SQLSTATE: 42000 code: 103010 message: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Parse error at line: 1, column: 104: Incorrect syntax near ','.
答案 0 :(得分:1)
试试这个
INSERT INTO [BP].[users](id,username,email,contact,status)
select '12','sujata','td@hh.com','8588841506','0'
union
select '13','sonali','th@wq.com','7894561231','0'
union
select '14','khushboo','tr@lp.com','7894561230','0'
答案 1 :(得分:0)
其实我的PHP技能并不是很好。但我建议采用一种解决动态问题的方法。 首先,你可以用正则表达式解析你的字符串,或者如果value是一个数组,你可以用其他方式循环
('12','sujata','td@hh.com','8588841506','0'),
('13','sonali','th@wq.com','7894561231','0'),
('14','khushboo','tr@lp.com','7894561230','0')
然后你可以每行插入
答案 2 :(得分:0)
如果您有csv
文件,则需要运行以下代码以在表
$query = <<<eof
LOAD DATA INFILE 'your csv file name'
INTO TABLE BP_users
FIELDS TERMINATED BY '|' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
(id,username,email,contact,status)
eof;
$db->query($query);