How not to insert data into the database with mysqli and php?

Asked

Viewed 57 times

0

I wonder if anyone can help me is that I don’t want to enter in the database values where the title is equal but I’m not getting. With the code below it always inserts in the database, not this filter not allow to insert data with the same title in the database. Note: I must use mysqli. Thank you.

function inserirNaTabelaFilmesRegisto(
        $db, //ponteiro pelo qual se faz o acesso à SGBDR
        $titulo, $urlImagem, $hrefAssistir, $categoria, $classificacao
    )
    {

//var_dump($db);
//$db = estabelecerLigacaoSGBDR();
if ($db!==false){
    $select = "select * from  filme where titulo = '$titulo'";
    if($result = $db->query($select)===true) {
 echo "ERRO!<br><br><br><br><br><br><br><br>";
    }else{
    $q = "insert into ".BD.".".TABELA_FILME;
           $q.=" values (null, '$titulo', '$urlImagem', '$hrefAssistir', '$categoria', '$classificacao');";
    $db->query($q);
    $e = mysqli_errno($db);
    $eM = mysqli_error($db);
    echo $eM;
    if ($e===0){
        return true;
    }
    }
}//if
return false;
}//inserirNaTabelaApontamentosRegisto
  • Give UNIQUE in the column, this will prevent duplicate information

  • You can properly design the database and set the column as unique, not needing to do code for it.

  • It turned out @Inkeliz and @Andersoncarloswoss! Thank you very much. God Bless you.

  • Maybe it helps to put it as the only one in the bank or make a query to the bank and check if it already exists.

  • In this link: http://blog.thiagobelem.net/gui-pratico-de-mysqli-no-php teaches how to make a SELECT query with mysqli, when taking the data compares with which you want to check if it already exists.

  • Thanks @Henrique Buzin for the info.

Show 1 more comment
No answers

Browser other questions tagged

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