0

I'm having problems with my insert statement:

Create = function (LN,FN,Add,Tel,Cell)
    LastName = tostring(LN);
    FirstName = tostring(FN);
    Address = tostring(Add);
    Telephone =tostring(Tel); 
    Cellphone = tostring(Cell);

--source of the problem

conn:execute([[INSERT INTO book(LastName, FirstName, Address, Telephone, Cellphone) VALUES ("]]"'"LastName"','"FirstName"','" Address"','" Telephone"','" Cellphone")]]'")

 print ("\n Creating an account Successful")
 end 
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • `conn:execute([[INSERT INTO book(LastName, FirstName, Address, Telephone, Cellphone) VALUES (']]..LastName.."','"..FirstName.."','"..Address.."','"..Telephone.."','"..Cellphone.."')")` – Egor Skriptunoff Apr 18 '14 at 07:54
  • 2
    Please look into prepared statements and data binding. String concatenation is less efficient and leads to injection attacks. – Colonel Thirty Two Apr 18 '14 at 11:53

1 Answers1

3

I'd suggest that you use the string.format for placing the data:

Create = function (LN,FN,Add,Tel,Cell)
    local LastName, FirstName, Address, Telephone, Cellphone = tostring(LN), tostring(FN), tostring(Add), =tostring(Tel), tostring(Cell)
    local sQuery = [[INSERT INTO book(LastName, FirstName, Address, Telephone, Cellphone) VALUES ('%s', '%s', '%s', '%s', '%s')]]
    conn:execute( sQuery:format(LastName, FirstName, Address, Telephone, Cellphone) )
hjpotter92
  • 78,589
  • 36
  • 144
  • 183