mysql - 为每个新用户更新数据库表中的行 WebMatrix

标签 mysql webmatrix

我的数据库(StarterSite)中有一个表(About_User),该表应该可以工作,以便每个单独的行都针对每个单独的用户。但是,无论哪个用户登录,他们都可以访问同一行(换句话说,不会为每个新用户创建新行)。我进行了三次检查,以确保数据库中的所有内容都设置正确。我在此注册页面中使用 StarterSite 模板(针对问题进行编辑):

// If all information is valid, create a new account
    if (Validation.IsValid()) {
        // Insert a new user into the database
        var db = Database.Open("StarterSite");

        // Check if user already exists
        var user = db.QuerySingle("SELECT Email FROM UserProfile WHERE LOWER(Email) = LOWER(@0)", email);
        if (user == null) {
            // Insert email into the profile table
            db.Execute("INSERT INTO UserProfile (Email) VALUES (@0)", email);

            // Create and associate a new entry in the membership database.
            // If successful, continue processing the request
            try {
                bool requireEmailConfirmation = !WebMail.SmtpServer.IsEmpty();
                var token = WebSecurity.CreateAccount(email, password, requireEmailConfirmation);
                if (requireEmailConfirmation) {
                    var hostUrl = Request.Url.GetComponents(UriComponents.SchemeAndServer, UriFormat.Unescaped);
                    var confirmationUrl = hostUrl + VirtualPathUtility.ToAbsolute("~/Account/Confirm?confirmationCode=" + HttpUtility.UrlEncode(token));

                    WebMail.Send(
                        to: email,
                        subject: "Please confirm your account",
                        body: "Your confirmation code is: " + token + ". Visit <a href=\"" + confirmationUrl + "\">" + confirmationUrl + "</a> to activate your account."
                    );
                }

                if (requireEmailConfirmation) {
                    // Thank the user for registering and let them know an email is on its way
                    Response.Redirect("~/Account/Thanks");
                } else {
                    // Navigate back to the homepage and exit
                    WebSecurity.Login(email, password);

                    Response.Redirect("~/");
                }
            } catch (System.Web.Security.MembershipCreateUserException e) {
                ModelState.AddFormError(e.Message);
            }
        } else {
            // User already exists
            ModelState.AddFormError("Email address is already in use.");
        }
    }

该表确实有一个(应该)自动增量的 ID 列。

其他列来自从用户收集不同信息的表单,例如“关于我”页面,因此每个人的信息都应该不同。

我怎样才能做到这一点?

最佳答案

你的问题中有一些我不明白的地方。首先,您的代码看起来没有经过编辑(它与第 39 行到第 82 行的模板相同)。其次,您的问题有 mysql 标签:如果您使用的是 MySql 数据库而不是 Starter Site 模板的 Sql Ce 数据库,也许这可能是问题所在(查看 WebSecurity.CreateAccount fails for MySQL )。

使用默认的 StarteSite Sql Ce 数据库,您的问题的答案非常简单。添加以下行

db.Execute("INSERT INTO About_User(UserId, UserPage) VALUES (@0, @1)", 
    db.GetLastInsertId(), userPage);

就在 db.Execute("INSERT INTO UserProfile (Email) VALUES (@0)", email); 语句之后。

About_User 表中不需要 ID 自动增量列,只需要 UserId 主键列作为外键。

关于mysql - 为每个新用户更新数据库表中的行 WebMatrix,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23964604/

相关文章:

c# - 从 asp.net 网页中的 jQuery Ajax 调用检索 JSON 数组

c# - 如何为 IEnumerable<dynamic> 赋值

mysql - 在 mysql 中使用 CAST 进行不正确的排序

php - 使用连接变量连接到 MySQL DB

php - Mysql - PHP - 查看总和列

asp.net - 使用 webmatrix 创建 Web 服务

ASP.NET - Razor 静态变量生命周期?

c# - ASP.net Razor - 使用 GMail SMTP 从网站发送电子邮件

mysql - 如何计算2列的记录数

mysql - 导入mysql数据库时出错