使用 MySQL 的 HTML 和 Php 表单无法工作

问题描述 投票:0回答:1

我是编码新手,在这里问我的第一个问题。 我尝试使用 Php 和 MySQL 工作台创建 HTML 注册表单。

根据网上教程, 我提交表单的输出必须是“注册成功。”

但是,它显示:

此页面无法正常工作。

如果问题仍然存在,请联系网站所有者。

HTTP 错误 405

这是我的 HTML 代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body bgColor="yellow">
    <form method="post" action="connect.php">
        <fieldset>
            <legend>Details</legend>
        <p>
            <label>User Type: </label>
            <select name="user_type">
                <option>Choose a user type</option>
                <option>Admin</option>
                <option>Teacher</option>
                <option>Student</option>
            </select>
        </p>
        <p>
            <label>First Name: </label>
            <input type="text" name="first_name" />
        </p>
        <p>
            <label>Last Name: </label>
            <input type="text" name="last_name" />
        </p>
        <p>
            <label>Gender: </label>
            <br/>
            <input type="radio" name="gender" value="Female">Female<br/>
            <input type="radio" name="gender" value="Male">Male<br/>
            <input type="radio" name="gender" value="Other">Other
        </p>
        <p>
            <label>Phone number: </label>
            <input type="text" name="phone" />
        </p>
        <p>
            <label>Email: </label>
            <input type="text" name="email" />
        </p>
        <p>
            <label>Password: </label>
            <input type="password" name="pass" />
        </p>
        <input type="submit">
        </fieldset>
    </form>
</body>
</html>

这是我的 PHP 代码:

<?php

//Based on the name of input and select
$user_type = $_POST['user_type'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$gender = $_POST['gender'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$pass = $_POST['pass'];

// Database connection
$conn = new mysqli('local host','root','college');
if($conn->connect_error){
    die('Connection Failed: '.$conn->connect_error);
}
else
{
    $stmt = $conn->prepare("insert into users(user_type, first_name, last_name, gender, phone, email, pass)
                            values(?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssiss", $user_type, $first_name, $last_name, $gender, $phone, $email, $pass)
    $stmt->execute();
    echo "Signed up successfully.";
    $stmt->close();
    $conn->close();
}

?>

我应该使用 Xampp 而不是 MySQL Workbench 还是这段代码有问题?

php mysql forms
1个回答
-1
投票
<?php

$user_type = $_POST['user_type'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$gender = $_POST['gender'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$pass = $_POST['pass'];

$conn = new mysqli('0.0.0.0:3306','root','root', 'ast');
if($conn->connect_error){
    die('Connection Failed: '.$conn->connect_error);
}
else
{
    $stmt = $conn->prepare("insert into users(user_type, first_name, last_name, gender, phone, email, pass) values(?, ?, ?, ?, ?, ?, ?)");
    $stmt->bind_param("ssssiss", $user_type, $first_name, $last_name, $gender, $phone, $email, $pass);
    $stmt->execute();
    echo "Signed up successfully.";
    $stmt->close();
    $conn->close();
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Document</title>
</head>
<body bgColor="yellow">
    <form method="post" action=<?=$_SERVER['PHP_SELF']?>>
        <fieldset>
            <legend>Details</legend>
        <p>
            <label>User Type: </label>
            <select name="user_type">
                <option>Choose a user type</option>
                <option>Admin</option>
                <option>Teacher</option>
                <option>Student</option>
            </select>
        </p>
        <p>
            <label>First Name: </label>
            <input type="text" name="first_name" />
        </p>
        <p>
            <label>Last Name: </label>
            <input type="text" name="last_name" />
        </p>
        <p>
            <label>Gender: </label>
            <br/>
            <input type="radio" name="gender" value="Female">Female<br/>
            <input type="radio" name="gender" value="Male">Male<br/>
            <input type="radio" name="gender" value="Other">Other
        </p>
        <p>
            <label>Phone number: </label>
            <input type="text" name="phone" />
        </p>
        <p>
            <label>Email: </label>
            <input type="text" name="email" />
        </p>
        <p>
            <label>Password: </label>
            <input type="password" name="pass" />
        </p>
        <input type="submit">
        </fieldset>
    </form>
</body>
</html>

试试这个。

© www.soinside.com 2019 - 2024. All rights reserved.