Problem avoiding duplicate records

0

First of all I want to say that I have already reviewed the possible answers to this in this and other forums. As indicated by the title, what I want to do is: if there is a record with the same number, launch an aletra with js and if not, register the new data. I'm using Dreamweaver, so the order and the name of some variables, apart I do not know if this can affect my code with respect to what I want to do.

$consulta = mysql_query("SELECT NUMERO_PLANILLA FROM huawei_rollout WHERE 
NUMERO_PLANILLA = '$NUMERO_PLANILLA'", $conexion);
if(mysql_num_rows($consulta) > 0){
     echo '<script>alert("El numero de Planilla ya existe");</script>';
 }else{

if((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frmGuardar")) {
$insertSQL = sprintf("INSERT INTO huawei_rollout (ID, FECHA, MES_EJECUCION, 
MES_FACTURACION, CONTRATISTA) VALUES (%s, %s, %s, %s)",

                   GetSQLValueString($_POST['ID'], "int"),
                   GetSQLValueString($_POST['FECHA'], "date"),
                   GetSQLValueString($_POST['MES_EJECUCION'], "text"),
                   GetSQLValueString($_POST['MES_FACTURACION'], "text"));



mysql_select_db($database_conexion, $conexion);
$Result1 = mysql_query($insertSQL, $conexion) or die(mysql_error());

$insertGoTo = "ingre_plani_exi.php";
if (isset($_SERVER['QUERY_STRING'])) {
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $_SERVER['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}

}

    
asked by Anderviver 21.06.2017 в 23:30
source

2 answers

0

This answer already has several days, but I post it in case someone serves, I want to clarify some variables this is why I did it from dreamweaver.

$NUMERO_PLANILLA = $_POST['NUMERO_PLANILLA'];

mysql_select_db($database_conexion, $conexion);
$query_numero_planilla = "SELECT NUMERO_PLANILLA FROM huawei_rollout WHERE 
NUMERO_PLANILLA = '$NUMERO_PLANILLA'";
$numero_planilla = mysql_query($query_numero_planilla, $conexion) or 
die(mysql_error());
$row_numero_planilla = mysql_fetch_assoc($numero_planilla);
if(mysql_num_rows($numero_planilla)>0){
echo '<script>alert("Ya existe el numero de planilla ingresado");</script>';
echo '<script>document.write("Redireccionando...");
setTimeout("window.history.go(-1)",1000); </script>';
}else{

query="INSERT INTO huawei_rollout (ID, F..."<COSULTA DE INSERTAR>...");
    
answered by 30.06.2017 / 23:59
source
0

Since (mysql_num_rows ($ query)> 0) {is not working for you, try the following:

       $consulta = mysql_query("SELECT NUMERO_PLANILLA FROM huawei_rollout WHERE 
        NUMERO_PLANILLA = '$NUMERO_PLANILLA'", $conexion);

$numpero_plantilla = 0;
while ($fila = mysql_fetch_assoc($consulta)) {
    $numpero_plantilla = $fila['NUMERO_PLANILLA'];
}

if($numpero_plantilla  = 0){
    echo '<script>alert("El numero de Planilla ya existe");</script>';
}else{

    if((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "frmGuardar"){
       $insertSQL = sprintf("INSERT INTO huawei_rollout (ID, FECHA, MES_EJECUCION, 
MES_FACTURACION, CONTRATISTA) VALUES (%s, %s, %s, %s)",

    GetSQLValueString($_POST['ID'], "int"),
    GetSQLValueString($_POST['FECHA'], "date"),
    GetSQLValueString($_POST['MES_EJECUCION'], "text"),
    GetSQLValueString($_POST['MES_FACTURACION'], "text"));



    mysql_select_db($database_conexion, $conexion);
    $Result1 = mysql_query($insertSQL, $conexion) or die(mysql_error());

    $insertGoTo = "ingre_plani_exi.php";
    if (isset($_SERVER['QUERY_STRING'])) {
        $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
        $insertGoTo .= $_SERVER['QUERY_STRING'];
    }
    header(sprintf("Location: %s", $insertGoTo));
}

I was wondering about the version since you can use PDO from 5.1, you only have to activate the extensions, if you want to know how to do it in PDO I can leave you the answer of how it would be in PDO

    
answered by 21.06.2017 в 23:56