How to search for ID in Mysql

Asked

Viewed 1,951 times

1

Staff I am developing a system of input and output of vehicles of the company, in the index I made 2 inputs one of entrada and another saída, when I click on input appears the registration screen of the vehicle as placa, nome do motorista, data da entrada e hora da entrada, when you click insert I put to generate a protocol that pulls the id.

With the exit of the vehicle I was without idea then I decided to do the following, When the vehicle is leaving the company the person clicking there exit to trim a button look, then I put the protocol and appears the license plate of the driver and the name of the same, because I am not able to make the system to search, help me with the system to search, follows below the code of the entry:

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>'); } ?>

HTML CODE OF ENTRY

<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" />
  • Just a note: we are in 2014, PHP6 is already in development and mysql has already been deprecated (will be removed), this means that an update in your server’s PHP in the future will stop your code from working, which is not very cool, since this is being produced for a company. So be careful to keep the company server using PHP5 or the project will stop working, or alternatively you can use the new functions that replace mysql: the class mysqli and the class PDO. It will still take a few years for PHP6 to launch and be common on servers, but... ;)

  • Thanks for the tip, I will update myself , rsrs.

3 answers

3

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

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>');
  • 1

    Also no longer use mysql library_, is insecure and inconsistent. Use the mysqli library_ or PDO.

  • @Marcelo Gomes, worked vlw.

  • @gcarvalho97 beauty, I’ll start updating myself ;D

  • @Paulopimentel if Marcelo’s suggestion worked, accept this answer as the solution!

  • @Onosendai, Helped in another problem, but the question problem remains unanswered, rsrs

  • Paulo, you have two fields with "name" equal, that is, name=id... only the field should have that name=id... remove the form, and leave only the field... <form action="" method="post" --> name="id" <--- > <input name="id" type="text" /> , it seems to me that your code down there is right.. only will not work if the main field of the table is not ID ... it can be "code" or any other name. Check your table to see if the field is really called "id". Remember that minute and upper case make a difference in the names of mysql fields.

  • Ahh, I just saw... you’re making a wrong assignment... isset() only returns true or false.. and not the contents of the variable... you need to take isset out of assignment... put it like this: $id = (int)$_POST['id'];

  • @Paulopimentel look at the comments I made above.

  • @Marcelogomes Friend, I solve the problem, I’m not at home now, when I get home I will show you the solution, thanks to you already ooo, helped me a lot!

Show 4 more comments

2

How do you not define the id in the code it must be auto-incremented in the database, so what you’re looking for is probably the highest value:

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

Note that it is never recommended to use the SELECT * see that link for more details.

And this code is not safe because I’m only getting the most value, if you want your code to be safer I suggest you use this:

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

Selects in the table the ids in descending order and checks if the card is the same informed and only takes the first result (for if there are equal plates).

Note: take care not to allow your system to accept equal boards.

  • Friend is the following the plates can be repeated, because the same vehicle can enter the next day or even the same day, so I created the protocol that pulls the id, what I need to do is to search for protocol(id), ie any protocol(id) that I enter there it appear the results of that id type, name and plate or whatever I determine.

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 output search code, however I cannot find the id according to what I put s;

Browser other questions tagged

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