Get a value from another field other than id

0

I have the following tables:

1 - AssignmentsLogs with the following fields: Id, OrderNode, Contributor and DataAtribution.

2 - StateLoves with the following fields: id, StatusId, and Status.

The OrderID is equal to the IdEst.

Now I want that when I insert into the AssignList table that it also inserts me into the Switches table, but I want it to fetch the value of the FirstData from the first table automatically and insert me into the SwitchesList table in the IdState field.

If it was to get the id I used the following code:

$sql = "INSERT INTO AtribuicaoLuvas (NumPedido,Colaborador,DataAtribuicao)
VALUES ('$pedido','$colaborador','$data')";

if ($conn->query($sql) === TRUE) {
$last_id = $conn->insert_id;
}

$sql1 = "INSERT INTO EstadoLuvas ('IdEstado','Estado') 
VALUES ('$last_id','$estado')"; 

if ($conn->query($sql1) === TRUE);

But in this case how do I do it?

    
asked by anonymous 05.01.2018 / 18:45

1 answer

-1

Considering that you are using PDO.

First let's "prepare ()" the query, changing the values sent by the user by? (this also solves the SQL injection problem).

In "execute ()" you send an array with the value of each field.

If the error code == 00000 then everything worked out.

<?php 
$sql = "INSERT INTO AtribuicaoLuvas (NumPedido, Colaborador, DataAtribuicao) VALUES (?, ?, ?)";

$consulta = $pdo->prepare($sql);
$consulta->execute(array($pedido, $colaborador, $data);

if ($consulta->errorCode() == '00000') {
  $last_id = $pdo->lastInsertId();

  $sql = "INSERT INTO EstadoLuvas ('IdEstado','Estado') VALUES (?, ?)"; 

  $consulta = $pdo->prepare($sql);
  $consulta->execute(array($last_id, $estado);
}
?>
    
06.01.2018 / 15:21