lundi 25 mai 2015

Execute INSERT in case no conditional statement occurs

I have a PHP file that checks if any of the 3 possible words given trough $_POST have already been created on DB.

If any of those words do exist on any of DB records, it echoes an error.

If none of those words exist on any DB records, it executes an INSERT query.

This code checks correctly wheter any of the 3 given words (if given) exists, and if it does, the error is echoed properly. But if none of those exists, the website just freezes and no insert is made.

How could I ask PHP to run the INSERT query (what is now placed on the ELSE statement) in case all IF statements are false?

    $ca_key1 = $_POST['ca_key1'];
    $ca_key2 = $_POST['ca_key2'];
    $ca_key3 = $_POST['ca_key3'];

$selectKeys = mysqli_query($con, "SELECT ca_key1,ca_key2,ca_key3 FROM ws_campaigns WHERE ca_fk_us_id='$id'");

    if ($ca_key1!==""){            
        while($registroKeys = mysqli_fetch_array($selectKeys)){
           if ($registroKeys['ca_key1']===$ca_key1 || $registroKeys['ca_key2']===$ca_key1 || $registroKeys['ca_key3']===$ca_key1){
               echo "No se ha creado la campaña. <br>La palabra '".$ca_key1."' ya está siendo utilizada en una campaña previa.";
           } 
        }      
    }

    else if ($ca_key2!==""){            
        while($registroKeys = mysqli_fetch_array($selectKeys)){
           if ($registroKeys['ca_key1']===$ca_key2 || $registroKeys['ca_key2']===$ca_key2 || $registroKeys['ca_key3']===$ca_key2){
               echo "No se ha creado la campaña. <br>La palabra '".$ca_key2."' ya está siendo utilizada en una campaña previa.";
           } 
        }      
    }

    else if ($ca_key3!==""){            
        while($registroKeys = mysqli_fetch_array($selectKeys)){
           if ($registroKeys['ca_key1']===$ca_key3 || $registroKeys['ca_key2']===$ca_key3 || $registroKeys['ca_key3']===$ca_key3){
               echo "No se ha creado la campaña. <br>La palabra '".$ca_key3."' ya está siendo utilizada en una campaña previa.";
           } 
        }      
    }

    else{
        //DEVOLUCIÓN ID CAMPAÑA
        if ($result = $con->query("INSERT INTO ws_campaigns (ca_id, ca_name, ca_content, ca_fk_us_id, ca_img, ca_prefix,ca_key1,ca_key2,ca_key3) VALUES ('','$ca_title', '$ca_content','$id','$ca_img','$ca_prefix','$ca_key1','$ca_key2','$ca_key3')")) {
           echo $con->insert_id;
        }
    }

Aucun commentaire:

Enregistrer un commentaire