2015-10-04 70 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

相關問題