简体   繁体   中英

Mysqli prepared statement php

I am new to prepared statement and I'm figuring out how to do insert. I check all the file and all are working fine except it does not insert and i did not get any error also. Could you please shed some light to this guys.

I notice also that if I do it without a class, prepared statement will work.

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);

?>

Just change your code like below,

dataProcessor.inc.php

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

as per the refernce link - php mysql server gone away

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM