如何将登录/注册页面连接至XAMPP环境下的MySQL数据库?
Hey there! Let's walk through getting your login/register pages hooked up to XAMPP's Apache server and MySQL database. I'll break this down into simple, actionable steps based on your existing code:
- Fire up your XAMPP Control Panel and start both the Apache and MySQL modules.
- Move all your files (login HTML, register HTML, CSS files, and the PHP handler we'll create) into XAMPP's
htdocsfolder—for example,C:\xampp\htdocs\your-business-site\. This is where Apache looks for web content.
We need a place to store user accounts. Let's use XAMPP's built-in phpMyAdmin:
- Open your browser and go to
http://localhost/phpmyadmin. - Create a new database (name it something like
business_db). - Run this SQL query to create a
userstable (paste it into the SQL tab):
CREATE DATABASE IF NOT EXISTS business_db; USE business_db; CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY, email VARCHAR(100) NOT NULL UNIQUE, password VARCHAR(255) NOT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP );
This table stores unique emails, hashed passwords (never store plain text passwords!), and account creation timestamps.
action_page.php) Your forms currently point to action_page.php, so we'll turn this into the backend that handles both registration and login. Create this file in your project folder:
<?php // Enable error reporting for debugging (remove this in production!) error_reporting(E_ALL); ini_set('display_errors', 1); // XAMPP MySQL connection details (default values—adjust if you changed them) $server = "localhost"; $db_user = "root"; $db_pass = ""; $db_name = "business_db"; // Connect to the database $conn = new mysqli($server, $db_user, $db_pass, $db_name); // Check if connection failed if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } // Handle Registration (triggered when "psw-repeat" field exists in form) if (isset($_POST['psw-repeat'])) { $email = $_POST['email']; $password = $_POST['psw']; $repeat_pass = $_POST['psw-repeat']; // Basic validation: make sure passwords match if ($password !== $repeat_pass) { die("Oops! Passwords don't match. Go back and try again."); } // Hash the password for security (critical for production!) $hashed_pass = password_hash($password, PASSWORD_DEFAULT); // Insert user into database (using prepared statements to prevent SQL injection) $stmt = $conn->prepare("INSERT INTO users (email, password) VALUES (?, ?)"); $stmt->bind_param("ss", $email, $hashed_pass); if ($stmt->execute()) { echo "Registration successful! You can now <a href='login.html'>log in</a>."; } else { if ($conn->errno === 1062) { echo "This email is already registered. Try logging in instead!"; } else { echo "Error: " . $stmt->error; } } $stmt->close(); } // Handle Login (triggered when "uname" field exists in form) elseif (isset($_POST['uname'])) { // Note: Your login form uses "uname" but we store emails—let's map that to email $email = $_POST['uname']; $password = $_POST['psw']; // Fetch user from database $stmt = $conn->prepare("SELECT id, password FROM users WHERE email = ?"); $stmt->bind_param("s", $email); $stmt->execute(); $stmt->store_result(); if ($stmt->num_rows === 1) { $stmt->bind_result($user_id, $stored_hash); $stmt->fetch(); // Verify password against the stored hash if (password_verify($password, $stored_hash)) { // Start a session to keep the user logged in session_start(); $_SESSION['user_id'] = $user_id; $_SESSION['user_email'] = $email; echo "Welcome back, " . $email . "! <a href='dashboard.html'>Go to your dashboard</a>"; // For production, use header("Location: dashboard.html"); exit(); instead of echo } else { echo "Incorrect password. Try again!"; } } else { echo "No account found with that email. Maybe register first?"; } $stmt->close(); } // Close the database connection $conn->close(); ?>
Your current forms are missing a method attribute (default is GET, which is unsafe for passwords). Let's fix that and tweak for consistency:
Login Page Updates
Change your form tag to use POST, and update the username field to use email (matches our database):
<form action="action_page.php" method="POST"> <div class="container"> <label for="email"><b>Email</b></label> <input type="email" placeholder="Enter Email" name="uname" required> <!-- We kept "uname" to match the PHP handler, but you could rename it to "email" for clarity --> <label for="psw"><b>Password</b></label> <input type="password" placeholder="Enter Password" name="psw" required> <button type="submit">Login</button> <label> <input type="checkbox" checked="checked" name="remember"> Remember me </label> </div> <div class="container" style="background-color:#f1f1f1"> <button type="button" class="cancelbtn">Cancel</button> <span class="psw">Forgot <a href="#">password?</a></span> </div> </form>
Registration Page Updates
Just add the method="POST" attribute to your form tag, plus a small validation tweak and fixed login link:
<form action="action_page.php" method="POST"> <div class="container"> <h1>Register</h1> <p>Please fill in this form to create an account.</p> <hr> <label for="email"><b>Email</b></label> <input type="email" placeholder="Enter Email" name="email" required> <!-- Changed to type="email" for native validation --> <label for="psw"><b>Password</b></label> <input type="password" placeholder="Enter Password" name="psw" required> <label for="psw-repeat"><b>Repeat Password</b></label> <input type="password" placeholder="Repeat Password" name="psw-repeat" required> <hr> <p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p> <button type="submit" class="registerbtn">Register</button> </div> <div class="container signin"> <p>Already have an account? <a href="login.html">Sign in</a>.</p> <!-- Updated link to point to your login page --> </div> </form>
- Make sure Apache and MySQL are still running in XAMPP.
- Open your browser and go to
http://localhost/your-business-site/register.html—create a test account. - Then go to
http://localhost/your-business-site/login.htmland log in with the account you just made.
Quick Notes for Production
- Turn off error reporting (remove the
error_reportingandini_setlines in the PHP file). - Use HTTPS to encrypt data between the user's browser and your server.
- Add more validation (e.g., check for valid email format, password strength).
- Implement password reset functionality if needed.
内容的提问来源于stack exchange,提问作者FewGames Development

