0

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

?>
tereško
  • 57,247
  • 24
  • 95
  • 149
sonny
  • 47
  • 6
  • 1
    Why is this downvoted? OP provided a near perfect MCVE and a clear question? That said, can you take a look at the MySQLi error log? https://stackoverflow.com/questions/17053466/how-to-display-errors-for-my-mysqli-query – Loek Jul 09 '18 at 10:31
  • 1
    @Loek a question about a database problem that includes “and i did not get any error also” while showing code that does not make _any_ attempt to actually check for errors, is worth a downvote IMHO. We should not have to explain that same stuff over and over and over again here. “Being a beginner” can’t be the excuse all the time for not reading up on basics like this yourself upfront. – CBroe Jul 09 '18 at 11:25
  • Fair enough! I shall be more strict in the future ;) – Loek Jul 09 '18 at 11:27
  • Possible duplicate of [How to display errors for my MySQLi query?](https://stackoverflow.com/questions/17053466/how-to-display-errors-for-my-mysqli-query) – CBroe Jul 09 '18 at 11:28
  • Sorry for not including the error guys. I have check it also late. "Execute failed: (2006) MySQL server has gone away" – sonny Jul 09 '18 at 11:30

1 Answers1

0

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

KMS
  • 567
  • 3
  • 15
  • I tried this one but still i cannot make it to insert. Been googling it also but couldn't find a working solution. Appreciate your effort though. – sonny Jul 09 '18 at 11:19
  • It is working now after i change wamp to 64bit and use your code. $stmt = $this->connect()->prepare($sql); This does not work until i make it same as your sample. I don't know whats the explanation. – sonny Jul 10 '18 at 06:46
  • I got an error "mysql gone away" while running your code in local, that time i got this link https://stackoverflow.com/questions/23695856/php-mysql-server-gone-away . Here we can see the explanation. – KMS Jul 10 '18 at 06:58