1

だから私はRuby/Sinatraでログイン/サインアップページを構築しています.誰かが使用中の電子メールでサインアップしようとすると、そのことを伝えて許可しないようにロジックを追加しようとしています.サインアップします

require 'rubygems'
require 'sinatra'
require 'mysql'

get "/" do
        erb :form
end

post "/" do

begin
        con = Mysql.new('localhost', 'tut', 'tut', 'recruited_users')
        auth = con.query('SELECT school FROM users WHERE email = "#{params[:email]}" AND password = "#{params[:password]}"')
        auth.fetch_row
        ensure
                con.close if con
        end
end

get '/signup' do
        erb :signup
end

post '/signup' do

begin
        con = Mysql.new('localhost', 'tut', 'tut', 'recruited_users')
        check_for_user = con.query("SELECT email FROM users WHERE email = '#{params[:email]}'")
        if check_for_user == ''
                "Sorry, but there is already an account for this user. The ID is '#{params[:check_for_user]}', please try again"
        else
                auth = con.query("INSERT INTO users (email, password, school) VALUES('#{params[:email]}', '#{params[:password]}', '#{params[:school]}')")
                "Succesfully created user #{params[:email]}"
        end
        ensure
                con.close if con
        end
end

問題は、変数check_for_userが値を受け取っていないことです。少なくとも、私が操作できる値ではありません。メールがデータベースにまだ存在しない場合にのみ新しいユーザーを作成できるように、if ステートメントを設定できるようにする必要があります。

4

1 に答える 1

3

まず第一に#{...}、単一引用符で囲まれた文字列内で文字列補間 ( ) を使用することはできません。これは、二重引用符で囲まれた文字列 (または二重引用符で囲まれた文字%Q{...}列のように動作するもの) でのみ機能します。次に、SQL の文字列リテラルは一重引用符で囲む必要があります。MySQL と SQLite では二重引用符を使用できますが、これは悪い習慣です。第三に、1999 年に PHP をハッキングしていないので、文字列補間を使用して SQL を構築するべきではなく、プレースホルダーを使用する必要があります。

sth = con.prepare('select school from users where email = ? and password = ?')
sth.execute(params[:email], params[:password])
row = sth.fetch
if(!row)
    # Didn't find anything
else
    # The school is in row.first
end
于 2012-09-03T05:02:23.857 に答える