'Do not repeat data in MySQL with PHP

I am doing a personal project with PHP and Mysql. They are my first steps. I am saving two pieces of data using a form in my database. Everything works fine on localhost with xampp. My problem comes when I have uploaded it to my server. The connection to the database is correct, it displays the data on the site and saves it correctly. The problem is that it saves the data repeatedly, something that did not happen locally. This is the code of my registry.php file

<?php
include './config/conexion.php';

$cantante = addslashes(htmlspecialchars($_GET['cantante']));
$cancion = addslashes(htmlspecialchars($_GET['cancion']));

    # SQL
    $query = "INSERT INTO datos(cantante, cancion)
              VALUES ('$cantante', '$cancion') ";
    #Verificar que los datos cancion no se repitan en la BD
    $verificar = mysqli_query($conexion, "SELECT * FROM datos WHERE cancion='$cancion'");
    $menu = mysqli_query($conexion, "SELECT * FROM datos ORDER BY id DESC LIMIT 12");

   
if (mysqli_num_rows($verificar) == $cancion > 0) {
 
    
}else {

    $ejecutar = mysqli_query($conexion, $query);
    
    if ($ejecutar) {
        $ok = "Letra de canción " . $cancion;
    }else{
       
       header('Location: /error.php');
       die();
    }
    
    mysqli_close($conexion);
    
}

This code saves the data correctly, but does not validate the $cancion variable in case it is repeated. In the event that it is repeated, the execution should continue without making any warning:

if (mysqli_num_rows($verificar) == $cancion > 0) {

And if it doesn't exist, save it and continue execution.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source