Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

insert statement using last-insert_id()

New Here ,
Dec 08, 2006 Dec 08, 2006
Hi folks wondering if you can help with a little problem I have. I'm trying to insert data into 4 tables and update the related key using last0_insert_id() and set@ = last_insert_id();
When I run the query colfusion tells me I have a syntax error.

2nd question is:
Can the insert query be written this way in coldfusion
or would it have to be broken down in to seperate queries.

The error occurred in C:\CFusionMX7\wwwroot\review1\admin\insert1.cfm: line 34

32 :
33 : INSERT INTO review(user_id, accom_id, hotel, room, food, entertainment, beach)
34 : VALUES(@user_id, @Accom_id, '#Trim(FORM.hotel)#', '#Trim(FORM.room)#', '#Trim(FORM.food)#','#Trim(FORM.entertainment)#', '#Trim(FORM.beach)#');
35 :
36 : </cfquery>


here is my query which looks ok syntax wise.
<cfquery datasource="reviews">

INSERT INTO country(country_name)
VALUES('#Trim(FORM.country_name)#');

set @country_id = last_insert_id();

INSERT INTO resort(country_id, resort_name)
VALUES(@country_id, '#Trim(FORM.resort_name)#');

set @Resort_id = last_insert_id();

INSERT INTO accom(resort_id, accom_type_id, accom_name)
VALUES(@resort_id, #FORM.accom_type_id#, '#Trim(FORM.accom_name)#');

set @Accom_id = last_insert_id();

INSERT INTO users(first_name, last_name, email)
VALUES('#Trim(FORM.first_name)#', '#Trim(FORM.last_name)#', '#Trim(FORM.email)#');

set @user_id = last_insert_id();

INSERT INTO review(user_id, accom_id, hotel, room, food, entertainment, beach)
VALUES(@user_id, @Accom_id, '#Trim(FORM.hotel)#', '#Trim(FORM.room)#', '#Trim(FORM.food)#','#Trim(FORM.entertainment)#', '#Trim(FORM.beach)#');

</cfquery>

785
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 08, 2006 Dec 08, 2006
sorrry for the repeated post my browser is playing up
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 08, 2006 Dec 08, 2006
In order to run multiple queries from one cfquery, be sure that you have ?allowMultiQueries=true at the end of your connection string, in the datasource entry in CF Administrator.

If that doesn't work, post the full error message here (which includes the generated SQL).
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 09, 2006 Dec 09, 2006
quote:

?allowMultiQueries=true
sorry to be a pain but, do I just put ?allowMultiQueries=true in the connection string box or is it test?allowMultiQueries=true as I'm using the test database in mysql. I'm new to all this so excuse my ignorance.

Could you give me an example of what I put in the connection string box.

Thanks in advance
Dai
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 09, 2006 Dec 09, 2006
Cheers for the reply. I have a query that works using multiple cfquery tags does the job great but, mysql should be able to do the job with the mysql code.
Even Though I have working version I would still like to know if my mysql code is correct and why the insert fails and how to put it right.

Here is the full error message:

Error Executing Database Query.
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; set @country_id = last_insert_id(); INSERT INTO resort(country_id, resor' at line 2

The error occurred in C:\CFusionMX7\wwwroot\review1\admin\play\insert1.cfm: line 34

32 :
33 : INSERT INTO review(user_id, accom_id, hotel, room, food, entertainment, beach)
34 : VALUES(@user_id, @accom_id, '#Trim(FORM.hotel)#', '#Trim(FORM.room)#', '#Trim(FORM.food)#','#Trim(FORM.entertainment)#', '#Trim(FORM.beach)#');
35 :
36 : </cfquery>

SQL INSERT INTO country(country_name) VALUES('Spain'); set @country_id = last_insert_id(); INSERT INTO resort(country_id, resort_name) VALUES(@country_id, 'Barcelona'); set @resort_id = last_insert_id(); INSERT INTO accom(resort_id, accom_type_id, accom_name) VALUES(@resort_id, 2, 'Mari posa'); set @accom_id = last_insert_id(); INSERT INTO users(first_name, last_name, email) VALUES('Dai', 'Butt', 'dai@dai.com'); set @user_id = last_insert_id(); INSERT INTO review(user_id, accom_id, hotel, room, food, entertainment, beach) VALUES(@user_id, @accom_id, 'Just the good ol'' boys, never meanin'' no harm. Beats all you''ve ever saw, been in trouble with the law since the day they was born. Straight''nin'' the curve, flat''nin'' the hills. Someday the mountain might get ''em, but the law never will. Makin'' their way, the only way they know how, that''s just a little bit more than the law will allow. Just good ol'' boys, wouldn''t change if they could, fightin'' the system like a true modern day Robin Hood.', 'Just the good ol'' boys, never meanin'' no harm. Beats all you''ve ever saw, been in trouble with the law since the day they was born. Straight''nin'' the curve, flat''nin'' the hills. Someday the mountain might get ''em, but the law never will. Makin'' their way, the only way they know how, that''s just a little bit more than the law will allow. Just good ol'' boys, wouldn''t change if they could, fightin'' the system like a true modern day Robin Hood.', 'Just the good ol'' boys, never meanin'' no harm. Beats all you''ve ever saw, been in trouble with the law since the day they was born. Straight''nin'' the curve, flat''nin'' the hills. Someday the mountain might get ''em, but the law never will. Makin'' their way, the only way they know how, that''s just a little bit more than the law will allow. Just good ol'' boys, wouldn''t change if they could, fightin'' the system like a true modern day Robin Hood.','Just the good ol'' boys, never meanin'' no harm. Beats all you''ve ever saw, been in trouble with the law since the day they was born. Straight''nin'' the curve, flat''nin'' the hills. Someday the mountain might get ''em, but the law never will. Makin'' their way, the only way they know how, that''s just a little bit more than the law will allow. Just good ol'' boys, wouldn''t change if they could, fightin'' the system like a true modern day Robin Hood.', 'Just the good ol'' boys, never meanin'' no harm. Beats all you''ve ever saw, been in trouble with the law since the day they was born. Straight''nin'' the curve, flat''nin'' the hills. Someday the mountain might get ''em, but the law never will. Makin'' their way, the only way they know how, that''s just a little bit more than the law will allow. Just good ol'' boys, wouldn''t change if they could, fightin'' the system like a true modern day Robin Hood.');

Is it just me who is having problems with adobe sight being very slow to load.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Dec 09, 2006 Dec 09, 2006
quote:

Originally posted by: Dai777
... ...
Is it just me who is having problems with adobe sight being very slow to load.



Glad you got it sorted out (And made a tutorial no less!).

Yes, these forums have continual problems with speed, uptime and reliability (makes you nervous about CF doesn't it?).
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 09, 2006 Dec 09, 2006
finally got it sorted thanks to Mikeroo so made a tute on it.
The tute can be downloaded from http://www.megaupload.com/?d=YQNBIOKZ
for you to view.
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 09, 2006 Dec 09, 2006
it does a bit
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 14, 2006 Dec 14, 2006
LATEST
new tute explaining the insert into multiple tables:

http://fileho.com/download/43c6d4940924/cf-mysql-multi-insert.zip.html
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources