Ver Mensaje Individual
  #5 (permalink)  
Antiguo 08/10/2012, 08:30
Avatar de zalito12
zalito12
 
Fecha de Ingreso: noviembre-2011
Ubicación: Coruña, España
Mensajes: 430
Antigüedad: 12 años, 5 meses
Puntos: 67
Respuesta: Ver si existe un registro antes de insertar

Has dicho que no te ejecuta el if, que te pasa directamente al insert, eso es mentira, el if si te lo está ejecutando, ya que el inserte está dentro del else, lo único que la condición que le has puesto al if te devuelve false, por algún motivo.
Pon algunas lineas de control para ver el fallo, por ejemplo:
Código PHP:
<?php 
$state 
false;
if (
$_POST['action'] == "add") { 
    
$cnx mysql_connect("localhost""root""1234");
    
mysql_select_db("htp"$cnx);
    
    
$rut $_POST['rut'];
    
$consul ="SELECT rut FROM usuario WHERE rut = '".$rut."'";

    
$result mysql_query($consul,$cnx) or die(mysql_error());
    echo 
mysql_num_rows($result);
    
//Si no sucede nada extraño y no tira ningún fallo, prueba: $result = mysql_query($consul) or die(mysql_error());

    
if (mysql_num_rows($result) > 0) {
        echo 
" <script> 
        alert('Error, El rut ya existe'); 
        </script> "
;
        
    } else {
    
    
$sql "INSERT INTO usuario (rut, nombre, apellido, direccion, sexo, region, comuna, telefono, email) ";
    
$sql.= "VALUES ('".$_POST['rut']."', '".$_POST['nombre']."', '".$_POST['apellido']."','".$_POST['direc']."',  '".$_POST['sexo']."','".$_POST['region']."','".$_POST['comuna']."','".$_POST['telefono']."','".$_POST['email']."')";
       
$sql mysql_query($sql$cnx) or die(mysql_error());
    
$state true;
}
}