简体   繁体   中英

How to show only one record at a time using PHP and Mysql?

I'm trying to create an option for the user to edit the information in a table I have created using PHP and MySQL. I managed to create this function and the data is being modified, the only problem is when I click the "edit" button on one record of the table, the edit screen always shows all the records from the table, instead of showing only the data I want to modify, how can I solve this?

Here is my code:

<?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>
  ";    
}
?>

on your edit button, you should also set what information from all the informations they have that they want to edit. The common step here is to supply row id that will be edited:

<a href="edit.php?id=<?php echo $fieldId; ?>">edit</edit>

And on your edit script, you read the id parameter using $_GET['id'] and set it in your query:

$sql = "SELECT `ID`, `carro`, `datasaida` , `horasaida` , `datachegada`, `horachegada`, `kminicial`, `kmfinal`, `destino`, `motorista` FROM `carro` WHERE `ID` = '$id'";

This will make sure that you only get one record instead of the whole tables.

PHP has built in formatting/templating which you could use to improve the format of your code:

<?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"];

    // we can make use of PHP's built formatting/templating functionality here
    ?>
    <form id="form1" name="form1" method="post" action="salvar_edicao.php">
        ID: <input name="id" type="text" readonly="readonly" id="id" value="<?php echo $id; ?>" size="35"/><br>
        CARRO: <input name="carro" type="text" id="carro" value="<?php echo $carro; ?>" size="35"/><br>
        DATA DE SAIDA: <input name="datasaida" type="text" id="datasaida" value="<?php echo $datasaida; ?>" size="35"/><br>
        HORA DE SAIDA: <input name="horasaida" type="text" id="horasaida" value="<?php echo $horasaida; ?>" size="35"/><br>
        DATA DE CHEGADA: <input name="datachegada" type="text" id="datachegada" value="<?php echo $datachegada; ?>" size="35"/><br>
        HORA DE CHEGADA: <input name="horachegada" type="text" id="horachegada" value="<?php echo $horachegada; ?>" size="30"/><br>
        KM INICIAL: <input name="kminicial" type="text" id="kminicial" value="<?php echo $kminicial; ?>" size="35"/><br>
        KM FINAL: <input name="kmfinal" type="text" id="kmfinal" value="<?php echo $kmfinal; ?>" size="35"/><br>
        DESTINO: <input name="destino" type="text" id="destino" value="<?php echo $destino; ?>" size="35"/><br>
        MOTORISTA: <input name="motorista" type="text" id="motorista" value="<?php echo $motorista; ?>" size="35"/><br>

        <input type="submit" onclick="return confirm('Deseja mesmo editar esse registro?');" name="Submit" value="SALVAR ALTERAÇÕES" class="btnNew"/>
    </form>
    <?php
}

After submitting the form, your code located in salvar_edicao.php should look something like this:

<?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");

// check that the data has been posted
if(!isset($_POST["id"])){
    // No `id` found in post data
    die;
}

$sql   = "SELECT `ID`, `carro`, `datasaida` , `horasaida` , `datachegada`, `horachegada`, `kminicial`, `kmfinal`, `destino`, `motorista` FROM `carro` WHERE `ID` = ?";
$statement = $con->prepare($sql);
$statement->bind_param('i', $_POST['id']);
$statment->execute();

$query = $statement->get_result();

$dados = $query->fetch_assoc();
if(is_null($dados)){
    // No rows exist for this ID
    die;
}

// $dados = row in database
// $_POST = data submitted from form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM