Problem with While in Mysql Query

1

Good friends I hope you can help me, I am saving data provided by the user in 2 different tables, when saving the second data I get this message Fatal error: Maximum execution time of 30 seconds exceeded

Query to Verify Existence in the DB

     $conta=0; 
    $instruccion="SELECT rifem FROM empresa WHERE rifem = '".$rif."'";// Seleccionamos que no Exita

        $instruccions="SELECT DISTINCT registrocliente.contactoc FROM proworld_cliente inner join registrocliente on(proworld_cliente.id_proworld='".$proworld."' AND proworld_cliente.id_cliente=registrocliente.id ) WHERE registrocliente.contactoc='".$comtact."'"; // Seleccionamos que no Exita

        $consulta=mysqli_query($con,$instruccion) or die ("Fallo la Consulta de Empresas");

        $consultas=mysqli_query($con,$instruccions) or die ("Fallo la Consulta de Cliente");

        $num_resultado=mysqli_num_rows($consulta);

        $num_resultados=mysqli_num_rows($consultas);

  if ($num_resultado==0){

            if ($num_resultados==0){

I make the first Insert in the first Table:

     $instruccion1= "INSERT INTO empresa(razonsocial,rifem,referencia,direccionem) value 
                                (UPPER('$razons'), '$rif', UPPER('$ref'), UPPER('$direcc'))";
     $consultaemp= mysqli_query($con,$instruccion1) or die ("Fallo al guardar la empresas");

After having made that insert I must find the ID of that previously inserted data, I do the DO While so that it does not leave the Cycle with a Empty value because if I remove the DO WHILE the query follows its course and many sometimes it does not keep all the values or does not find that data (ID)

do{
    $instruccion2="SELECT id FROM empresa WHERE rifem = '".$rif."'";
    $consulta2=mysqli_query($con,$instruccion2);
        while($resul=mysqli_fetch_array($consulta2)){
                $idempresa=$resul[0];
        }
        if ($idempresa>0){$conta=1;}

}while ($conta<=0);

second insert, in this case I need that the value ($ company) is greater than 0 or you receive the data of the first insert and previously you can insert it again

 $sql="INSERT INTO registrocliente(contactoc,tipoc,fechainicioc,statusc,zonac,visibilidadc,notac,precioc,diascreditoc,diastoleranciac,limitecreditoc,id_pe,tipo_pe) VALUES ('$comtact', '$tipocl', '$fechaini', '$stac', '$zona', '$visi', UPPER('$nota'), '$precio','$diascre','$diasto','$limicre', '$idempresa', '$esempresa')";

ASI WOULD SEE MY COMPLETE CODE

  $instruccion="SELECT rifem FROM empresa WHERE rifem = '".$rif."'";// Seleccionamos que no Exita
        $instruccions="SELECT DISTINCT registrocliente.contactoc FROM proworld_cliente inner join registrocliente on(proworld_cliente.id_proworld='".$proworld."' AND proworld_cliente.id_cliente=registrocliente.id ) WHERE registrocliente.contactoc='".$comtact."'"; // Seleccionamos que no Exita
        $consulta=mysqli_query($con,$instruccion) or die ("Fallo la Consulta de Empresas");
        $consultas=mysqli_query($con,$instruccions) or die ("Fallo la Consulta de Cliente");  
        $num_resultado=mysqli_num_rows($consulta);
        $num_resultados=mysqli_num_rows($consultas);

        if ($num_resultado==0){

            if ($num_resultados==0){

                $instruccion1= "INSERT INTO empresa(razonsocial,rifem,referencia,direccionem) value 
                                (UPPER('$razons'), '$rif', UPPER('$ref'), UPPER('$direcc'))";
                $consultaemp= mysqli_query($con,$instruccion1) or die ("Fallo al guardar la empresas");

                do{
                    $instruccion2="SELECT id FROM empresa WHERE rifem = '".$rif."'";
                    $consulta2=mysqli_query($con,$instruccion2);
                        while($resul=mysqli_fetch_array($consulta2)){
                             $idempresa=$resul[0];
                        }
                        if ($idempresa>0){$conta=1;}

                }while ($conta<=0); 

                $sql="INSERT INTO registrocliente(contactoc,tipoc,fechainicioc,statusc,zonac,visibilidadc,notac,precioc,diascreditoc,diastoleranciac,limitecreditoc,id_pe,tipo_pe) VALUES ('$comtact', '$tipocl', '$fechaini', '$stac', '$zona', '$visi', UPPER('$nota'), '$precio','$diascre','$diasto','$limicre', '$idempresa', '$esempresa')";
                $query_new_insert=mysqli_query($con,$sql);
    
asked by David 01.10.2017 в 03:47
source

1 answer

0

Declare $idempresa out of if s assigned zero as the initial value; remove the do { [...] } while($conta>0); (practice assuming that the INSERT previous yes was made without failure opposite will occur the or die{[...]} ) process the INSERT that you have after the do [...] while(...); to get the $idempresa that was generated uses mysqli_insert_id (do not forget to look at the considerations for that works)

$idempresa = mysqli_insert_id($con);

Among the considerations I remember for the above is that (in your case) the field Id of your table empresa must be of type autoincremento .

I suggest prior to this that I indicate; do INSERT to empresa (manually or using SQL that you apply) to verify that Id is automatically generated with an expected value (that is, a value other than NULL and greater than 0 correctly).

    
answered by 01.10.2017 / 05:52
source