Pick up bigger ID and insert Bank

1

My doubt is that I need to get the ID with the highest value and enter the information in it. The PHP file is working but I need it to identify the ID with the highest VALUE and insert it into it instead of creating another one.

StartReport.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);

? >

    
asked by anonymous 12.11.2018 / 00:38

1 answer

0

Just do a select with order by and limiting the return to 1 value.

It would look like this:

SELECT id FROM tabela ORDER BY id DESC LIMIT 1;

As you are using PDO , to get 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
    
12.11.2018 / 01:30