Take larger ID and insert Bank

Asked

Viewed 72 times

1

My doubt is the following need to take the ID with higher value and enter the information in it. The PHP file is working but I need it to identify the ID with higher VALUE and insert it instead of creating another.

Homereport.php :

<?php
 include "conexao.php";
 session_start();

 $os_id = $_POST['os_id'];
 $localizacao = $_POST['localizacao'];
 $atividade = $_POST['atividade'];
 $observacao = $_POST['observacao'];
 $data = $_POST['data'];




     // Relatorio será iniciado
            $sql_insert = "INSERT INTO fotos (os_id, localizacao, atividade, observacao, data) VALUES(:OS_ID, :LOCALIZACAO, :ATIVIDADE, :OBSERVACAO, :DATA)";
            $stmt = $PDO -> prepare($sql_insert);

            $stmt ->bindParam(':OS_ID', $os_id);
            $stmt ->bindParam(':LOCALIZACAO', $localizacao);
            $stmt ->bindParam(':ATIVIDADE', $atividade);
            $stmt ->bindParam(':OBSERVACAO', $observacao);
            $stmt ->bindParam(':DATA', $data);

            if($stmt -> execute()) {
            $dados = $stmt -> fetch(PDO::FETCH_ASSOC);  
            $retornoApp = array("RELATORIO" => "SUCESSO", "OS_ID"=>$dados['os_id'], "LOCALIZACAO"=>$dados['localizacao'], "ATIVIDADE"=>$dados['atividade'], "OBSERVACAO" =>$dados['observacao'], "DATA" =>$dados['data'] );
} else {
    $retornoApp = array("RELATORIO" => "ERRO");

 }

 echo json_encode($retornoApp);

?>

  • Get the id of where? From the bank?

  • This it would give a select and search in the PHOTOS table the biggest id to insert the information in it

  • It’s not just giving one ORDER BY DESC LIMIT 1 ?

  • But then I put a varialvel and then to do the Insert would be like

1 answer

0


Just select with order by and limit the return to 1 value.

Would look like this:

SELECT id FROM tabela ORDER BY id DESC LIMIT 1;

How are you using PDO, to catch the id would be +- so the result:

$stmt = $pdo->query("SELECT id FROM tabela ORDER BY id DESC LIMIT 1");
$result = $stmt->fetch();
$id = $result['id']; //CAPTURANDO O ID
  • I did it this way: $stmt = $PDO->query("SELECT id FROM photos ORDER BY id DESC LIMIT 1"); $result = $stmt->fetch(); $id = $result['id']; //CAPTURING ID // Report will start $sql_insert = "UPDATE photos set os_id = '$os_id', location = '$location', activity = '$activity', observation = '$note', date = '$date' Where id = '$id'"; $stmt = $PDO -> prepare($sql_insert);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.