gpt4 book ai didi

php - 如何将登录成员(member)下的成员(member)表与玩家表合并?

转载 作者:行者123 更新时间:2023-11-30 00:05:21 28 4
gpt4 key购买 nike

我正在制作一个网站,您需要在其中注册,然后创建一个角色来玩。我如何将注册页面中使用的表格与玩家的表格结合起来,以便玩家始终获得他创建的角色。

我有一个表members,用于存储注册用户以及角色的 table 玩家

我查看了 session_id,但据我了解,我无法将数据存储到 mysql 表中。

我还考虑过在创建时将用户名和电子邮件从成员表添加到玩家表中,但我失败了。我想我可以说,如果两个表中的用户名和 ID 相同,则该玩家将被使用。

我对这一切都很陌生。

这是注册码

<?php
include_once 'db_connect.php';
include_once 'psl-config.php';

$error_msg = "";

$now = time();


if (isset($_POST['username'], $_POST['email'], $_POST['p'])) {
// Sanitize and validate the data passed in
$username = filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_EMAIL);
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
// Not a valid email
$error_msg .= '<p class="error">The email address you entered is not valid</p>';
}

$password = filter_input(INPUT_POST, 'p', FILTER_SANITIZE_STRING);
if (strlen($password) != 128) {
// The hashed pwd should be 128 characters long.
// If it's not, something really odd has happened
$error_msg .= '<p class="error">Invalid password configuration.</p>';
}

// Username validity and password validity have been checked client side.
// This should should be adequate as nobody gains any advantage from
// breaking these rules.
//

$prep_stmt = "SELECT id FROM members WHERE email = ? LIMIT 1";
$stmt = $mysqli->prepare($prep_stmt);

// check existing email
if ($stmt) {
$stmt->bind_param('s', $email);
$stmt->execute();
$stmt->store_result();

if ($stmt->num_rows == 1) {
// A user with this email address already exists
$error_msg .= '<p class="error">A user with this email address already exists.</p>';
$stmt->close();
}
$stmt->close();
} else {
$error_msg .= '<p class="error">Database error Line 39</p>';
$stmt->close();
}

// check existing username
$prep_stmt = "SELECT id FROM members WHERE username = ? LIMIT 1";
$stmt = $mysqli->prepare($prep_stmt);

if ($stmt) {
$stmt->bind_param('s', $username);
$stmt->execute();
$stmt->store_result();

if ($stmt->num_rows == 1) {
// A user with this username already exists
$error_msg .= '<p class="error">A user with this username already exists</p>';
$stmt->close();
}
$stmt->close();
} else {
$error_msg .= '<p class="error">Database error line 55</p>';
$stmt->close();
}

// TODO:
// We'll also have to account for the situation where the user doesn't have
// rights to do registration, by checking what type of user is attempting to
// perform the operation.

if (empty($error_msg)) {
// Create a random salt
//$random_salt = hash('sha512', uniqid(openssl_random_pseudo_bytes(16), TRUE)); // Did not work
$random_salt = hash('sha512', uniqid(mt_rand(1, mt_getrandmax()), true));

// Create salted password
$password = hash('sha512', $password . $random_salt);


// Insert the new user into the database
// Add here wat you want to add into the database at account creation

if ($insert_stmt = $mysqli->prepare("INSERT INTO members (username, email, password, salt, accdate) VALUES (?, ?, ?, ?, now())")) {
$insert_stmt->bind_param('ssss', $username, $email, $password, $random_salt);

// Execute the prepared query.
if (! $insert_stmt->execute()) {
header('Location: ../error.php?err=Registration failure: INSERT');
}
}
header('Location: ./register_success.php');
}
}

有人能指出我正确的方向吗?谢谢

最佳答案

需要阅读的内容有很多,一般来说,您可能希望将用户表上的用户 ID 与角色相关联。 ( user_id 将是指向 user 表上的 id 的字符的外键。两个表都有 id 的主键,它应该自动递增),然后您必须维护关系,以便当他们创建字符时您可以插入当前的字符将 user_id 登录到表中,如果您想将它们限制为一个字符,您可以使该列 ( user_id ) 唯一。

例如...

table user, 
id, name, login, password etc...

table character
id, user_id, character_name, hp, etc..

然后你可以像这样加入表格

SELECT u.*, c.* FROM users AS u JOIN characters AS c ON u.id = c.user_id WHERE u.id = $user_id.

这将(理论上)为您提供用户表和字符表中 $user_id 的所有记录(假设它们在两个表中都有记录,如果它们没有或可能没有字符记录,则使用 LEFT JOIN )

这里有一个关于一些基本数据库关系的很好的教程。

http://code.tutsplus.com/articles/sql-for-beginners-part-3-database-relationships--net-8561

关于php - 如何将登录成员(member)下的成员(member)表与玩家表合并?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24602214/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com