我正在制作一个网站,您需要注册,然后创建一个角色玩。我如何将注册页面中使用的表格与玩家的表格结合起来,以便玩家总是获得他创建的角色。
我有一个名为user的表成员来存储注册用户和一个表玩家的字符
我查看了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将是指向用户表上id的字符的外键。两个表都有一个主键id,应该自动增加),你必须维护关系,所以当他们创建字符时,你可以将当前登录的user_id插入到表中,如果你想限制他们一个字符,你可以使列(user_id)唯一。
例如…
table user,
id, name, login, password etc...
table character
id, user_id, character_name, hp, etc..
那么你可以像这样在表上join
SELECT u.*, c.* FROM users AS u JOIN characters AS c ON u.id = c.user_id WHERE u.id = $user_id.
,这将(理论上)给你所有的记录从用户表和字符表$user_id(假设他们有记录在两个表,使用左连接,如果他们没有或可能没有字符记录)
这是一个关于基本数据库关系的很好的教程。
http://code.tutsplus.com/articles/sql - -初学者来说3 -数据库-关系-网- 8561