Error trying to insert - Call a member function prepare () on a non-object

0

I'm trying to check if a field is repeated in the database, but when I run the file, I get the following error:

Fatal error: Call to a member function prepare() on a non-object in ***** on line 39

My code line 39 is as follows:

$stm = $pdo->prepare("SELECT * FROM imoveis WHERE codigodoimovel = '$codigodoimovel'");

It is the query I make, to check if the field already exists in the database.

My full code:

    <?php

define( 'MYSQL_HOST', '***' );
define( 'MYSQL_USER', '***' );
define( 'MYSQL_PASSWORD', '***' );
define( 'MYSQL_DB_NAME', '***' );

$PDO = new PDO( 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB_NAME, MYSQL_USER, MYSQL_PASSWORD );

try
{
    $PDO = new PDO( 'mysql:host=' . MYSQL_HOST . ';dbname=' . MYSQL_DB_NAME, MYSQL_USER, MYSQL_PASSWORD, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
}
catch ( PDOException $e )
{
    echo 'Erro ao conectar com o MySQL: ' . $e->getMessage();
}

$codigointerno          = addslashes ($_POST['codigointerno']);
$codigodoimovel         = addslashes ($_POST['codigodoimovel']);
$fotos                  = addslashes ($_POST['codigointerno']);
$fotocapa               = addslashes ($_POST['fotocapa']);
$motivo                 = addslashes ($_POST['slcmotivo']);
$tipo                   = addslashes ($_POST['slctipo']);
$quartos                = addslashes ($_POST['slcquartos']);
$suites                 = addslashes ($_POST['slcsuites']);
$aream2                 = addslashes ($_POST['txtaream2']);
$garagem                = addslashes ($_POST['slcgaragem']);
$condominio             = addslashes ($_POST['txtcondominio']);
$iptu                   = addslashes ($_POST['txtiptu']);
$caracteristicas        = addslashes (implode( "<br>" , $_POST['chkcaracteristicas']));
$titulo                 = addslashes ($_POST['txttitulo']);
$descricao              = addslashes ($_POST['txtdescricao']);
$preco                  = addslashes ($_POST['txtpreco']);
$localizacao            = addslashes ($_POST['txtcep']);
$endereco               = addslashes ($_POST['txtendereco']);
$destaque               = addslashes ($_POST['slcdestaque']);

$stm = $pdo->prepare("SELECT * FROM imoveis WHERE codigodoimovel = '$codigodoimovel'");
$stm->execute();

$nlinhas = $stm->rowCount(); 

if ($nlinhas!=0)
{
    echo "<script>alert('Imovel já cadastrado!');";
}

else

{
    $stm = $pdo->prepare("INSERT INTO imoveis (codigointerno, codigodoimovel, fotos, fotocapa, motivo, tipo, quartos, suites, aream2, garagem, condominio, iptu, caracteristicas, titulo, descricao, preco, localizacao, endereco, destaque) VALUES ('$codigointerno', '$codigodoimovel', '$fotos', '$fotocapa', '$motivo', '$tipo', '$quartos', '$suites', '$aream2', '$garagem', '$condominio', '$iptu', '$caracteristicas', '$titulo', '$descricao', '$preco', '$localizacao', '$endereco', '$destaque')");

    $stm->execute();

    echo "<script>alert('Imovel inserido com sucesso!'); window.top.location.href = 'http://portolarimoveis.com.br/painel';</script>";

}

?>

I would like to understand where the error is.

Thanks in advance!

    
asked by anonymous 21.07.2018 / 16:35

1 answer

0

You must create the object with the same name that you want to use it with. You are creating the object as $PDO = new PDO , but you want to use it as $pdo->prepare . Or use $PDO or $pdo in both operations.

    
21.07.2018 / 16:45