gpt4 book ai didi

php - 扩展php注册系统

转载 作者:行者123 更新时间:2023-11-29 16:05:18 25 4
gpt4 key购买 nike

我在 mysql 数据库中有下表:

CREATE TABLE IF NOT EXISTS `accounts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(255) NOT NULL,
`email` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

和一个处理用户的 php 系统。我想在注册表中添加一个新字段,如可选择列表:初级、中级、高级,用户可以在填写注册表时选择状态。我如何根据 mysql 表和 php 代码中的值(初级、中级、高级)处理这个新的选择列表。因为根据经验,我有一个个人资料页面,将显示不同的信息(也基于用户的经验)。这是原始文件:https://codeshack.io/secure-registration-system-php-mysql/

<form action="register.php" method="post" autocomplete="off">
<div class="form-group input-group">
<select class="form-control" name="invest">
<option selected="">Investor Experience Level</option>
<option>Beginner</option>
<option>Intermediate</option>
<option>Advanced</option>
</select>
</div> <!-- form-group end.// -->
<label for="username">
<i class="fas fa-user"></i>
</label>
<input type="text" name="username" placeholder="Username" id="username" required>
<label for="password">
<i class="fas fa-lock"></i>
</label>
<input type="password" name="password" placeholder="Password" id="password" required>
<label for="email">
<i class="fas fa-envelope"></i>
</label>
<input type="email" name="email" placeholder="Email" id="email" required>
<p>Already have an account? <a href="login.html">Login here</a>.</p>
<input type="submit" value="Register">
</form>
<?php
// Change this to your connection info.
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'root';
$DATABASE_PASS = '';
$DATABASE_NAME = 'phplogindb';
// Try and connect using the info above.
$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if (mysqli_connect_errno()) {
// If there is an error with the connection, stop the script and display the error.
die ('Failed to connect to MySQL: ' . mysqli_connect_error());
}

// Now we check if the data was submitted, isset() function will check if the data exists.
if (!isset($_POST['username'], $_POST['password'], $_POST['email'])) {
// Could not get the data that should have been sent.
die ('Please complete the registration form!');
}
// Make sure the submitted registration values are not empty.
if (empty($_POST['username']) || empty($_POST['password']) || empty($_POST['email'])) {
// One or more values are empty.
die ('Please complete the registration form');
}

// We need to check if the account with that username exists.
if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {

if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
die ('Email is not valid!');
}

if (preg_match('/[A-Za-z0-9]+/', $_POST['username']) == 0) {
die ('Username is not valid!');
}

if (strlen($_POST['password']) > 20 || strlen($_POST['password']) < 5) {
die ('Password must be between 5 and 20 characters long!');
}

// Bind parameters (s = string, i = int, b = blob, etc), hash the password using the PHP password_hash function.
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
// Store the result so we can check if the account exists in the database.
if ($stmt->num_rows > 0) {
// Username already exists
echo 'Username exists, please choose another!';
} else {
// Username doesnt exists, insert new account
if ($stmt = $con->prepare('INSERT INTO accounts (username, password, email) VALUES (?, ?, ?)')) {
// We do not want to expose passwords in our database, so hash the password and use password_verify when a user logs in.
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$stmt->bind_param('sss', $_POST['username'], $password, $_POST['email']);
$stmt->execute();
header('Location: login.html');
echo 'You have successfully registered, you can now login!';
} else {
// Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
echo 'Could not prepare statement!';
}
}
$stmt->close();
} else {
// Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
echo 'Could not prepare statement!';
}
$con->close();
?>

最佳答案

要向现有表添加新列,您应该运行以下 SQL 命令:

ALTER TABLE accounts ADD COLUMN experience ENUM('Beginner','Intermediate','Advanced') NULL DEFAULT 'Beginner' FIRST;

枚举列的优点是只存储枚举(1-3)而不存储字符串。

然后插入命令将是

INSERT INTO accounts (username, password, email,experience ) VALUES (?, ?, ?, ?)

经验列的值可以是字符串“Beginner”、“Intermediate”、“Advanced”之一或相应的数值 1、2、3

关于php - 扩展php注册系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55800071/

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