2015-10-04 73 views
0

我目前遇到了一个问题,我实际上无法解决,我在互联网上进行了研究,但是我发现的建议只是不提供,为我工作。尝试向MySQL中插入数据时,“您的SQL语法有错误”

我试图做一个注册系统,只是为了好玩,没有什么严重的,但我很乐意想知道我做错了什么,因为我大概可以使用这一天。

screenshot of the error

编辑:我从@Gordon和许多其他的建议解决方案这可能会更加有用,我会考虑尝试所有的建议。

非常感谢你=)

而且我的代码:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    mysqlconn = New MySqlConnection 
    mysqlconn.ConnectionString = "server=localhost;userid=root;password=Emil8250;database=Mysql" 
    Dim Reader As MySqlDataReader 

    If TxT_User.Text = "" Or TxT_Pass1.Text = "" Or TxT_Pass2.Text = "" Or TxT_Email.Text = "" Or TxT_SC.Text = "" Then 
     MsgBox("You need to fill out the required informations above", MsgBoxStyle.Critical) 
    Else 
     If TxT_Email.Text.Contains("@") Then 
      If TxT_Pass2.Text = TxT_Pass1.Text Then 
       Try 
        mysqlconn.Open() 
        Dim Query As String 
        Query = "INSERT INTO syntax.members (Username, Password, Email, Secret Answer) VALUES ('" & TxT_User.Text & "', '" & TxT_Pass1.Text & "', '" & TxT_Email.Text & "', '" & TxT_SC.Text & "')" 
        cmd = New MySqlCommand(Query, mysqlconn) 
        Reader = cmd.ExecuteReader 

        MsgBox("Account created!", MsgBoxStyle.Information) 

        mysqlconn.Close() 
       Catch ex As MySqlException 
        MsgBox(ex.Message, MsgBoxStyle.Information) 
       Finally 
        mysqlconn.Dispose() 
       End Try 
      Else 
       MsgBox("Your Password doesn't match the retyped version", MsgBoxStyle.Critical) 
      End If 

     Else 
      MsgBox("Your email is not valid, please type a valid email address", MsgBoxStyle.Critical) 
     End If 
    End If 
End Sub 
+0

在尝试执行并在此处共享之前,您可以打印“Query”的内容吗? – Mureinik

+0

你想要MySQL中的值吗?我如何将查询信息完全打印给您? – GuestAnon

+2

虽然没有直接回答你的问题,但我建议在进一步学习之前,立即摆脱编写使用字符串连接的VB/SQL代码的习惯。这是一个巨大的安全漏洞,也是非常糟糕的做法。相反,将参数添加到您的SQL命令。这不仅更安全,而且会产生更强大的代码。 – Lunster

回答

0

一个明显的问题是 “机密答案” 在这里:

INSERT INTO syntax.members (Username, Password, Email, Secret Answer) . . . 

不幸的是,我不知道是哪个你想要:

INSERT INTO syntax.members (Username, Password, Email, `Secret Answer`) . . . 
INSERT INTO syntax.members (Username, Password, Email, SecretAnswer) . . . 
INSERT INTO syntax.members (Username, Password, Email, Secret, Answer) . . . 

从价值清单看,它是前两种之一。

+0

那么,在我的SQL中,列名= Secret Answer,是否会成为造成问题的“空间”? – GuestAnon

+0

@GuestAnon。 。 。空间是问题。第一种选择是正确的解决方案。但是,最好有不需要转义的列名(通常只有字母数字字符和下划线,并且不匹配MySQL保留字)。 –

+0

我在哪里可以找到MySQL保留字?所以我确定我不会再做任何愚蠢的事情? – GuestAnon

相关问题