phpMyAdmin mySQL 数据库似乎无法在我的代码中识别?

phpMyAdmin mySQL database not seem to be recognised in my code?

所以我无法让我的 PHP 代码实际执行任何操作来使用我使用 phpMyAdmin 设置的 mySQl 数据库注册新用户。

我已经尝试了很多方法来解决这个问题,我觉得我很自信地理解代码正在尝试做什么,但是当我测试它时,我没有收到任何错误(或成功消息)。

按钮上的操作已执行(因为根本没有 PHP)并且没有新条目插入到我的 table 'tbl_users' 我的数据库中称为 'cmeteventmanagement'.

请注意,我使用的是 wampserver,该项目用于教育目的。

<!-- PHP to use form inputs with database -->
<?php

    // Define variables and set to empty values
    $firstname = "";
    $lastname = "";
    $email = "";
    $telno = "";
    $gender = "";
    $username = "";
    $password = "";

    // Declare variables to hold error messages for each field.
    $firstnameError = "";
    $lastnameError = "";
    $emailError = "";
    $telnoError = "";
    $genderError = "";
    $usernameError = "";
    $passwordError = "";
    $foundErrors = false;

    // If the form has been submitted, AND the method is POST
    if($_SERVER["REQUEST_METHOD"] == "POST")
    {
        // FIRST NAME
        $firstname = clearUserInputs($_POST["firstname"]);

        // LAST NAME
        $lastname = clearUserInputs($_POST["lastname"]);

        // EMAIL
        if(empty($_POST['email']))
        {
            $emailError = "Email is required";
            $foundErrors = true;
        }
        else
        {
            $email = clearUserInputs($_POST["email"]);
        }

        // TELEPHONE NUMBER
        $telno = clearUserInputs($_POST["telno"]);

        // GENDER
        $gender = clearUserInputs($_POST["gender"]);

        if(empty($_POST['username']))
        {
            $usernameError = "Username is required";
            $foundErrors = true;
        }
        else
        {
            $username = clearUserInputs($_POST["username"]);
        }

        if(empty($_POST["password"]))
        {
            $passwordError = "Password is required";
            $foundErrors = true;
        }   
        else
        {
            $password = clearUserInputs($_POST["password"]);
        }

        // If no errors were found
        if($foundErrors == false)
        {
            // Declare variables for database and server connection
            $serv_name = "localhost";
            $db_user = "root";
            $db_pass = "";
            $db_name = "cmeteventmanagement";

            // Save form data to the MySQL database
            try
            {
                $connect = new PDO("mysql:host=$serv_name; dbname=$db_name", $db_user, $db_pass);
                $connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                echo "Connected successfully to Server and Database <br>";
            }
            catch (PDOException $e)
            {
                echo "Connection to Server and Database failed: " . $e->getMessage();
            }

            // Prepare SQL statements to insert data
            $sql = "INSERT INTO tbl_users (firstname, lastname, email, telno, gender, username, password) VALUES (?, ?, ?, ?, ?, ?)";
            $stmt = $connect->prepare($sql);
            $stmt->execute([$firstname, $lastname, $email, $telno, $gender, $username, $password]);
            echo "New user registered successfully";
        }
    }

    // function to clear userinputs
    function clearUserInputs($data){
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
    }

?>

<!-- Start of HTML elements -->
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title>Register</title>
</head>
<body>
    <!-- Header Links -->
    <a href="index.html">Home</a>
    <a href="login.php">Login</a>
    <a href="register.php">Register</a>

    <!-- Page Title -->
    <h1>Register</h1>

    <!-- Main Content -->
    <!-- <form method="POST" action="events.html" autocomplete="off"> -->
    <form method="POST" action="login.php" autocomplete="off">
        First Name:
            <br>
            <input type="text" name="firstname" maxlength="20">
            <br><br>
        Last Name:
            <br>
            <input type="text" name="lastname" maxlength="20">
            <br><br>
        Email:
            <br>
            <input type="text" name="email" maxlength="50">
            <br><br>
        Tel Number:
            <br>
            <input type="text" name="telno" maxlength="15">
            <br><br>
        Gemder:
            <br>
            <input type="radio" name="gender" value="male">Male
            <input type="radio" name="gender" value="female">Female
            <input type="radio" name="gender" value="other">Other
            <br><br>
        Desired Username:
            <br>
            <input type="text" name="username" maxlength="25">
            <br><br>
        Desired Password:
            <br>
            <input type="password" name="password" minlength="8" maxlength="15">
            <br><br>
        <input type="submit" value="Register">
    </form>
</body>
</html>

我想你把参数的那个问号给留下了。所以你需要在 VALUES 部分添加 ? 因为你想传递 7 个参数但是有 6 个问号。

$sql = "INSERT INTO tbl_users (firstname, lastname, email, telno, gender, username, password) VALUES (?, ?, ?, ?, ?, ?, ?)";