doumubi6784 2019-04-22 19:33
浏览 71

扩展php注册系统

I have the following table in mysql database :

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;

and a php system to handle the users . I want to add a new field to the register form like a selectable list : Beginner,Intermediate, Advanced, where user can select the status at the time they will fill the register form. How can I handle this new select list based on its values(Beginner,Intermediate, Advanced) in the mysql table and also in php code. Because based on experience I have a profile page that will displayed different information(also based on experience of the user). This is the original document : 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();
?>
  • 写回答

2条回答 默认 最新

  • doushi2047 2019-04-22 19:59
    关注

    First, add a column into your table to hold your value:

    ALTER TABLE `mytable` ADD `myselect` TEXT NOT NULL; 
    

    Next, make a select element in your form:

    <form action = 'myphppage.php' method='post'>
        <select name='yourselect'>
            <option value='Advanced'>Advanced</option>
            <option value='Intermediate'>Intermediate</option>
            <option value='Beginner'>Intermediate</option>
        </select>
        <button type='submit'>Submit</button>
    </form>
    

    When the user clicks submit, the value of your select element will be sent in your $_POST array to 'myphppage.php', with the name of the element as an index.

    <?php
    
       $select = $_POST['yourselect']; 
    
    ?>
    

    Now that you have the value as a string, you can use the same methods you've posted above to insert the value into the database.

    <?php
    
        if($stmt = $con->prepare('INSERT INTO `mytable`(`myselect`) VALUES (?)'){
            $stmt->bind_param('s', $select){
                $stmt->execute();
            }
        }
    
    ?>
    

    Obviously, you'll need to tailor this to match the specifics of your situation, but this is the idea.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分