How to do an Update to two tables at the same time

0

Can an UPDATE be made to two tables at the same time? I'm doing this but obviously it does not work:

<?php
 $id = $_POST['eIdp'];
 $producto = $_POST['eidPr'];
 $factura = $_POST['eNumero'];
 $eFecha = $_POST['eFecha'];
 $eVencimiento = $_POST['eVencimiento'];
 $estado = $_POST['select2-1'];
 $tipo = $_POST['select2-4'];
 $total = $_POST['eTotal'];
 $referencia = $_POST['select2-3'];


//die()

 $results = "Update pedidos Set Numero='$factura', Fecha='$eFecha', Vencimiento='$eVencimiento', Estado='$estado', Producto='$producto', Tipo='$tipo', Total='$total', Referencia='$referencia' where id= " .$_POST['eIdp'];

"Update referencias Set estadoMaquina='$tipo' where referencia=  $referencia ";



  if ( !mysqli_query($mysqli, $results)) {
    die( 'Error: ' . mysqli_error() );
  }
 mysqli_close($mysqli);
?>

I also have to know if you can do a where with a field like this:

where DI001 = DI001

    
asked by Miguel 12.09.2018 в 10:23
source

2 answers

0

You can have the mistake here:

 $results = "Update pedidos Set Numero='$factura', Fecha='$eFecha', Vencimiento='$eVencimiento', Estado='$estado', Producto='$producto', Tipo='$tipo', Total='$total', Referencia='$referencia' where id= " .$_POST['eIdp'];

"Update referencias Set estadoMaquina='$tipo' where referencia=  $referencia ";

You have to continue writing the corresponding string. For example:

$results = "Update pedidos Set Numero='$factura', Fecha='$eFecha', Vencimiento='$eVencimiento', Estado='$estado', Producto='$producto', Tipo='$tipo', Total='$total', Referencia='$referencia' where id= " .$_POST['eIdp'];

$results.=";Update referencias Set estadoMaquina='$tipo' where referencia=  $referencia ";

With this, your string $ results has both continuous queries.

    
answered by 12.09.2018 в 10:28
0

To arm two queries at once you need mysqli_multi_query($link, $query) to execute the two UPDATE

Also as it says @Jakala you need to concatenate the queries with .=

Your code would look like this:

<?php
     $id = $_POST['eIdp'];
     $producto = $_POST['eidPr'];
     $factura = $_POST['eNumero'];
     $eFecha = $_POST['eFecha'];
     $eVencimiento = $_POST['eVencimiento'];
     $estado = $_POST['select2-1'];
     $tipo = $_POST['select2-4'];
     $total = $_POST['eTotal'];
     $referencia = $_POST['select2-3'];

     $results = "Update pedidos Set Numero='$factura', Fecha='$eFecha', Vencimiento='$eVencimiento', Estado='$estado', Producto='$producto', Tipo='$tipo', Total='$total', Referencia='$referencia' where id= " .$_POST['eIdp'];

     $results.=";Update referencias Set estadoMaquina='$tipo' where referencia=  $referencia ";

     if ( !mysqli_multi_query($mysqli, $results)) {
          die( 'Error: ' . mysqli_error() );
     }

     mysqli_close($mysqli);
?>
    
answered by 12.09.2018 в 13:19