简体   繁体   中英

MySQL - Select row values from url ID

I created a table which is updated through a form and each row gets assigned a specific number.

When viewing this table, I want to click on that assigned number and get a page where all the details of that row are displayed.

If I do $sql = "SELECT * FROM clients WHERE nif_id='114522';"; - where the nif_id is the assigned number - I get the values for that number, but I need it to change with every number in the table.

Any ideas?

UPDATE

This is the table code:

<div class="card card-body">
        <table class="table">
          <thead>
              <tr>
                  <th>NIF</th>
                  <th>Nome</th>
                  <th>Apelido</th>
                  <th>Telemóvel</th>
                  <th>E-mail</th>
              </tr>
          </thead>
          <tbody>
        <?php
          include_once '../includes/db.inc.php';
          $sql = "SELECT * FROM clients ORDER BY nif_id ASC;";
          $result = mysqli_query($conn, $sql);
          $resultCheck = mysqli_num_rows($result);

          if ($resultCheck > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
              $first = $row["prm_nome"];
              $last = $row["apelido"];
              $phone = $row['nmr_tlm'];
              $email = $row['mail'];
              $nif = $row['nif_id'];

              echo '<tr>';
                      echo '<td>'.$nif.'</td>';
                      echo '<td>'.$first.'</td>';
                      echo '<td>'.$last.'</td>';
                      echo '<td>'.$phone.'</td>';
                      echo '<td>'.$email.'</td>';
                      echo '</tr>';
            }
          }
         ?>
       </tbody>
       </table>
     </div>

You can use the get request parameters.

ex: www.myapp.com/table?id=3920393

add functionality in your PHP file as follows

if(isset($_GET["id"])){ 
   $id = $_GET["id"];
   $sql = "SELECT * FROM clients WHERE nif_id='".$id."';";

  //make db call & display HTML
}

This is a very simple implementation and does not implement any security or SQL injection security. This was more of a conceptual answer as to how you can tackle your problem.

This is quite a common scenario for web-based systems.

<div class="card card-body">
    <table class="table">
      <thead>
          <tr>
              <th>NIF</th>
              <th>Nome</th>
              <th>Apelido</th>
              <th>Telemóvel</th>
              <th>E-mail</th>
          </tr>
      </thead>
      <tbody>
    <?php
      include_once '../includes/db.inc.php';
      $sql = "SELECT * FROM clients ORDER BY nif_id ASC;";
      $result = mysqli_query($conn, $sql);
      $resultCheck = mysqli_num_rows($result);

      if ($resultCheck > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
          $first = $row["prm_nome"];
          $last = $row["apelido"];
          $phone = $row['nmr_tlm'];
          $email = $row['mail'];
          $nif = $row['nif_id'];

          echo '<tr>';
                  echo '<td><a href="detail.php?nifid=' . $nif . '">'.$nif.'</a></td>';
                  echo '<td>'.$first.'</td>';
                  echo '<td>'.$last.'</td>';
                  echo '<td>'.$phone.'</td>';
                  echo '<td>'.$email.'</td>';
                  echo '</tr>';
        }
      }
     ?>
   </tbody>
   </table>
 </div>

where the detail.php is another page to query specific details regarding the query nifid .

As a reminder, if the data type of the column is INT, there is no need to use single quotes to surround the value in the SQL statement.

Sample detail.php :

<?php
if(!isset($_GET['nifid']) || (int)$_GET['nifid'] <= 0) {
    // Invalid or missing NIFID
    header('Location: table.php');
}
include_once '../includes/db.inc.php';
$id = (int)$_GET['nifid'];
$sql = "SELECT * FROM clients WHERE nif_id=$id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);

// TODO: display the result in whatever way you like
?>

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