Error trying to insert - Call to a Member Function prepare() on a non-object

Asked

Viewed 475 times

0

I am trying to check before if a field is repeated in the database, but while running the file I get the following error:

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

My line 39 of the code is as follows::

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

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

My complete 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’d like to understand where the mistake is.

Thank you in advance!

1 answer

0


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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.