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;
}
?>