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!
A primary mistake!!! It’s tiredness! Thank you!
– Hebert Richard Masseno Dias