0

enter image description hereI am facing issues while inserting multiple records in sql server using php.

Here is my query:-

 INSERT INTO [BP].[users](id,username,email,contact,status) VALUES
        ('12','sujata','[email protected]','8588841506','0'),
        ('13','sonali','[email protected]','7894561231','0'),
        ('14','khushboo','[email protected]','7894561230','0')

It shows me this error message always:-

Msg 103010, Level 16, State 1, Line 20

Parse error at line: 2, column: 49: Incorrect syntax near ','.

This is my query from php file:-

 $query = "INSERT INTO users(id,username,email,contact,status,num_update) VALUES  $import_data ;

This is what import data returns

('12','sujata','[email protected]','8588841506','0'),('13','sonali','[email protected]','7894561231','0'),('14','khushboo','[email protected]','7894561230','0')

When I print the query it shows:-

INSERT INTO users(id,username,email,contact,status) VALUES ('12','sujata','[email protected]','8588841506','0'),('13','sonali','[email protected]','7894561231','0'),('14','khushboo','[email protected]','7894561230','0')

When I print Sql Error,it shows:-

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 ','.
14
  • what is the type of your status column ? Commented Apr 20, 2018 at 5:17
  • @Lakshitha:-status is varchar. Commented Apr 20, 2018 at 5:19
  • Try to remove your schema(i.e. [BP]) and insert data Commented Apr 20, 2018 at 5:20
  • @Devika:-Schema name is BP & Table name is users. Commented Apr 20, 2018 at 5:20
  • @Lakshitha:- When I enter insert single entry,it works perfect but facing issue with multiple entries. Commented Apr 20, 2018 at 5:21

3 Answers 3

1

Try this

 INSERT INTO [BP].[users](id,username,email,contact,status)

 select  '12','sujata','[email protected]','8588841506','0'
  union

select  '13','sonali','[email protected]','7894561231','0'
 union

select '14','khushboo','[email protected]','7894561230','0' 
Sign up to request clarification or add additional context in comments.

4 Comments

You will have to split (by comma) each entry to distinguished the separate record and shall replace with a union. It is obvious that you have to alter your import date to generate the desired query. And not to forget you will have to get rid of from the parenthesis as well.
:-This solution worked..I was able to insert multiple records in sql server only using this way...
The only issue I am facing now is that each record is inserting twice in database..Any suggestions over this
Could you upload the code anywhere so that I can have a look? My suggestion is that you need to make sure you generating dynamic correct and only once.
0

Actually my php skill not wery good. But I suggest an approach for solve a dynamic one. First you can parse your string with regex or if value is an array you can loop at other ways

    ('12','sujata','[email protected]','8588841506','0'),
    ('13','sonali','[email protected]','7894561231','0'),
    ('14','khushboo','[email protected]','7894561230','0')

Then you can insert per row

Comments

0

if you have csv file then you need to do run following code to insert data in table

$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);

5 Comments

How to create chat room here,will discuss it there
It seems I dont have previliges to chat..It shows me mssage hereYou must have 20 reputation on Stack Overflow to talk here.
you can upload your code with csv file any where and give me url .
Rahul,Thank you for all your help..You were really a support in all this troubleshotting!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.