0
Well, I started my PDO studies recently, I’m trying to include a class in a form, but it’s giving the error:
failed to open stream:
This either if I use direct include with the path, or if I use this function: (Note: In the Method class).
function __autoload($class) {
$class = strtolower($class);
// Inclui a classe que precisamos
include_once("classes/class-{$class}.php");
}
Connectionfactory.php
class ConnectionFactory{
static function getConnection(){
$con = new PDO("mysql:host=localhost;dbname=projeto_php_pdo", "root", "");
return $con;
}
}
Methodspdo.php
class metodosPDO {
function __autoload($class) {
$class = strtolower($class);
// Inclui a classe que precisamos
include_once("classes/class-{$class}.php");
}
static function inserirCliente($nome, $sobrenome) {
$con = ConnectionFactory::getConnection();
$stmt = $con->prepare("INSERT INTO pessoa(nome, sobrenome) VALUES(?, ?)");
$stmt->bindParam(1, $nome);
$stmt->bindParam(2, $sobrenome);
$stmt->execute();
$stmt->close();
return;
}
}
pag_cadastro.php
<?PHP
// INCLUIR AQUI
?>
<form action="#" method="post">
<input type="text" name="nome" value="" />
<input type="text" name="sobrenome" value="" />
<input type="submit" value="Cadastrar" name="btn" />
</form>
<?php
$nome = $_POST['nome'];
$sobrenome = $_POST['sobrenome'];
$verf = metodosPDO::inserirCliente($nome, $sobrenome);
?>
The folder structure is like this:
Files of source code: PDO ~> Methodspdo.PHP / Connectionfactory.php Formularios ~> pag_cadastro.php
It won’t be for yours
__autoload
convert everything to lowercase? And the files are camelCase?– Leite
This was one of the errors, the other was the __autoload path, the right include would be "include_once("../PDO/{$class}.php");". Thanks for the help!
– Leandro