我的数据库连接似乎不起作用

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

当我想要进行填充数据库的注册时,它不会将我重定向到我的index.html 文件,而是向我抛出一个显示我的 php 代码的屏幕。

我已经尝试制作一个仅用于连接的文件,检查了所有可能错误的内容,但我仍然遇到同样的问题

db.php:

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mi_base_de_datos";
$port = 3307;

$conn = new mysqli($servername, $username, $password, $dbname, $port);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
?>

注册.php

<?php
include 'db.php';

ob_start();

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $password = $_POST['password'];
    $confirm_password = $_POST['confirm_password'];
    $birthday = $_POST['birthday'];
    $gender = $_POST['gender'];

    if ($password !== $confirm_password) {
        echo "Passwords do not match.";
        exit();
    }

    $hashed_password = password_hash($password, PASSWORD_DEFAULT);

    $sql = $conn->prepare("INSERT INTO users (name, email, password, birthday, gender) VALUES (?, ?, ?, ?, ?)");
    $sql->bind_param("sssss", $name, $email, $hashed_password, $birthday, $gender);

    if ($sql->execute()) {
        header("Location: ../index.html");
        ob_end_flush();
        exit();
    } else {
        echo "Error: " . $sql->error;
    }

    $sql->close();
}

$conn->close();

ob_end_flush();
?>

登录.php:

<?php
include 'db.php';

if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['email']) && isset($_POST['password'])) {
    $email = $_POST['email'];
    $password = $_POST['password'];

    $stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
    $stmt->bind_param("s", $email);
    $stmt->execute();
    $result = $stmt->get_result();

    if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
        if (password_verify($password, $row['password'])) {
            header("Location: ../index.html");
            exit();
        } else {
            echo "Invalid password.";
        }
    } else {
        echo "No user found with this email.";
    }

    $stmt->close();
} else {
    echo "Please fill in both the email and password fields.";
}

$conn->close();
?>

formulario.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register and Login</title>
    <link rel="stylesheet" href="css/formulario.css">
</head>
<body>
    <div class="header">
        <div class="header-content">
            <h1>Bienvenido</h1>
            <div class="form-container">
                <!-- Formulario de Iniciar Sesión -->
                <div class="form-box" id="login-form">
                    <h2>Iniciar Sesión</h2>
                    <form action="php/login.php" method="POST">
                        <input type="email" name="email" placeholder="Correo Electrónico" required>
                        <input type="password" name="password" placeholder="Contraseña" required>
                        <button type="submit">Iniciar Sesión</button>
                    </form>
                    <p>¿No tienes una cuenta? <a href="#" onclick="toggleForm('register')">Regístrate aquí</a></p>
                </div>

                <!-- Formulario de Registro -->
                <div class="form-box" id="register-form" style="display: none;">
                    <h2>Registrarse</h2>
                    <form action="php/register.php" method="POST">
                        <input type="text" name="name" placeholder="Nombre" required>
                        <input type="email" name="email" placeholder="Correo Electrónico" required>
                        <input type="password" name="password" placeholder="Contraseña" required>
                        <input type="password" name="confirm_password" placeholder="Confirmar Contraseña" required>
                        <input type="date" name="birthday" placeholder="Fecha de Nacimiento" required>
                        <select name="gender" required>
                            <option value="">Seleccione su género</option>
                            <option value="Mujer">Mujer</option>
                            <option value="Hombre">Hombre</option>
                            <option value="Otro">Otro</option>
                        </select>
                        <button type="submit">Registrarse</button>
                    </form>
                    <p>¿Ya tienes una cuenta? <a href="#" onclick="toggleForm('login')">Inicia sesión</a></p>
                </div>
            </div>
        </div>
    </div>

    <script>
        function toggleForm(form) {
            document.getElementById('login-form').style.display = form === 'login' ? 'block' : 'none';
            document.getElementById('register-form').style.display = form === 'register' ? 'block' : 'none';
        }
    </script>
</body>
</html>

XAMPP 控制面板 数据库结构

php html database xampp
1个回答
-1
投票

我了解您在注册后遇到重定向问题

检查您的index.html路径并将其与您的网址交叉匹配。 (我认为从 header('Location:index.html') 中删除 ../ 后它会起作用)

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.