How to search by ID in MySQL

1

I am developing a system of entry and exit of vehicles of the company, in the index I made 2 inputs one of entrada and other saída , when I click on input it appears the screen of register of the vehicle like placa, nome do motorista, data da entrada e hora da entrada , when click on insert I put it to generate a protocol that pulls from id .

With the exit of the vehicle I was without idea of it I decided to do the following, When the vehicle is to leave the company the person click there in exit aparacer a search button, from there I put the protocol and the driver's name and the driver's name appears of the same, because I am not able to do the search system, help me with the search system, here is the code below:

if (isset($_POST["enviar"])) {

$placa      = isset($_POST["placa"]) ? $_POST["placa"] : '';
$nome       = isset($_POST["nome"]) ? $_POST["nome"] : '';
$dataentrada  = isset($_POST["dataentrada"]) ? $_POST["dataentrada"] : '';
$horaentrada  = isset($_POST["horaentrada"]) ? $_POST["horaentrada"] : '';
$observacao = isset($_POST["observacao"]) ? $_POST["observacao"] : '';
$id = "id";

$sql  = "INSERT INTO entrada(placa,nome,dataentrada,horaentrada,observacao) VALUES('$placa','$nome','$dataentrada','$horaentrada','$observacao')";
$resultado = mysql_query($sql);

$sql1 = mysql_query("SELECT * FROM entrada");
$resultado1 = mysql_fetch_array($sql1) or die (mysql_error());

echo ('<center> <b>
Usuário cadastrado com sucesso!
      </b> <br />
Seu protocolo é: #' . $resultado1["id"] . ' </center>'); } ?>

INPUT HTML CODE

<form action="" method="post">
Placa:                       <input type="text" name="placa" /> <br />
Nome do Condutor (0pcional): <input type="text" name="nome" /> <br />
Data de Entrada:             <input type="date" name="dataentrada" /> <br />
Hora Entrada:                <input type="time" name="horaentrada" /> <br />
Observação:                  <textarea name="observacao" cols="30"> </textarea> <br />
<input type="submit" name="enviar" value="Registrar" />
    
asked by anonymous 13.07.2014 / 04:50

3 answers

3

The easiest thing to do is to use the mysql_insert_id () command to get the last inserted ID, after you do the INSERT, and you delete the select.

It would look like this:

$sql  = "INSERT INTO entrada(placa,nome,dataentrada,horaentrada,observacao) VALUES('$placa','$nome','$dataentrada','$horaentrada','$observacao')";

$resultado = mysql_query($sql);
$idinserido = mysql_insert_id();

echo ('<center> <b>
Usuário cadastrado com sucesso!
      </b> <br />
Seu protocolo é: #' .$idinserido . ' </center>');
    
13.07.2014 / 10:54
2

Since you do not set the id in the code, it should be auto-incremented in the database, so what you are looking for is probably the largest value:

$sql1 = mysql_query("SELECT MAX(id) FROM entrada");

Note that it is never recommended to use SELECT * see #

And this code is not secure because I'm only getting the most value, if you want your code to be more secure I suggest using this:

 $sql1 = mysql_query("SELECT id FROM entrada ORDER BY id DESC WHERE placa = $placa LIMIT 1");

Select in the table the ids in descending order and check if the board is the same and only the first result is taken (if there are any boards).

Note: Be careful not to allow your system to accept the same boards.

    
13.07.2014 / 07:12
0
<?php
include ("config.php");
if (isset($_POST['enviar'])) {

$id = (int) isset($_POST['id']);
$protocolo = isset($_query['id']);
$query = mysql_query("SELECT * FROM entrada WHERE id = '$id'");

while ($dados = mysql_fetch_assoc($query)){

echo $dados['placa'];
echo $dados['nome'];
}
}
?>

<form action="" method="post" name="id">
<input name="id" type="text" />
<input name="enviar" type="submit" />
</form>

The code fetch output, but I can not get the id according to what I put; s

    
15.07.2014 / 04:28