You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PHP注册系统无法跳转及写入MySQL数据库问题排查求助

注册系统异常排查与修复

问题现象

提交注册表单后停留在register.php页面,既跳不到index.php,用户信息也写不进MySQL数据库。预期是注册完成后,数据成功入库并自动跳转首页。

相关代码文件

index.php

<html>
<body>
    <h1>signup form</h1>
    <form action="register.php" method="post">
        <input type="text" name="uid" placeholder="username">
        <input type="password" name="pwd" placeholder="password">
        <button type="submit" name="submit" value="submit">submit</button>
    </form>
</body>
</html>

db.php

<?php

class db
{
    private $host;
    private $user;
    private $pwd;
    private $dbname;

    protected function connect()
    {
        $this->host = "localhost";
        $this->user = "admin";
        $this->pwd = "123";
        $this->dbname = "oop";

        $conn = new mysqli($this->host, $this->user, $this->pwd, $this->dbname);
        return $conn;
    }
}

register.php

<?php

if (isset($_POST["submit"])) {
    $uid = $_POST["uid"];
    $pwd = $_POST["pwd"];

    include "db.php";
    include "registration.php";
    include "register-contr.php";
    $signup = new SignupContr($uid, $pwd);

    $signup->signupUser();

    header("location: index.php?error=none");
    exit();
}

registration.php

<?php

class Signup extends db
{
    protected function setUser($uid, $pwd)
    {
        $stmt = $this->connect()->prepare('INSERT INTO users (uid, pwd) VALUES (?, ?);');

        $hashedpwd = password_hash($pwd, PASSWORD_DEFAULT);

        if (!$stmt->execute(array($uid, $hashedpwd))) {
            $stmt = null;
            header("location: ../index.php?error=stmtfailed");
            exit();
        }

        $stmt = null;
    }
}

register-contr.php

<?php

class SignupContr extends Signup
{
    private $uid;
    private $pwd;

    public function __construct($uid, $pwd)
    {
        $this->uid = $uid;
        $this->pwd = $pwd;
    }

    public function signupUser()
    {
        if ($this->emptyInput() == false) {
            header("location: ../index.php?error=emptyinput");
            exit();
        }

        if ($this->invalidUid() == false) {
            header("location: ../index.php?error=username");
            exit();
        }

        $this->setUser($this->uid, $this->pwd);
    }

    private function emptyInput()
    {
        $result = "";
        if (empty($this->uid) || empty($this->pwd)) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }

    private function invalidUid()
    {
        $result = "";
        if (!preg_match("/^[a-zA-Z0-9]*$/", $this->uid)) {
            $result = false;
        } else {
            $result = true;
        }
        return $result;
    }
}

问题根源

  1. MySQLi预处理语句使用错误:mysqli_stmt::execute()不支持直接传数组参数,必须用bind_param()绑定;而且没检查prepare()是否成功,一旦数据库连接或SQL语句有误,直接执行execute()会报错中断流程。
  2. 逻辑判断初始化错误:emptyInput()和invalidUid()里把$result初始化为空字符串,空字符串在布尔判断里等价于false,会导致后续的条件判断逻辑混乱。
  3. 跳转路径不一致:register.php和index.php在同一目录,但错误跳转用了../index.php,会跳去上级目录,导致跳转失败后停留在register.php。
  4. 无错误提示:没开启PHP错误显示,出问题看不到具体报错,难排查。

修复步骤

1. 开启开发环境错误提示

在所有PHP文件开头加这段代码,方便排查问题:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

2. 修正registration.php的数据库操作

<?php

class Signup extends db
{
    protected function setUser($uid, $pwd)
    {
        $conn = $this->connect();
        // 检查数据库连接是否成功
        if ($conn->connect_error) {
            header("location: index.php?error=dbconnect");
            exit();
        }

        $stmt = $conn->prepare('INSERT INTO users (uid, pwd) VALUES (?, ?);');
        // 检查预处理语句是否创建成功
        if (!$stmt) {
            header("location: index.php?error=stmtprepare");
            exit();
        }

        $hashedpwd = password_hash($pwd, PASSWORD_DEFAULT);
        // 用bind_param绑定参数,ss表示两个字符串类型参数
        $stmt->bind_param("ss", $uid, $hashedpwd);

        if (!$stmt->execute()) {
            $stmt->close();
            $conn->close();
            header("location: index.php?error=stmtfailed");
            exit();
        }

        $stmt->close();
        $conn->close();
    }
}

3. 修正register-contr.php的逻辑判断

<?php

class SignupContr extends Signup
{
    private $uid;
    private $pwd;

    public function __construct($uid, $pwd)
    {
        $this->uid = $uid;
        $this->pwd = $pwd;
    }

    public function signupUser()
    {
        if (!$this->emptyInput()) {
            header("location: index.php?error=emptyinput");
            exit();
        }

        if (!$this->invalidUid()) {
            header("location: index.php?error=username");
            exit();
        }

        $this->setUser($this->uid, $this->pwd);
    }

    private function emptyInput()
    {
        // 直接返回布尔值,简化逻辑
        return !(empty($this->uid) || empty($this->pwd));
    }

    private function invalidUid()
    {
        // 直接返回正则匹配结果
        return preg_match("/^[a-zA-Z0-9]*$/", $this->uid);
    }
}

4. 统一register.php的跳转路径

<?php
// 开启错误提示
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

if (isset($_POST["submit"])) {
    $uid = $_POST["uid"];
    $pwd = $_POST["pwd"];

    include "db.php";
    include "registration.php";
    include "register-contr.php";
    $signup = new SignupContr($uid, $pwd);

    $signup->signupUser();

    // 确保数据入库后跳转首页
    header("location: index.php?error=none");
    exit();
}

5. 检查数据库表结构

确保oop数据库存在,users表结构正确,执行以下SQL:

CREATE DATABASE IF NOT EXISTS oop;
USE oop;
CREATE TABLE IF NOT EXISTS users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    uid VARCHAR(50) NOT NULL UNIQUE,
    pwd VARCHAR(255) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

内容的提问来源于stack exchange,提问作者Xanward

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 03:40:48