php - 提交按钮不向数据库发送数据并且不显示错误
问题描述
我正在尝试在我的数据库中创建一个新用户但没有响应,数据没有插入数据库并且没有显示错误。
我试图在互联网上研究这个问题,但似乎没有得到我的问题的答案
这是我的 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 显示任何错误
解决方案
if($this->insertintodb($username, $password))$this->errors=
['Creation Success!!!'];
如果您的函数 insertintodb 无法插入,它将返回 false 并且您不会为 false 结果设置任何错误。
推荐阅读
- visual-studio-code - VS Code Python 扩展:Linter 的设置
- angularjs - 如何防止最初为空集的变量出现模板错误?
- android - 为什么 ConsentForm.load 有时会导致 android ANR?
- c# - 由于距离而改变游戏对象颜色
- java - 获取所有枚举常量
- c - C中损坏的大小与prev_size
- android - 升级 android studio 3.1.3 后无法解析 sysmbol 'GoogleApiAvailability'
- sql - 检索所有链接记录的 SQL
- c# - 无法将 T 的对象转换为具体类型
- python - 如何在另一个视图中访问 Django 模型对象?