Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Trouble with query update

Author  Topic 

mastajbl
Starting Member

42 Posts

Posted - 2009-05-04 : 14:20:54
I am trying to update the password field when a user needs to change their password. Now i'm not sure if I am going about this the right way, but this is what i have. The problem occuring is that it is saying Unterminated string constant.

query = "UPDATE login (Password) VALUES "& Request.Cookies("Password")"
conn.Execute (query)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-04 : 14:34:36
Your code is vulnerable to SQL injection. Do not concatenate like that, instead use parameterized queries.

Here's what a valid UPDATE statement looks like:
UPDATE login
SET password = 'SomeValue'
WHERE loginId = SomeValue

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

mastajbl
Starting Member

42 Posts

Posted - 2009-05-04 : 14:41:35
Even though the user will be sending this via a web application?? Let me post my whole code so you have a better idea as to what i'm doing mayb...not sure I explained or gave you enough details. It's vbscript, but the part i am having problems with is my update query section as posted previously.

<%

Username = Request.Cookies("userID")
Password = Request.Form("Pass1")
Response.Cookies("Password")= Request.Form("Pass1")

if UserName<>"" then
if Password<>"" then
set conn = Server.CreateObject ("ADODB.Connection")
conn.Open Application("connString")
query = "SELECT [Password] FROM Login WHERE userID='" & UserName & "' AND Password='" & Password & "'"
set rs = conn.Execute (query)

' Check if the user and password are valid
if rs.eof then
' There is no record to match the UserName and Password
Response.Write "<p class=errmsg>Invalid user and password!</p>"
conn.Close
else
' User login ok and set the user_id value from the Members table in a session variable
Response.Cookies("chgPassword") = rs("chgPassword")
rs.close
set rs = nothing

' Insert a new password in the login table with the password.
query = "UPDATE login (Password) VALUES ("& Request.Cookies("Password")")"
conn.Execute (query)

conn.close
set conn = nothing

Response.Redirect "loginauth2.asp"
end if
else
Response.Write "<p class=errmsg>Please fill both fields.</p>"
end if
end if

%>
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2009-05-04 : 14:47:06
You shouldn't contact strings like that. But it looks like you update sytax is invalid. Try:
query = "UPDATE login SET Password = '" & Request.Cookies("Password") & "' WHERE userID='" & UserName & "'"
EDIT: Good call Tara. I forgot about the WHERE clause.

Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-04 : 14:47:45
A web application is exactly where you'd run into SQL injection. You need to do some research to prevent your system from being compromised.

I'm not a VBScript developer, so I'm unable to help you specifically with that code, but your UPDATE statement does not look like valid T-SQL syntax.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-04 : 14:48:19
I think you're going to need a WHERE clause on that query, otherwise you are updating all rows!

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

mastajbl
Starting Member

42 Posts

Posted - 2009-05-04 : 14:54:45
yea it appears you're right...I missed a few thing in there. Does SQL Injection apply to access databases as well? It appears I have some reading to do in the meantime.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-04 : 14:58:37
SQL injection is not limited to Microsoft SQL Server. It can happen on Microsoft Access, Oracle, MySql, ...

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

mastajbl
Starting Member

42 Posts

Posted - 2009-05-04 : 15:12:56
Well after doing some reading I can see how the code is vulnerable, however it is run entirely on server side so i dont see how you could inject coding into it??

On the other hand I am receiving a new error.

[Microsoft][ODBC Microsoft Access Driver] Syntax error in UPDATE statement.
It is happening when at the conn.Execute

query = "UPDATE login WHERE userID='" & UserName &"' SET Password = " & Request.Cookies("chgPassword")
conn.Execute (query)
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-04 : 15:14:20
The WHERE clause goes at the end. See my example I provided earlier.

You said this is from a web application, and that's where SQL injection is likely to happen.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

mastajbl
Starting Member

42 Posts

Posted - 2009-05-04 : 15:27:47
Hmmm..it definitely update my database for my userid, but it removed my password without inputing my new one...any idea on that??
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-05-04 : 15:30:03
If it didn't update the password with the correct data, then something is wrong with "Request.Cookies("chgPassword")".

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -