简体   繁体   English

如何使用PHP和Mysql一次只显示一条记录?

[英]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.我正在尝试为用户创建一个选项来编辑我使用 PHP 和 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:这里的常见步骤是提供将被编辑的行 ID:

<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:在您的编辑脚本中,您使用$_GET['id']读取id参数并将其设置在您的查询中:

$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 内置了格式化/模板,您可以使用它来改进代码的格式:

<?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:提交表单后,位于salvar_edicao.php的代码应如下所示:

<?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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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