首页 > 解决方案 > 提交按钮不向数据库发送数据并且不显示错误

问题描述

我正在尝试在我的数据库中创建一个新用户但没有响应,数据没有插入数据库并且没有显示错误。

我试图在互联网上研究这个问题,但似乎没有得到我的问题的答案

这是我的 index.php

<?php
class login{
protected $localhost='localhost';
protected $user='root';
protected $pass='';
protected $dbname='mydb';
protected $conn;
public $errors=array();

function __construct(){
$this->conn=mysqli_connect($this->localhost, $this->user, $this->pass, 
$this->dbname);
}
protected function checkinput($var){
    $var=htmlspecialchars($var);
    $var=trim($var);
    $ar=stripslashes($var);
    return $var;
}
public function insertintotb($username, $password){ 
$username=$this->checkinput($username);
$password=$this->checkinput($password);

    if($this->checkerrors($username, $password)){
        if($this->checkusername($username)){
            if($this->insertintodb($username, $password))$this->errors= 
['Creation Success!!!'];
        }
    }
}
protected function checkerrors($username, $password){
    if(strlen($username)>10 || strlen($username)<4){
    array_push($this->errors, 'Username must be between 4 to 10 
characters');
    return false;
    }
    if(strlen($password)>10 || strlen($password)<4){
        array_push($this->errors, 'Password must be atleast 4 to 10 
characters');
        return false;
}
    return true;
}
protected function insertintodb($username, $password){
   $query= "INSERT INTO users(username, password) VALUES('".$username."', 
'".$password."')";
    mysqli_query($this->conn, $query);
    if((mysqli_affected_rows)>0){
        return true;
    }else{
        return false;
    }
}
public function checkusername($username){
    $query="SELECT username FROM users where username='".$username."'";
    if((mysqli_affected_rows)>0){
        array_push($this->erros, 'Username already exists, please enter 
another username');
        return false;
    }else{
        return true;
    }

}
}

?>

这是我的html

<?php include'index.php';?>
<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
<link rel="stylesheet" href="style.css">
</head>

<body>
<div class="box">
    <div class="form_input">
        <form method="post" action="">
            <input type="text" name="username" placeholder="Enter 
Username"><br><br>
            <input type="password" name="password" placeholder="Enter 
Password"><br>
            <br><input type="submit" name="send">
        </form>
        <?php
    if(isset ($_post['send'])){
$object = new login();
$object->insertintotb($_post['username'], $_post['password']);
foreach($object->errors as $error){
echo $error;
}

}
    ?>
    </div>
</div>


</body>

</html>

我希望将用户的表单数据插入数据库或根据我的 index.php 显示任何错误

标签: phphtml

解决方案


if($this->insertintodb($username, $password))$this->errors= 
     ['Creation Success!!!'];

如果您的函数 insertintodb 无法插入,它将返回 false 并且您不会为 false 结果设置任何错误。


推荐阅读