Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysqli编写的php语句_Php_Mysqli_Prepared Statement - Fatal编程技术网

Mysqli编写的php语句

Mysqli编写的php语句,php,mysqli,prepared-statement,Php,Mysqli,Prepared Statement,我对准备好的声明还不熟悉,我正在琢磨如何插入。我检查了所有的文件,所有的工作都很好,除了它没有插入,我也没有得到任何错误。你能帮我解释一下吗 我还注意到,如果我在没有类的情况下做这件事,预先准备好的语句就可以了 index.php <!DOCTYPE html> <html> <head> <title>Title of the document</title> </head> <body> <form

我对准备好的声明还不熟悉,我正在琢磨如何插入。我检查了所有的文件,所有的工作都很好,除了它没有插入,我也没有得到任何错误。你能帮我解释一下吗

我还注意到,如果我在没有类的情况下做这件事,预先准备好的语句就可以了

index.php

<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>

<body>

<form action="insertData.php" method="POST">

<input type="text" name="username" placeholder="username">
<input type="password" name="password" placeholder="password">
<input type="text" name="firstname" placeholder="firstname">
<input type="text" name="email" placeholder="email">
<input type="submit" name="submit" value="submit">


</form>

</body>

</html>

文件标题
dbh.inc.php

<?php

class Dbh{

private $servername;
private $username;
private $password;
private $dbname;

protected function connect(){

$this->servername = "localhost";
$this->username = "root";
$this->password = "";
$this->dbaname = "test-login-db";

$conn = new mysqli($this->servername, $this->username, $this->password, $this->dbaname);

return $conn;

}
}

?>

dataProcessor.inc.php

<?php

class DataProcess extends Dbh{



public function insertAllUsers($username, $password, $firstname, $email){

$sql = "INSERT INTO user (username, password, firstname, email) VALUES (?, ?, ?, ?)";
$stmt = $this->connect()->prepare($sql);
$stmt->bind_param("ssss", $username, $password, $firstname, $email);
$stmt->execute();
$stmt->close();


}
}

?>

insertData.php

<?php

include "includes/dbh.inc.php";
include "includes/dataProcessor.inc.php";

$username = $_POST['username'];
$password = $_POST['password'];
$firstname = $_POST['firstname'];
$email = $_POST['email'];

$result = new DataProcess();
$result->insertAllUsers($username, $password, $firstname, $email);

?>

只需按如下方式更改代码

dataProcessor.inc.php

$stm = $this->connect();
$stmt = $stm->prepare($sql); // This line
$stmt->bind_param("ssss", $username, $password, $firstname, $email);
$stmt->execute();
$stmt->close();

根据参考链接-

为什么会被否决?OP提供了一个近乎完美的MCVE和一个清晰的问题?也就是说,您可以查看一下MySQLi错误日志吗@Loek一个关于数据库问题的问题,其中包括“我也没有得到任何错误”,而显示的代码没有尝试实际检查错误,这是值得投反对票的。我们不应该在这里一遍又一遍地解释同样的事情。“作为一个初学者”不能一直成为你自己不提前阅读基础知识的借口。这很公平!以后我会更加严格;)可能重复的抱歉没有包括错误。我也很晚才检查。“执行失败:(2006)MySQL服务器已消失”我尝试了这个,但仍然无法插入。也在谷歌上搜索,但找不到有效的解决方案。感谢您的努力。在我将wamp更改为64位并使用您的代码后,它现在可以工作了$stmt=$this->connect()->prepare($sql);直到我把它和你的样品做得一样,它才起作用。我不知道原因是什么。我在本地运行代码时遇到了一个错误“mysql消失了”,当时我得到了这个链接。在这里我们可以看到解释。