Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

ASP.NET Forum


You are currently viewing our ASP.NET Forum as a guest. Please register to participate.
Login



Reply
Help using SQL insert with variables
Old 08-09-2006, 01:27 AM Help using SQL insert with variables
Super Talker

Posts: 116
Trades: 0
I'm programming something that seems so simple in PHP. It's the SQL Insert using variables (eventually, I want to use data from a Request.Form("field").

Right now, I'm writing the info to a variable...

strEmail = Request.Form("email")
strName = Request.form("name")

The problem is actually writing the sql insert statement...

strSQL_Insert = "INSERT INTO emails (email, name) VALUES (" & "'" & strEmail & "'," & "'" & strName & "'" & ");"

You can't see, but inbetween the quotes there's an apostrophe... " ' "

This works when I use one field only but tells me there are more variables under after the Values than there are columns.

I've tried everything... taking out the quote with the apostrophe to just say..,

VALUES)" & strEmail & strName & ");" - didn't work.

I also tried taking out the semicolon at the end of the statement... didn't work.

Does anyone have any ideas? I know it's all in the syntax.

Thanks
Donna
DonnaZ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-09-2006, 11:56 AM Re: Help using SQL insert with variables
Super Talker

Posts: 116
Trades: 0
Hi All...

I figured it out. Basically I have to declare a temp variable and then write the actual variable string into this temp variable.

Ex:

strTemp = "'" & Request.Form("band_name") & "',"
strTemp = strTemp & " '" & Request.Form("email") & "' "

(You can't see the apostrophes & quotes... but it's " ' " & Request.Form("band_name") & " ', ")

Then I apply this strTemp variable into the SQL Insert...

strSQL_Insert = "INSERT INTO tablename (band_name, contactemail) values (" & strTemp & ");"

Connect.Execute strSQL_Insert

I just wanted to share this.

Boy, SQL inserts are way easier in PHP.

If anyone else has a solution, I'm open to it.

Thanks
DonnaZ
DonnaZ is offline
Reply With Quote
View Public Profile
 
Old 08-12-2006, 09:18 AM Re: Help using SQL insert with variables
chrishirst's Avatar
Missing! presumed drunk.

Posts: 41,519
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
it looks like the quotes are a bit messed up

INSERT INTO emails (email, name) VALUES ('" & strEmail & "','" & strName & "');"


to debug SQL strings use
Code:
response.write strSQLString 
response.end
just before the call to execute the command, that way you will see the command as the sql server will see it
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- I SEO the only industry where all the cowboys are Indians?
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-12-2006, 06:29 PM Re: Help using SQL insert with variables
Minaki's Avatar
Defies a Status

Posts: 1,626
Location: Guildford, UK
Trades: 0
As Chris said, it looks like a string concatination issue. Nothing to do with the difference between ASP & PHP at all really.

What's even easier (and safer, as far as SQL Injection attacks are concerned), is to use parameterised queries. Such as:

INSERT INTO Table (ColumName1, ColName2) VALUES (@Value1, @Value2)

Not sure how to set the parameters up in ASP though, I'm used to doing it in ASP.NET.
__________________
Minaki Serinde MCP
"Wow, Linux is nearly on-par with Windows ME!"

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Minaki is offline
Reply With Quote
View Public Profile Visit Minaki's homepage!
 
Old 08-17-2006, 12:11 PM Re: Help using SQL insert with variables
yoemanspiffy's Avatar
Experienced Talker

Posts: 42
Trades: 0
Donna,

The issue you are having has nothing to do with the SQL. Dealing with those single and double quotes is just a real pain in the arse.

Minaki,

You are pretty darned close.

what you have with:

INSERT INTO Table (ColumName1, ColName2) VALUES (@Value1, @Value2)

Is the RAW SQL code you will be passing to the SQL server. you will need to declare those @Values bud. I used to use a combination of what you have here and the solution DonnaZ has.

What mine would look like:

strSQL = "DECLARE email NVARCHAR(254) SET email='"& Request.Form("email")&"'"
strSQL = strSQL + " DECLARE band_name NVARCHAR(254) SET band_name)='" & Request.Form("band_name)&"'")
strSQL = strSQL + " INSERT INTO Table (email, band_name) VALUES (@Value1, @Value2);"

All that having been said: Pull your SQL OUT of your code and you will be much happier . I know there are folks who really, really like writing dynamic SQL inside their aps, I did it for several years, but once I went cold turkey, and pulled out as much of the SQL as possible, I had so much time on my hands I was stunned.

What I would do for this is to create a stored procedure for insert, update, delete. As an example, the INSERT sp might look like this:

DROP PROCEDURE spMySite_User_INSERT
GO
CREATE PROCEDURE spMySite_User_INSERT
@userEmail NVARCHAR(254),
@userName NVARCHAR(254)
AS
INSERT INTO TableUser (email, band_name) VALUES (@userEmail , @userName )
GO

Then in your code your SQL statement looks like this:

Connect.Execute "EXEC spMySite_User_INSERT @userEmail='"&strEmail&"', @userName='"&strBand_name&"';"

Cleaner, more elegant, and you can use that same pice of code over and over again, from any page you want OR any application you want.

Now in ASP.NET the whole thing gets even cooler, because you get to remove all the crud completely.

I build a class for dealing with data including all the connection crud, etc. and in that class have the methods such as INSERT, DELETE, UPDATE, etc. (most of the times it is a single method that dynamically figures our what you aretrying to do based upon table, data passed and a switch)

In an ASP.NET (using C#) page code I use a call like this:

string m = new sqlExecuteSP().spProcessINSERT("INSERT", strEmail, strBand_Name);

Done. The string returned tells me "Success" or "Failure";

That is all the code for it I have on a page : NO SQL!

Now, you do have to do some work on your sqlExecuteSP class, but that is peanuts compared to hand coding all that SQL in the application pages.

Alright, I'll shut up.
__________________
My grandfather used to work for your grandfather. Of course the rates have gone up.


Please login or register to view this content. Registration is FREE
yoemanspiffy is offline
Reply With Quote
View Public Profile Visit yoemanspiffy's homepage!
 
Old 08-25-2006, 09:08 AM Re: Help using SQL insert with variables
saadatshah's Avatar
Extreme Talker

Posts: 215
Name: Syed Saadat Ali
Location: Lahore, Pakistan
Trades: 0
print your variables to find out the exact query made & to track errors
__________________
- -- --- ---- ----- ------ ------- ---------------
If you have knowledge, let others light their candles in it.
saadatshah is offline
Reply With Quote
View Public Profile Visit saadatshah's homepage!
 
Old 09-05-2006, 05:36 PM Re: Help using SQL insert with variables
Super Talker

Posts: 116
Trades: 0
Thanks to everyone!

I printed everything out (including how to catch the SQL errors) - excellent.

DonnaZ
DonnaZ is offline
Reply With Quote
View Public Profile
 
Old 09-05-2006, 11:18 PM Re: Help using SQL insert with variables
ADAM Web Design's Avatar
Canadastaninianite

Posts: 5,938
Name: Adam for web page design, not program
Location: Toronto, Ontario, Canada
Trades: 0
I've got a function for this specific purpose that I created. It not only solves this issue, but (even though I never intended for it at the time), it solves the issue of SQL Injection as well.
Code:
function SQLComply (Term)
 Term = trim (Term)
 if Term <> "" then
  Term = Replace (Term, chr (39), chr (39) & chr (39))
 end if
 SQLComply = Term
end function
so strName = SQLComply (Request.Form ("name")) would work just fine. And it works with any database without the need for a stored procedure.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
(my blog)


Please login or register to view this content. Registration is FREE
(with proof)
ADAM Web Design is offline
Reply With Quote
View Public Profile Visit ADAM Web Design's homepage!
 
Reply     « Reply to Help using SQL insert with variables
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24063 seconds with 12 queries