Ver Mensaje Individual
  #7 (permalink)  
Antiguo 08/10/2012, 14:06
e_daniel316
 
Fecha de Ingreso: marzo-2008
Mensajes: 94
Antigüedad: 16 años, 1 mes
Puntos: 0
Respuesta: Ver si existe un registro antes de insertar

Cita:
Iniciado por zalito12 Ver Mensaje
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;
}
}
tienes razon el if se ejecuta, hice lo que me dijiste y
echo mysql_num_rows($result); devuelve "0".