热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

将变量赋给mysql查询ruby-Assignvariabletomysqlqueryruby

SoImbuildingaloginsignuppageinRubySinatra,andImtryingtoaddsomelogicsothatifsom

So I'm building a login/signup page in Ruby/Sinatra, and I'm trying to add some logic so that if someone tries to sign up with an email that is in use, it will tell them so, and not allow them to sign up

所以我正在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

The problem is that the variable check_for_user is not receiving any value, at least not one that I can work with. I need to be able to set up the if statement so that they can only create a new user if the email does not already exist in the database.

问题是变量check_for_user没有收到任何值,至少没有我可以使用的值。我需要能够设置if语句,以便只有在数据库中尚不存在电子邮件时才能创建新用户。

1 个解决方案

#1


3  

First of all, you can't use string interpolation (#{...}) inside a single quoted string, that only works with double quoted strings (or things like %Q{...} that behave like double quoted strings). Secondly, string literals in SQL should be quoted with single quotes, MySQL and SQLite let you get away with double quotes but that's a bad habit. Thirdly, we're not hacking PHP in 1999 so you shouldn't be using string interpolation to build SQL, you should use placeholders:

首先,你不能在单个带引号的字符串中使用字符串插值(#{...}),它只适用于双引号字符串(或类似于双引号字符串的%Q {...}之类的东西) 。其次,SQL中的字符串文字应该用单引号引用,MySQL和SQLite让你得到双引号,但这是一个坏习惯。第三,我们不是在1999年破解PHP,所以你不应该使用字符串插值来构建SQL,你应该使用占位符:

sth = con.query('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

推荐阅读
  • 本文详细介绍了如何通过多种编程语言(如PHP、JSP)实现网站与MySQL数据库的连接,包括创建数据库、表的基本操作,以及数据的读取和写入方法。 ... [详细]
  • 本文详细探讨了不同SQL数据库管理系统(DBMS)在限制输出结果、拼接字段和日期时间处理方面的函数差异。通过具体示例,帮助读者理解并掌握如何在不同DBMS中实现相同功能。 ... [详细]
  • PHP 编程疑难解析与知识点汇总
    本文详细解答了 PHP 编程中的常见问题,并提供了丰富的代码示例和解决方案,帮助开发者更好地理解和应用 PHP 知识。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • 本文深入探讨 MyBatis 中动态 SQL 的使用方法,包括 if/where、trim 自定义字符串截取规则、choose 分支选择、封装查询和修改条件的 where/set 标签、批量处理的 foreach 标签以及内置参数和 bind 的用法。 ... [详细]
  • MySQL 用户创建失败的解决方案
    本文详细介绍了在 MySQL 中遇到用户创建失败问题时的解决方法,包括如何正确配置环境、执行命令以及常见错误排查技巧。通过逐步指导,帮助用户顺利添加和管理 MySQL 用户。 ... [详细]
  • MySQL 数据库迁移指南:从本地到远程及磁盘间迁移
    本文详细介绍了如何在不同场景下进行 MySQL 数据库的迁移,包括从一个硬盘迁移到另一个硬盘、从一台计算机迁移到另一台计算机,以及解决迁移过程中可能遇到的问题。 ... [详细]
  • 本文详细介绍了 MySQL 中 LAST_INSERT_ID() 函数的使用方法及其工作原理,包括如何获取最后一个插入记录的自增 ID、多行插入时的行为以及在不同客户端环境下的表现。 ... [详细]
  • MongoDB的核心特性与架构解析
    本文深入探讨了MongoDB的核心特性,包括其强大的查询语言、灵活的文档模型以及高效的索引机制。此外,还详细介绍了MongoDB的体系结构,解释了其文档、集合和数据库的层次关系,并对比了MongoDB与传统关系型数据库(如MySQL)的逻辑结构。 ... [详细]
  • PHP 5.2.5 安装与配置指南
    本文详细介绍了 PHP 5.2.5 的安装和配置步骤,帮助开发者解决常见的环境配置问题,特别是上传图片时遇到的错误。通过本教程,您可以顺利搭建并优化 PHP 运行环境。 ... [详细]
  • 尝试执行数据库模式加载时遇到错误'Mysql2::Error: 指定的键太长;最大键长度为767字节'。本文将探讨这一问题的成因及解决方案。 ... [详细]
  • 在寻找轻量级Ruby Web框架的过程中,您可能会遇到Sinatra和Ramaze。两者都以简洁、轻便著称,但它们之间存在一些关键区别。本文将探讨这些差异,并提供详细的分析,帮助您做出最佳选择。 ... [详细]
  • 本文详细介绍了在使用FAPlayer的编译脚本时遇到的Ruby依赖问题,并提供了在Cygwin环境下安装Ruby的具体步骤。 ... [详细]
  • Linux环境下Redmine快速搭建指南
    本文将详细介绍如何在Linux操作系统中使用Bitnami Redmine安装包快速搭建Redmine项目管理平台,帮助读者轻松完成环境配置。 ... [详细]
author-avatar
手机用户2502878095
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有