تقييم الموضوع :
  • 0 أصوات - بمعدل 0
  • 1
  • 2
  • 3
  • 4
  • 5
[سؤال] هل من الممكن جلب مواضيع عضو معين من قاعدة بيانات mysql
#1
السلام عليكم

هل هناك كود لجلب بيانات العضو
مثال : العضو يقوم بالتسجيل عن طريق البرنامج
وعندما يقوم بالدخول تظهر فقط مواضيعه
يعني لا تظهر مواضيع الاعضاء الاخرين
فقط يرى مواضيعه الخاصه

انا استخدم هذا الكود

PHP كود :
Imports MySql.Data.MySqlClient

Public Class Form1
    
'Represents an SQL statement or stored procedure to execute against a data source.
    Dim cmd As New MySqlCommand
    Dim da As New MySqlDataAdapter
    '
declare conn as connection and it will now a new connection because 
    
'it is equal to Getconnection Function
    Dim con As MySqlConnection = jokenconn()

    Public Function jokenconn() As MySqlConnection
        Return New MySqlConnection("Server=mysite.com;Port=3306;Database=mydata;Uid=myuser;Pwd=mypass;")
    End Function
    Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click
        Dim sql As String
        Dim publictable As New DataTable
        Try
            '
check if the textbox is equal to nothing then it will display the message below!.
            If 
txtuname.Text "" And txtpass.Text "" Then
                MsgBox
("Password or Username Incorrect!")

            Else
                
sql "select * from users where username ='" txtuname.Text "' and password = '" txtpass.Text "'"
                'bind the connection and query
                With cmd
                    .Connection = con
                    .CommandText = sql
                End With
                da.SelectCommand = cmd
                da.Fill(publictable)
                '
check if theres a result by getting the count number of rows
                
If publictable.Rows.Count 0 Then

                    
'it gets the data from specific column and assign to the variable
                    Dim user_type, name As String
                    user_type = publictable.Rows(0).Item(1)
                    name = publictable.Rows(0).Item(2)
                    '
check if the type of user is admin
                    
If user_type "admin" Then
                        
'welcomes the user as Admiistrator
                        MsgBox("Welcome " & name & " you login as Administrator ")
                        '
set the lbllogin text to Logout 
                        lbllogin
.Text "Logout"
                        'disabled the groupbox
                        GroupBox1.Enabled = False
                        '
reset all the two textbox
                        txtuname
.Text ""
                        
txtpass.Text ""
                        'set the lblname to the specific user
                        lblname.Text = "Hi, " & name

                    ElseIf user_type = "Encoder" Then
                        MsgBox("Welcome " & name & " you login as Encoder ")
                        lbllogin.Text = "Logout"
                        GroupBox1.Enabled = True
                        txtuname.Text = ""
                        txtpass.Text = ""
                        lblname.Text = "Hi, " & name
                    Else
                        MsgBox("You login as Guest!")
                        lbllogin.Text = "Logout"
                        GroupBox1.Enabled = False
                        txtuname.Text = ""
                        txtpass.Text = ""
                        lblname.Text = "Hi, " & name
                        lbllogin.Enabled = True
                        DataGridView1.Visible = True
                    End If

                Else
                    MsgBox("Contact administrator to registered!")
                    txtuname.Text = ""
                    txtpass.Text = ""
                End If

                da.Dispose()
            End If

        Catch ex As Exception
            MsgBox(ex.Message)

        End Try
        con.Clone()

       
    End Sub

    Private Sub lbllogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbllogin.Click

        If lbllogin.Text = "Logout" Then
            lbllogin.Text = "Login"
            lblname.Text = "Hi, Guest!"
        ElseIf lbllogin.Text = "Login" Then
            GroupBox1.Enabled = True

        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GroupBox1.Enabled = True
        lbllogin.Enabled = False
        Try
            Using da As New MySqlDataAdapter("Select * from `comments`", con)
                Using dt As New DataTable
                    da.Fill(dt)
                    DataGridView1.DataSource = dt
                    If DataGridView1.Rows.Count > 0 Then
                        DataGridView1.Columns(0).HeaderText = "ID"
                        DataGridView1.Columns(0).Width = 50
                        DataGridView1.Columns(1).HeaderText = "added by"
                        DataGridView1.Columns(1).Width = 150
                        DataGridView1.Columns(2).HeaderText = "Name Thread"
                        DataGridView1.Columns(2).Width = 200
                        DataGridView1.Columns(3).HeaderText = "Notes"
                        DataGridView1.Columns(3).Width = 200
                       
                    End If
                End Using
            End Using



        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class 


هناك جدولين في قاعدة البيانات
comments
users

البيانات الموجوده في جدول comments

post_id username title message



البيانات الموجوده في جدول users

id username password



اتمنى سؤالي واضح

شكرا لكم
الرد }}}
تم الشكر بواسطة: Sajad , Sajad
#2
مفترض في جدول comments تضع رقم المستخدم id بدلا عن اسمه username حتى تستجلب مواضيعه بسهولة

وجملة sql لجلب مواضيع العضو كالتالي:
PHP كود :
"Select * from comments Where id='userID' '" 
(وَقُل رَّبِّ زِدْنِي عِلْمًا)

الرد }}}
تم الشكر بواسطة: المشاعر
#3
حاولت اعملها ماصار

يظهر معاي خطأ


الملفات المرفقة صورة/صور
   
الرد }}}
تم الشكر بواسطة:
#4
لماذا لاتستخدم العلاقات (relationship) ؟



وبالتالي كل User سيصبح عنده collection وفي داخل الcollection يُوجد كل الcomments الخاصة بالuser


لاحظ كيف الموضوع بيصير سهل سواء في الحفظ او في عرض البيانات-

الرد }}}
تم الشكر بواسطة: kslawy
#5
أولا اشكركم على مساعدتي
ولكن للأسف لم افهم واحب اعتذر لكم
وأتمنى احد منكم ان يقوم بتعديل مشروعي

المشروع مرفع مع الشكر الجزيل للقائمين على الموقع


الملفات المرفقة
.rar   Login.rar (الحجم : 120.02 ك ب / التحميلات : 67)
الرد }}}
تم الشكر بواسطة:
#6
.....

تم تعديل الكود للفورم1

كما ذكر الأستاذ kslawy استخدم id المستخدم بدلا من اسمه وهو الأسلوب الصحيح

على افتراض ان:
الجدول users له الحقول التالية
id رقم
username نص
password نص
usertype نص

الجدول comments له الحقول التالية
post_id رقم
userid رقم
title نص
message نص

تم تجاهل أكواد Button1 وما بعدها
كود :
Imports MySql.Data.MySqlClient

Public Class Form1

    Private conString As String = "Server=localhost; Database=oman ;Uid=root; Pwd=;"


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        GroupBox1.Enabled = True
        lbllogin.Enabled = False

    End Sub


    Private Sub btnlogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlogin.Click

        'check if the textbox is equal to nothing then it will display the message below!.
        If Me.txtuname.Text = "" And Me.txtpass.Text = "" Then
            MsgBox("Password or Username Incorrect!")
            Exit Sub
        End If

        Try

            Using con As New MySqlConnection(conString)

                Dim sql As String = "SELECT * FROM `users` WHERE `username`=@username AND `password`=@password"
                Using da As New MySqlDataAdapter(sql, con)
                    da.SelectCommand.Parameters.AddWithValue("@username", Me.txtuname.Text)
                    da.SelectCommand.Parameters.AddWithValue("@password", Me.txtpass.Text)

                    Dim dt As New DataTable
                    If da.Fill(dt) > 0 Then

                        Dim row As DataRow = dt.Rows(0)
                        Select Case row("usertype")

                            Case "admin"
                                'welcomes the user as Admiistrator
                                MsgBox("Welcome " & row("username") & " you login as Administrator ")
                                'set the lbllogin text to Logout
                                'disabled the groupbox
                                Me.GroupBox1.Enabled = False
                                'reset all the two textbox
                                'set the lblname to the specific user
                                Me.lblname.Text = "Hi, " & row("username")

                            Case "Encoder"
                                MsgBox("Welcome " & row("username") & " you login as Encoder ")
                                Me.GroupBox1.Enabled = True
                                Me.lblname.Text = "Hi, " & row("username")

                            Case Else
                                MsgBox("You login as Guest!")
                                Me.GroupBox1.Enabled = False
                                Me.lblname.Text = "Hi, " & row("username")
                                Me.lbllogin.Enabled = True
                                Me.DataGridView1.Visible = True

                        End Select

                        Me.lbllogin.Enabled = True
                        Me.lbllogin.Text = "Logout"

                        ViewUserComments(row("id"))

                    Else
                        MsgBox("Contact administrator to registered!")
                    End If

                    Me.txtuname.Text = ""
                    Me.txtpass.Text = ""

                End Using
            End Using


        Catch ex As Exception
            MsgBox(ex.Message)

        End Try



    End Sub


    Private Sub ViewUserComments(userid As String)
        Using con As New MySqlConnection(conString)
            Using da As New MySqlDataAdapter("SELECT * FROM `comments` WHERE `userid`=@userid ", con)
                da.SelectCommand.Parameters.AddWithValue("@userid", userid)
                Using dt As New DataTable
                    If da.Fill(dt) > 0 Then

                        DataGridView1.DataSource = dt

                        DataGridView1.Columns(0).HeaderText = "ID"
                        DataGridView1.Columns(0).Width = 50
                        DataGridView1.Columns(1).HeaderText = "added by"
                        DataGridView1.Columns(1).Width = 150
                        DataGridView1.Columns(2).HeaderText = "Name Thread"
                        DataGridView1.Columns(2).Width = 200
                        DataGridView1.Columns(3).HeaderText = "Notes"
                        DataGridView1.Columns(3).Width = 200

                        DataGridView1.Columns(1).DataGridView.Name = "& name"

                    End If

                End Using
            End Using
        End Using

    End Sub


    Private Sub lbllogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lbllogin.Click

        If Me.lbllogin.Text = "Logout" Then
            Me.DataGridView1.DataSource = Nothing
            Me.lbllogin.Enabled = False
            Me.lbllogin.Text = "Login"
            Me.lblname.Text = "Hi, Guest!"
            Me.GroupBox1.Enabled = True
        End If
    End Sub





    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button1.Click
        PrintDocument1.Print()
    End Sub
    Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage

        Dim bm As New Bitmap(Me.DataGridView1.Width, Me.DataGridView1.Height)

        DataGridView1.DrawToBitmap(bm, New Rectangle(0, 0, Me.DataGridView1.Width, Me.DataGridView1.Height))

        e.Graphics.DrawImage(bm, 0, 0)

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"

        Dim sql As String = "SELECT * FROM comments"



        Dim ds As New DataSet()


        DataGridView1.DataSource = ds

        DataGridView1.DataMember = "comments"

    End Sub


End Class

.....
الرد }}}
تم الشكر بواسطة: php3
#7
اشكرك اخي
فعلا هو الكود المطلوب

الان بقى علينا في ملفات البي اتش بي ورح اكتب موضوع هناك Smile
لان سابقا كنت اتعلم السيشن في لغة البي اتش بي وتشتت افكاري
الرد }}}
تم الشكر بواسطة:


المواضيع المحتمل أن تكون متشابهة .
الموضوع : الكاتب الردود : المشاهدات : آخر رد
  هل من الممكن العمل على قاعدة بيانات اكسل على الفيجوال بيسك خالد كامل1 4 142 24-03-24, 02:00 AM
آخر رد: خالد كامل1
  [سؤال] أتمنى اجابة وافية:عمل برنامج بقاعدة بيانات SQL دون حاجة تثبيت SQLSERVER dr.programming 6 652 19-03-24, 08:56 AM
آخر رد: سلامه محمد11
  استعلام select عن مجموعة بيانات Adel27213 1 210 15-03-24, 01:11 AM
آخر رد: justforit
  تعديل طول حقل معين في جميع جداول القاعدة atefkhalf2004 1 106 09-03-24, 03:17 AM
آخر رد: atefkhalf2004
  تعديل بيانات عمود DataGridView دفعة واحدة مصمم هاوي 2 194 05-03-24, 08:27 PM
آخر رد: مصمم هاوي
  [VB.NET] تحديث بيانات فى كلاس من جدول سيكوال asdfar1977 6 196 26-02-24, 01:07 AM
آخر رد: asdfar1977
  كيفية جمع أرقام عمود فى الداتا جريد فيو بناءاً على بيانات معينة heem1986 5 236 25-02-24, 11:26 PM
آخر رد: heem1986
  [C#.NET] طباعة بيانات داتا جرد فيو h2551996 0 94 25-02-24, 02:31 PM
آخر رد: h2551996
  [VB.NET] معرفة بيانات الجهاز محمد بوقزاحة 1 326 21-02-24, 11:44 PM
آخر رد: AHMED213
  كيفية انشاء سكريبت لقاعدة بيانات من خلال الكود heem1986 1 213 20-02-24, 12:00 AM
آخر رد: Kamil

التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم