Edit only one table row

Asked

Viewed 91 times

-1

I am trying to create an option to edit the information of a table I created using PHP and Mysql, I even managed to create the option and the data can be modified, the only problem is that if I click the button "edit" in a table row, the editing screen always displays all the lines, ie, shows all the data instead of showing only what I want to edit, how do I resolve this?

this is the code I created:

 <?php
ini_set('default_charset','UTF-8');

$host="localhost"; 
$username="********";  
$password="********"; 
$db_name="*********"; 

$con=mysqli_connect("$host", "$username", "$password", "$db_name")or die("cannot connect"); 

mysqli_set_charset($con,"utf8");

$sql = "SELECT `ID`, `carro`, `datasaida` , `horasaida` , `datachegada`, `horachegada`, `kminicial`, `kmfinal`, `destino`, `motorista` FROM `carro`";
   $query = $con->query($sql);
while ($dados = $query->fetch_assoc()) {
    $id        = $dados["ID"];
    $carro      = $dados["carro"];
    $datasaida = $dados["datasaida"];
    $horasaida = $dados["horasaida"];
    $datachegada = $dados["datachegada"];
    $horachegada = $dados["horachegada"];
    $kminicial = $dados["kminicial"];
    $kmfinal = $dados["kmfinal"];
    $destino = $dados["destino"];
    $motorista = $dados["motorista"];

    echo "
    <form id=\"form1\" name=\"form1\" method=\"post\" action=\"salvar_edicao.php\">
    ID: <input name=\"id\" type=\"text\" readonly=\"readonly\" id=\"id\" value=\"$id\" size=\"35\"/><br>
    CARRO: <input name=\"carro\" type=\"text\" id=\"id\" value=\"$carro\" size=\"35\"/><br>
    DATA DE SAIDA: <input name=\"datasaida\" type=\"text\" id=\"id\" value=\"$datasaida\" size=\"35\"/><br>
    HORA DE SAIDA: <input name=\"horasaida\" type=\"text\"  id=\"id\" value=\"$horasaida\" size=\"35\"/><br>
    DATA DE CHEGADA: <input name=\"datachegada\" type=\"text\"  id=\"id\" value=\"$datachegada\" size=\"35\"/><br>
    HORA DE CHEGADA: <input name=\"horachegada\" type=\"text\"  id=\"id\" value=\"$horachegada\" size=\"30\"/><br>
    KM INICIAL: <input name=\"kminicial\" type=\"text\"  id=\"id\" value=\"$kminicial\" size=\"35\"/><br>
    KM FINAL: <input name=\"kmfinal\" type=\"text\"  id=\"id\" value=\"$kmfinal\" size=\"35\"/><br>
    DESTINO: <input name=\"destino\" type=\"text\"  id=\"id\" value=\"$destino\" size=\"35\"/><br>
    MOTORISTA: <input name=\"motorista\" type=\"text\"  id=\"id\" value=\"$motorista\" size=\"35\"/><br>
    <input type=\"submit\" onclick=\"return confirm('Deseja mesmo editar esse registro?');\" name=\"Submit\" value=\"SALVAR ALTERAÇÕES\" class=\"btnNew\"/>
    </form>
    ";  
}
?>
  • You need a condition WHERE to be able to recover the data and the same to update the data.

3 answers

1

I found a solution but using only isolated Mysql, you can try to adapt pro PHP.

In pure MYSQL we use:

update (name_table) set Name = José Augusto Where id = '2';

the above code ta in pure mysql, there says: update the table by changing the previous name to ''José Augusto', where the Id is equal to '2'.

0

<?php

//LIMIT 1, Limita em 1 resultado apenas

$sql = "SELECT `ID`, `carro`, `datasaida` , `horasaida` , `datachegada`, `horachegada`, `kminicial`, `kmfinal`, `destino`, `motorista` FROM `carro` LIMIT 1";

//Você pode acrescentar ORDER BY ao final para ordenar usando uma coluna

$sql = "SELECT `ID`, `carro`, `datasaida` , `horasaida` , `datachegada`, `horachegada`, `kminicial`, `kmfinal`, `destino`, `motorista` FROM `carro` LIMIT 1 ORDER BY ID ASC";

//ASC = Crescente
//Desc = Decrescente


?>

Also to filter should be used the WHERE:

<?php

//Exemplo
$sql = "SELECT id, cor, marca, ano FROM carros WHERE cor='vermelho' ORDER BY ano ASC";

/*
Pode me retornar:

4 - vermelho - Nissan - 2012
5 - vermelho - Honda- 2017
3 - vermelho - Fiat - 2019
9 - vermelho - Volkswagen - 2020
*/

?>

0

To select only one record

You need to specify a condition using the WHERE clause, this condition in your case may be the ID field. Remembering that I’m not worried about code security:

$id = $_GET['id'];  
$sql = "SELECT `ID`, `carro`, `datasaida` , `horasaida` , `datachegada`, `horachegada`, `kminicial`, `kmfinal`, `destino`, `motorista` FROM `carro` WHERE ID = $id";
$query = $con->query($sql);
$dados = $query->fetch_assoc();

For update:

You not only pass the values for each field you will update, you also need to use a condition, in this part you do not need to use $dados = $query->fetch_assoc();, because you will only update the database. If you want to return the data, you need to repeat the above step.

$id = $_POST['id'];
$sql = "UPDATE carro SET column1 = value1, column2 = value2, ... WHERE ID = $id";
$query = $con->query($sql);

Browser other questions tagged

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