Does not enter the if statement, inside the While

1

The following happens, I'm doing an update, when I enter an invoice from the suppliers, and when I click on a button x a function is executed (I'll place it below) it works well, the stock of the products entered is updated with the invoice of the branch to which you are paid, but what happens if that product does not exist in that branch? Well, I did an if I checked that if it does not show a result, the products will be inserted in the branch, and if they exist, that the stock of each one will be updated, but it does not work, I think the if is wrong. I leave my function (same is long ... PD: the table is because only copy the while I had pra q products will be displayed.):

  <?php 

   function ingresar_stock()

          {//consulta

            $id_if=$this->id_if;
            $id_di="";

    $sql="SELECT detalle_ingreso.id_di, detalle_ingreso.cantidad, detalle_ingreso.costo, productos.nombre_producto, 
            marcas.nombre_marca, ingreso_factura.id_if,ingreso_factura.id_suc, detalle_ingreso.codigo_producto, productos.precio_costo 
            FROM detalle_ingreso 
            INNER JOIN productos ON detalle_ingreso.codigo_producto = productos.codigo_producto
            INNER JOIN ingreso_factura ON detalle_ingreso.id_if = ingreso_factura.id_if 
            INNER JOIN marcas ON productos.id_marca = marcas.id_marca WHERE detalle_ingreso.id_if='$id_if'"; 

    $resultado=mysqli_query($this->conexion,$sql);
    $tabla="";


    while ($datos=mysqli_fetch_array($resultado))

    {               $codigo_producto=$datos["codigo_producto"];
                    $nombre_producto=$datos["nombre_producto"];
                    $nombre_marca=$datos["nombre_marca"];
                    $precio_costo=$datos["precio_costo"];
                    $cantidad=$datos["cantidad"];
                    $costo=$datos["costo"];
                    $id_di=$datos["id_di"];
                    $id_suc=$datos["id_suc"];

        $tabla.="<tr>

                    <td style='width: 15px; text-align: center'>$codigo_producto</td>
                    <td style='width: 190px; text-align: center'>$nombre_producto</td>
                    <td style='width: 20px; text-align: center'>$nombre_marca</td>
                    <td style='width: 20px; text-align: center'>$precio_costo</td>
                    <td style='width: 20px; text-align: center'>$cantidad</td>
                    <td style='width: 20px; text-align: center'>$costo</td>
                    <td style='width: 20px; text-align: center'><button    id_di='$id_di' class='btn btn-sm btn-default eliminar' >Eliminar<span class='glyphicon glyphicon-remove'></span></button></td>
                </tr>         <div class='col-md-2' style='top: 25px;'>";

            //--- Mi consulta, para verificar que no existe el producto en esa sucursal
            $sql_con="SELECT productos.codigo_producto , sucursales.id_suc
             FROM productos_sucursales
             INNER JOIN productos ON productos_sucursales.codigo_producto=productos.codigo_producto
             INNER JOIN sucursales ON productos_sucursales.id_suc=sucursales.id_suc
             WHERE productos_sucursales.id_suc='$id_suc' AND productos_sucursales.codigo_producto='$codigo_producto'";       
             $resultado_con=mysqli_query($this->conexion,$sql_con); 

             if($resultado_con==0){

                 $sql_insert="INSERT INTO productos_sucursales values('','$id_suc','$codigo_producto','$cantidad')";
         $resultado_insert=mysqli_query($this->conexion,$sql_insert);


             }

             else{

                 $sql2="UPDATE productos_sucursales
            SET stock_real=stock_real  + $cantidad
            WHERE codigo_producto=$codigo_producto AND id_suc=$id_suc";
        $resultado2=mysqli_query($this->conexion,$sql2);


              }



    }
    return $tabla;
}

   ?>
    
asked by Kvothe_0077 08.12.2016 в 23:08
source

4 answers

1

You must leave your if

if(!$resultado_con){

}

Since it returns false in case of not finding results.

My answer is similar to that of @ Error404, only that mine saves the evaluation of == false since if evaluates Boolean results therefore if you evaluate !$resultado_con you are looking for the result if it is false (empty) if not, and bring a object that is the result in case your query corresponds to the sentence else

EDITING

Try this way:

$rows = $resultado_con->num_rows;
if($rows == 0){
       //insert
}
    
answered by 08.12.2016 / 23:55
source
1

Good, have you checked what the query returns? will never return 0, returns a object or false if the query has failed. you should receive the result and try it to see what has been returned to you, then do the check for example of product code.

if($resultado_con==0)
    
answered by 08.12.2016 в 23:13
1

The mysqli_query function, as indicated in the documentation , returns false in case of not returning anything. Therefore, you would have to change your if to:

if($resultado_con==false){
   //Código si es falso (si no hay registros)
}else{
  //Código si es verdadero (hay registros)
}
    
answered by 08.12.2016 в 23:16
0

1) Even though it's a test project, it prevents someone from giving you a headache and lost money by escaping the input (even if you're the one who generates it).

SQL Injection

About the question, I usually avoid making the query to verify if an item is in the database using what I call an upsert. (This only works if you have primary keys defined, I usually use it with combined pk's).

The query is like this:

INSERT INTO table (id_prod, id_suc, value1, value2, value3)
VALUES (1, 2, '1','2','3')
ON DUPLICATE KEY UPDATE
table.value1 = VALUES(table.value1),
table.value2 = VALUES(table.value2);

This way you only have to keep a query, for this type of cases that manages both, insert and update.

If this way of doing things does not adapt to your style, as others have said, you must change your validation to work validating if the result returned by the query is false.

if($resultado_con == false) { doSomething(); }

    
answered by 08.12.2016 в 23:33