Validate if there is a record in the database

2

I am trying to validate if there is a record in the mysql database by means of an alert with php but it does not generate that alert

if (isset($_POST['numExp']) && !empty($_POST['numExp'])) {


    $consulta=$PDO->prepare("SELECT * FROM registro WHERE numExp=".$numExp);
    if (mysql_num_rows($consulta)=0) 
        { 
            echo '<script>
                    alert("ERROR AL REGISTRAR");
                    </script>'; 
        }else{

        $sql=$PDO->prepare("INSERT INTO fechas_tbl (numExp, fecha) VALUES (:numExp,:fecha)");

        $sql->bindParam(':numExp',$numExp);
        $sql->bindParam(':fecha',$fecha);
        $sql->execute();

        }
    
asked by matteo 24.06.2016 в 21:51
source

3 answers

1

Viewing your code is combining the drivers for the connection to mysql, if you use PDO, it should be as follows:

if (isset($_POST['numExp']) && !empty($_POST['numExp'])) {
  $numExp = $_POST['numExp'];

  $consulta=$PDO->prepare("SELECT * FROM registro WHERE numExp=".$numExp);
  $consulta->execute();
  $num_rows = $consulta->fetchColumn();

  if ($num_rows==0){ 
    echo '<script>
          alert("ERROR AL REGISTRAR");
          </script>'; 
  }else{
    $sql=$PDO->prepare("INSERT INTO fechas_tbl (numExp, fecha) VALUES (:numExp,:fecha)");

    $sql->bindParam(':numExp',$numExp);
    $sql->bindParam(':fecha',$fecha);
    $sql->execute();

  }
}
    
answered by 24.06.2016 / 22:12
source
3

Maybe this is what you are missing

$consulta=$PDO->prepare("SELECT * FROM registro WHERE numExp=".$numExp);

$consulta->execute();
$result = $consulta->fetchAll(PDO::FETCH_ASSOC);
if($result){//aqui va tu script de error}
    
answered by 30.06.2016 в 21:43
-2
echo '<script language="javascript">alert("Error.");</script>'; 

That could help you.

    
answered by 28.11.2016 в 05:08