php - 如何导航 html 表单并使用 php mysql 保存输入,而不继续进入 .php 文件页面?

标签 php html mysql

我有 2 个 html 表单。第一页是注册页面,第二页是登录页面。我设法使用名为 connect.php 的 php 文件将注册页面的输入发送到 mysql。注册页面中的“继续”按钮将导航到 php 文件,但现在我希望注册页面中的“继续”按钮链接到登录页面,而不是 connect.php 文件并更新 mysql。我尝试将 connect.php 的代码放入注册页面的 html 代码中,但似乎不起作用。我应该将 php 代码放在 html 代码中的哪里,我应该使用 bootstrap 吗?

这是我的 connect.php 代码:

<?php

$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];

$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "ktm_member";

//Create connection
$conn = new mysqli($host, $dbusername, $dbpassword, $dbname);

if (!empty($username))
{
    if (!empty($password))
    {

        if (mysqli_connect_error())
        {
            die ('Connect error('.mysqli_connect_errno().')'.mysqli_connect_error());
        }
        else
        {
            $sql = "INSERT INTO `member list`(`name`, `age`, `gender`, `email`, `username`, `password`) VALUES ('$_POST[name]', '$_POST[age]', '$_POST[gender]', '$_POST[email]', '$_POST[username]', '$_POST[password]')";
            if ($conn->query($sql))
            {
                echo "New record added successfully.";
            }
            else
            {
                echo "Error".$sql."<br>".$conn->error;
            }
            $conn->close();
        }
    }
    else
    {
        echo "Password should not be empty";
    }
}
else
{
    echo "Username should not be empty";
    die();
}

?>

这是我的注册页面 html 代码:

<!doctype html>
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Register Page</title>

<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="./W3.CSS Template_files/w3.css">
<link rel="stylesheet" href="./W3.CSS Template_files/css">
<link rel="stylesheet" href="style.css">
<style>
body,h1 {font-family: "Raleway", sans-serif}
body, html {height: 100%}
.bgimg {
    background-image: url('forestbridge.jpg');
    min-height: 100%;
    background-position: center;
    background-size: cover;
}
</style>
</head>
<body><div class="bgimg w3-display-container w3-animate-opacity w3-text-white"><div class="w3-display-middle">
<center><h2>Register</h2></center>
      <form name="form1" method="post" action="welcomeloginpage.php" autocomplete = "off">
        <p class="w3-large w3-center">
          <label for="name">Name: </label>
          <input type="text" name="name" id="name" placeholder="Your full name" autofocus required>
        </p>
        <p class="w3-large w3-center">
          <label for="age">Age: </label>
          <input type="number" name="age" id="age">
        </p>
        <p class="w3-large w3-center">
          <label for="gender">Gender: </label>
          <input type="radio" name="gender" value="M"> Male
          <input type="radio" name="gender" value="F"> Female<br> <id="gender">
        </p>
        <p class="w3-large w3-center">
          <label for="email">Email: </label>
          <input type="email" name="email" id="email" placeholder="xxx@xx.xx">
        </p> 
        <p class="w3-large w3-center">
          <label for="username">Username: </label>
          <input type="text" name="username" id="username">
        </p>
        <p class="w3-large w3-center">
          <label for="password">Password: </label>
          <input type="password" name="password" id="password" maxlength="10" placeholder="password" data-toggle="password">
        </p>
        <p style="text-align: center;">
          <input type="submit" name="submit" id="submit" value="Submit">
        </p>
      </form>

最佳答案

如果您希望 connect.php 脚本在注册成功后将用户发送到另一个页面,那么您可以使用像这样的 header() 命令

<?php

$name = $_POST['name'];
$age = $_POST['age'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];

$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "ktm_member";

//Create connection
$conn = new mysqli($host, $dbusername, $dbpassword, $dbname);

if (!empty($username)) {
    if (!empty($password)) {

        if (mysqli_connect_error()) {
            die ('Connect error('.mysqli_connect_errno().')'
                    .mysqli_connect_error());
        } else {

            // this query style is very dangerous see note below
            $sql = "INSERT INTO `member list`
                        (`name`, `age`, `gender`, 
                         `email`, `username`, `password`) 
                    VALUES ('$_POST[name]', '$_POST[age]', '$_POST[gender]', 
                            '$_POST[email]', '$_POST[username]', '$_POST[password]')";
            if ($conn->query($sql)) {
                // remove echo as you will never see it now
                //echo "New record added successfully.";
                header('Location: otherPage.php');
                exit;   // important as header does not stop code execution in this script
            } else {
                echo "Error".$sql."<br>".$conn->error;
            }
            $conn->close();
        }
    } else {
        echo "Password should not be empty";
    }
} else {
    echo "Username should not be empty";
    die();
}
?>

You do have at least 2 serious flaws in this code though that I must mention.

Your script is wide open to SQL Injection Attack Even if you are escaping inputs, its not safe! Use prepared parameterized statements in either the MYSQLI_ or PDO API's

Never store plain Text password in your database. The most likely place where these things get compromised is from within the company running this database. An unhashed password is a total givaway for a DB Admin who just got refused a pay rise!

PHP provides password_hash() and password_verify() please use them. And here are some good ideas about passwords If you are using a PHP version prior to 5.5 there is a compatibility pack available here

关于php - 如何导航 html 表单并使用 php mysql 保存输入,而不继续进入 .php 文件页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52539020/

相关文章:

php - 在 Symfony 中向我的 ChoiceType 表单字段添加一个 'other, please specify' 选项

父类不能在静态上下文中使用 PHP const/static 变量

jquery - 如何动态改变div内容?

css - 如何在网页源代码中添加链接?

php - 确定 PHP 下周

php - 如何衡量密码的强度?

html - Materialise SideNav 固定不适合 div

php - 就地编辑并将值也更改为数据库?

mysql - 自动增量主键完整性

PHP、MySQL、Huge Join、处理速度