我有没有在脚下射击......这就是问题所在!

时间:2011-04-02 20:24:34

标签: c# asp.net mysql sql

嘿,伙计们,我认为我只是在自己的脚下开枪。我有一个网站,用户有他们自己的个人资料,可以发布在他们的墙上,但我已经到了我想要实现的点,朋友可以在我的墙上张贴,我可以发布在我的朋友墙上。

这是我的网站到目前为止的样子:

enter image description here

我的数据库设计是这样的:

enter image description here

以下是我如何在墙上发布信息的代码片段:

    protected void Button1_Click(object sender, EventArgs e)
    {
        string theUserId = Session["UserID"].ToString();
        using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=******;"))
        {
            cn.Open();
            using (OdbcCommand cmd = new OdbcCommand("INSERT INTO WallPosting (UserID, Wallpostings) VALUES (" + theUserId + ", '" + TextBox1.Text + "')", cn))
            {
                cmd.ExecuteNonQuery();
            }
        }
        PopulateWallPosts(theUserId);
    }
}

现在我的问题是如何在我的墙上找到朋友的帖子AHHHHH ......恐惧地奔跑!!!

我是否在脚下开枪?

这也是我在页面上生成内容的方式:

private void PopulateWallPosts(string userId)
    {

        using (OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"))
        {
            cn.Open();
            using (OdbcCommand cmd = new OdbcCommand("SELECT idWallPosting, wp.WallPostings, p.PicturePath FROM WallPosting wp LEFT JOIN User u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHERE wp.UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
            {
                //("SELECT wp.WallPostings, p.PicturePath FROM WallPosting wp LEFT JOIN [User] u ON u.UserID = wp.UserID LEFT JOIN Pictures p ON p.UserID = u.UserID WHERE UserID=" + userId + " ORDER BY idWallPosting DESC", cn))
                using (OdbcDataReader reader = cmd.ExecuteReader())
                {
                    test1.Controls.Clear();

                    while (reader.Read())
                    {

                        System.Web.UI.HtmlControls.HtmlGenericControl div = new System.Web.UI.HtmlControls.HtmlGenericControl("div");
                        div.Attributes["class"] = "test";


                        div.ID = String.Format("{0}", reader.GetString(0));
                        string id = Convert.ToString(div.ID);
                        //store the div id as a string
                        Image img = new Image();
                        img.ImageUrl = String.Format("{0}", reader.GetString(2));
                        img.AlternateText = "Test image";

                        div.Controls.Add(img);
                        div.Controls.Add(ParseControl(String.Format("&nbsp&nbsp " + "{0}", reader.GetString(1))));
                        div.Attributes.Add("onclick", "confirm_delete(" + id + ");");
                        // send the div id to javascript
                        div.Style["clear"] = "both";
                        test1.Controls.Add(div);

                    }
                }
            }
        }
    }

2 个答案:

答案 0 :(得分:4)

你的WallPosting表需要另一个userId,代表谁发送它(第一个是它的墙壁)。

然后在您的插入代码中处理这两个用户ID:谁发送了它,是谁。

修改 以下是样本选择的样子:

SELECT wp.WallPostings, p.PicturePath 
FROM WallPosting wp 
INNER JOIN [User] u ON u.UserID = wp.PostedByUserID 
INNER JOIN Pictures p ON p.UserID = u.UserID 
WHERE wp.UserID=" + userId + " 
ORDER BY idWallPosting DESC

我还会记录至少发布帖子的日期/时间,然后按顺序显示。

答案 1 :(得分:1)

我要说你需要添加另一个名为'Walls'的表,将它们绑定到User,然后让Wallpostings有一个'Wall Id',然后只检索ID等于ID的所有WallPostings用户的墙..