Results 1 to 7 of 7

Thread: Mysql Register Button insert into

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    4

    Mysql Register Button insert into

    Hey, I'm new to the forums and well I've managed to make a login user form that uses mysql database.

    now i wanted to make a register form as well, but i can't seem to figure out how i do that.

    so, can anyone help me out?

    here's my login form.

    Code:
    My.Computer.Audio.Play(My.Resources.button1, AudioPlayMode.Background)
            Dim conn As MySqlConnection
            'connect to DB
            conn = New MySqlConnection()
            conn.ConnectionString = "server=127.0.0.1; user id=root; password=test; database=users"
            'see if connection failed.
            Try
                conn.Open()
            Catch myerror As MySqlException
                MessageBox.Show("Error Connecting to Database")
                Close()
            End Try
            'sql query
            Dim myAdapter As New MySqlDataAdapter
    
            Dim sqlquery = "SELECT username, password FROM accounts Where username='" & TextBox1.Text & "' and password='" & TextBox2.Text & "'"
            Dim myCommand As New MySqlCommand()
            myCommand.Connection = conn
            myCommand.CommandText = sqlquery
            'start query
            myAdapter.SelectCommand = myCommand
            Dim myData As MySqlDataReader
            myData = myCommand.ExecuteReader()
            'if nothing is entered into the textboxes
            If TextBox1.Text = "" Then
                MsgBox("Please Enter your Username")
                My.Computer.Audio.Play(My.Resources.button1, AudioPlayMode.Background)
            End If
            If TextBox2.Text = "" Then
                My.Computer.Audio.Play(My.Resources.button1, AudioPlayMode.Background)
                MsgBox("Please Enter your Password")
            Else
            End If
    
            'see if user exits.
            If myData.HasRows = 0 Then
                MessageBox.Show("The information that has been entered appears to be invalid.", "Vbphysics", MessageBoxButtons.OK, MessageBoxIcon.Error)
                My.Computer.Audio.Play(My.Resources.button1, AudioPlayMode.Background)
            Else
                Dim form2 = New Form2
                form2.Show()
                Me.Visible = False
            End If
        End Sub

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Mysql Register Button insert into

    Try something like this for your login form. As you've written it, I can hack your program by typing in one line of text into TextBox1.

    Code:
            Dim validuser As Boolean
    
            'no matter what you do, I play this sound, so just play it here.
            My.Computer.Audio.Play(My.Resources.button1, AudioPlayMode.Background)
    
            'if nothing is entered into the textboxes
            'check this before you do anything with the database!
            If TextBox1.Text.Equals(String.Empty) Then
                MessageBox.Show("Please Enter your Username")
                Exit Sub
            End If
            If TextBox2.Text.Equals(String.Empty) Then
                MessageBox.Show("Please Enter your Password")
                Exit Sub
            Else
            End If
    
            'by using the "Using" structure, my connection is cleaned up nicely when done.
            Using conn As New MySqlConnection("server=127.0.0.1; user id=root; password=test; database=users")
                'see if connection failed.
                Try
                    conn.Open()
                Catch myerror As MySqlException
                    MessageBox.Show("Error Connecting to Database")
                    Me.Close()
                    Exit Sub
                End Try
                'sql query
    
                Dim sqlquery = "SELECT username, password FROM accounts Where username=?USER and password=?PASS; "
                Dim myCommand As New MySqlCommand(sqlquery, conn)
                'if you don't use parameters, anyone can hack the system with ease!  no more annoying quote problems either!
                myCommand.Parameters.AddWithValue("USER", TextBox1.Text)
                myCommand.Parameters.AddWithValue("PASS", TextBox2.Text)
    
                Dim myData As MySqlDataReader = myCommand.ExecuteReader()
    
                'see if user exits.
                If Not myData.HasRows Then
                    MessageBox.Show("The information that has been entered appears to be invalid.", "Vbphysics", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    validuser = False
                Else
                    Dim f As New Form2
                    f.Show()
                End If
    
                'cleanup my junk.
                myData.Close()
                myCommand.Dispose()
                conn.Close()
            End Using
            If validuser Then
                Me.Close()
            End If
        End Sub
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Mysql Register Button insert into

    So, what are you actually saying? That you don't know how to insert data into a database, or that you don't know how to gather the data to insert?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    4

    Re: Mysql Register Button insert into

    Quote Originally Posted by jmcilhinney View Post
    So, what are you actually saying? That you don't know how to insert data into a database, or that you don't know how to gather the data to insert?
    Both of those, the only thing i can do with mysql is work with login forms, providing i get help to code it.

    I'm a slow leaner, due to my intelligence.

    Thanks for the help guys, really appreciate it.

  5. #5
    Member
    Join Date
    Jan 2010
    Posts
    36

    Re: Mysql Register Button insert into

    This is a samply of the code I used:

    vb.net Code:
    1. Private Sub addUserToDatabase(ByVal user1 As String, ByVal password1 As String, ByVal admin1 As Boolean, ByVal server1 As Integer)
    2.         Try
    3.             Dim sqlCommand As String = "INSERT INTO `userdatabase`.`users` (`user_id`, `user_name`, `user_password`, `server_id`, `admin_flag`) VALUES (NULL, '" & user1 & "', '" & password1 & "', '" & server1 & "', '" & admin1 & "')"
    4.             sqlInsertData(sqlCommand)
    5.         Catch ex As Exception
    6.         End Try
    7.        
    8.  
    9.     End Sub


    Then: for the actual inserting of data, the important part is the myCommand.ExecuteNonQuery()

    vb.net Code:
    1. Private Sub sqlInsertData(ByVal cmd As String)
    2.         Dim conn As New MySqlConnection
    3.         Dim myCommand As New MySqlCommand
    4.         conn.ConnectionString = "server=" & txtServer & ";" _
    5.             & "user id=" & txtUsername & ";" _
    6.            & "password=" & txtPassword & ";" _
    7.         & "database=" & txtDatabase
    8.         myCommand.Connection = conn
    9.         myCommand.CommandText = cmd
    10.         Try
    11.             conn.Open()
    12.             myCommand.ExecuteNonQuery()
    13.         Catch myerror As MySqlException
    14.             MsgBox("There was an error updating the database: " & myerror.Message)
    15.         End Try
    16.         conn.Close()
    17.     End Sub


    As stated above though you should use the "Using, End Using", it is something I will make changes to on my own application.



    Some sample code I used for getting user data from the database:

    vb.net Code:
    1. Public Sub readData()
    2.         Dim dataReader As DataTableReader
    3.         Dim getUserData As String = "SELECT `users`.`user_name`'User Name', `users`.`user_password`'Password', `users`.`admin_flag` 'Admin' FROM `users` WHERE ( `users`.`server_id` = '" & serverId & "')"
    4.         runSqlQuery(getUserData)
    5.         dataReader = myData.CreateDataReader
    6.         Do While dataReader.Read()
    7.             Dim name As String = CStr(dataReader(0))
    8.             Dim passwd As String = CStr(dataReader(1))
    9.             Dim admin As Boolean = CBool(dataReader(2))
    10.             Try
    11.                 'do stuff with your data here
    12.             Catch ex As Exception
    13.             End Try
    14.         Loop

    This SQL query code:

    vb.net Code:
    1. Private Sub runSqlQuery(ByVal cmd As String)
    2.         Try
    3.             conn.ConnectionString = "server=" & txtServer & ";" _
    4.           & "user id=" & txtUsername & ";" _
    5.          & "password=" & txtPassword & ";" _
    6.       & "database=" & txtDatabase & ";" _
    7.       & "Encrypt=true;"
    8.             conn.Open()
    9.         Catch myerror As MySqlException
    10.         End Try
    11.         myAdapter.SelectCommand = myCommand
    12.         myCommand.Connection = conn
    13.         myCommand.CommandText = cmd
    14.         myAdapter.Fill(myData)
    15.         conn.Close()
    16.         conn.Dispose()
    17.     End Sub
    Last edited by Suterusu; Feb 9th, 2010 at 08:05 AM.

  6. #6

    Thread Starter
    New Member
    Join Date
    Dec 2009
    Posts
    4

    Re: Mysql Register Button insert into

    This is nice and i appreciate it but it's not very explanatory.

    So something else, would be highly appreciated.

  7. #7
    Member
    Join Date
    Jan 2010
    Posts
    36

    Re: Mysql Register Button insert into

    What parts don't you understand? <.<

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width