简体   繁体   English

如何使客户端上的按钮重定向到带有数据库信息的php页面

[英]How to make buttons on client side that redirect to a php page with database information

I am brand new to programming and I had a unique (I think) question. 我是编程的新手,我有一个独特的(我认为)问题。

I made a MySql database for storing employee records. 我制作了一个MySql数据库来存储员工记录。 I am able to submit new employee data to the db and am able to retrieve employee data from the db and display it in an HTML table. 我能够向数据库提交新的员工数据,并且能够从数据库检索员工数据并将其显示在HTML表中。

I want to create buttons on the table for each ID#. 我想在表格上为每个ID#创建按钮。 Each button would redirect you to a different php page and display the employee information associated with the ID# whose button was clicked. 每个按钮会将您重定向到不同的php页面,并显示与单击该按钮的ID#相关的员工信息。

Does anyone know how would I go about doing this? 有谁知道我该怎么做?

echo '<table border="0" cellspacing="25" cellpadding="2">
      <tr>
          <td>ID</td>
          <td>First</td>
          <td>Last</td>
          <td>SIN</td>
          <td>Password</td>
      </tr>';

function loadList() {
include_once 'includes/dbh.inc.php';

try {
  $sqlarray = "SELECT * FROM employee;";
  $result = mysqli_query($conn, $sqlarray);
  $resultCheck = mysqli_num_rows($result);

  if ($resultCheck > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
      $employeeID = $row['id'] . "<br>";
      $employeeFN = $row['firstname'] . "<br>";
      $employeeLN = $row['lastname'] . "<br>";
      $employeeSIN = $row['sin_'] . "<br>";
      $employeePASS = $row['pass_'] . "<br>";

      echo '<tr>
                <td>'.$employeeID.'</td>
                <td>'.$employeeFN.'</td>
                <td>'.$employeeLN.'</td>
                <td>'.$employeeSIN.'</td>
                <td>'.$employeePASS.'</td>
            </tr>';
    }
  }
  else {
    echo "error no data";
  }

} catch (\Exception $e) {
  echo ("error");
}
}

Mate, welcome in the programming world. 伴侣,欢迎来到编程世界。 You can create a anchor tag which will redirect user to a new php page with employee id in URL. 您可以创建一个定位标记,该标记会将用户重定向到URL中带有员工ID的新php页面。 check below code: 检查以下代码:

echo '<tr>
          <td>'.$employeeID.'</td>
          <td>'.$employeeFN.'</td>
          <td>'.$employeeLN.'</td>
          <td>'.$employeeSIN.'</td>
          <td>'.$employeePASS.'</td>
          <td><a href="details.php?id='$employeeID.'>View Details</a></td>              
       </tr>'  

Here you need to create a new php page with details.php name. 在这里,您需要使用details.php名称创建一个新的php页面。 There you need to get employee id by GET global variable and need to fetch details from database to show. 在那里,您需要通过GET全局变量获取员工ID,并且需要从数据库中获取详细信息以进行显示。 Check below code: 检查以下代码:

<?php
include_once 'includes/dbh.inc.php';
$employeeId = $_GET['id'];

$sqlarray = "SELECT * FROM employee where id = '".$employeeId."';";
$result = mysqli_query($conn, $sqlarray);
if (!$result) {
    echo 'Could not run query: ' . mysql_error();
    exit;
}
$employeeDetails = mysql_fetch_row($result);

Here $employeeDetails has all data which you can use to render in HTML. 这里$ employeeDetails具有可用于以HTML呈现的所有数据。 But please read little bit about SQL injection before using the same code to avoid crashes. 但是在使用相同代码以避免崩溃之前,请先阅读一些有关SQL注入的知识。 Hope it helps you. 希望对您有帮助。

You could do like this by adding <a></a> tag 您可以通过添加<a></a>标签来做到这一点

echo '<tr>
          <td><a href="show.php?id='$employeeID.'>Show</a></td>
          <td>'.$employeeID.'</td>
          <td>'.$employeeFN.'</td>
          <td>'.$employeeLN.'</td>
          <td>'.$employeeSIN.'</td>
          <td>'.$employeePASS.'</td>
       </tr>';

And in show.php page get this id by $_GET['id'] 然后在show.php页面中通过$_GET['id']获取此ID

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

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