0

Here is the query:

INSERT INTO `users` (username, password, email) VALUES ('testu', 'testp', 'teste')

I am getting this error with the following VB code (ASP):

    username = Request.Form("username")
    password = Request.Form("password")
    email = Request.Form("email")

    Dim conn As OleDbConnection = New OleDbConnection(GetConnectionString())
    Dim comm As OleDbCommand = New OleDbCommand("INSERT INTO users (username, password, email) VALUES ('" & username & "', '" & password & "', '" & email & "')", conn)

    conn.Open()
    comm.ExecuteNonQuery()

What is wrong with this statement? I know the connection string is correct because I have used it throughout the rest of my application.

Jason Plank
  • 2,322
  • 4
  • 29
  • 39
Logan Serman
  • 27,645
  • 25
  • 100
  • 139

2 Answers2

2

Agree! Always use Parameter (prepared) query and hash salt your password. Read SO thread - Salting Your Password: Best Practices? .

 Dim conn As OleDbConnection = New OleDbConnection(GetConnectionString())
 Dim comm As OleDbCommand = New OleDbCommand("INSERT INTO users (username, [password], email) VALUES (@username,@password,@email)",conn)
 comm.Parameters.Add("@username",OleDbType.Varchar,30).Value=username
 ....
Community
  • 1
  • 1
kv-prajapati
  • 90,019
  • 18
  • 141
  • 178
  • Like I said it is for a very small school project. I am aware of prepared queries and salting. The problem here is the query I mentioned not working, and I do not believe using parameters will solve that (but I could be wrong). – Logan Serman Dec 06 '11 at 12:37
  • @LoganSerman - Password is reserved word of access database engine so you have to escape. See the edited post. http://office.microsoft.com/en-us/access-help/access-2007-reserved-words-and-symbols-HA010030643.aspx – kv-prajapati Dec 06 '11 at 12:47
  • Thank you, AVD. That was indeed the problem. – Logan Serman Dec 06 '11 at 12:48
1

Don't EVER pass parameters to SQL commands with concatenation! This is an open way for SQL injection attack.

You should use placeholders for parameters and add actual values using comm.AddParameter().

As for your error - I'd expect some of your parameter values contains a single quote (').

Sergey Kudriavtsev
  • 9,560
  • 3
  • 40
  • 61