I learn about RoR / databases, and this topic particularly confused me. In an Agile Development book with Rails 4, they provide an example of finding a list of all orders for an entry named Dave:
pos = Order.where("name = 'Dave' and pay_type = 'po")
The book goes on to say that you'll never want to do something like this:
name = params[:name]
pos = Order.where("name = '#{name}'and pay_type = 'po'")
Instead, you should do this:
name = params[:name]
pos = Order.where(["name = ? and pay_type = 'po'",name])
I understand that SQL injection is a concept, but there are a few details that confuse me. First, how exactly does SQL injection work as syntax.
I understand that the danger is that someone can delete the table / database if you interpolate the external form parameter as the first example, but how?
Let's say you had this:
name = params[:name]
pos = Order.where("name = '#{DROP DATABASE database_name}'and pay_type = 'po'")
SQL-? SQL , , "name = DROP DATABASE database_name", , ?
, . , , .
name = params[:name]
pos = Order.where(["name = ? and pay_type = 'po'", DROP DATABASE database_name])
DROP DATABASE, , ? SQL? http://hub.tutsplus.com/ Google, . ?