从数据库读取并显示内容

时间:2019-06-07 07:28:27

标签: php mysql

示例代码显示位置。我还要搜索其他4列数据。我是新来的,我想尝试学习,请尝试尽可能多地解释,而不是给我答案,请指导我。

我尝试添加其他4个表单,然后在顶部尝试添加除WHERE location =:location周围的位置之外的其他区域;但是随后表单开始起作用,并且不接受其他区域。

还尝试添加:

$location = $_POST['location'];
$fullname = $_POST['fullname'];
$paypal = $_POST['paypal'];
$email = $_POST['email'];
$serial_no = $_POST['serial_no'];

哪个也不起作用。

<?php

/**
 * Function to query information based on 
 * a parameter: in this case, location.
 *
 */

require "../config.php";
require "../common.php";

if (isset($_POST['submit'])) {
  if (!hash_equals($_SESSION['csrf'], $_POST['csrf'])) die();

  try  {
    $connection = new PDO($dsn, $username, $password, $options);

    $sql = "SELECT * 
            FROM clients2
            WHERE location = :location";

    $location = $_POST['location'];
    $statement = $connection->prepare($sql);
    $statement->bindParam(':location', $location, PDO::PARAM_STR);
    $statement->execute();

    $result = $statement->fetchAll();
  } catch(PDOException $error) {
      echo $sql . "<br>" . $error->getMessage();
  }
}
?>
<?php require "templates/header.php"; ?>

<?php  
if (isset($_POST['submit'])) {
  if ($result && $statement->rowCount() > 0) { ?>
    <h2>Results</h2>

    <table>
      <thead>
        <tr>
          <th>#</th>
          <th>Fullname</th>
          <th>PayPal</th>
          <th>Email Address</th>
          <th>Serial</th>
          <th>Location</th>

        </tr>
      </thead>
      <tbody>
      <?php foreach ($result as $row) : ?>
        <tr>
          <td><?php echo escape($row["client_id"]); ?></td>
          <td><?php echo escape($row["fullname"]); ?></td>
          <td><?php echo escape($row["paypal"]); ?></td>
          <td><?php echo escape($row["email"]); ?></td>
          <td><?php echo escape($row["serial_no"]); ?></td>
          <td><?php echo escape($row["location"]); ?></td>

        </tr>
      <?php endforeach; ?>
      </tbody>
    </table>
    <?php } else { ?>
      <blockquote>No results found for <?php echo escape($_POST['location']); ?>.</blockquote>
    <?php } 
} ?> 

<h2>Find user based on location</h2>

<form method="post">
  <input name="csrf" type="hidden" value="<?php echo escape($_SESSION['csrf']); ?>">
  <label for="location">Location</label>
  <input type="text" id="location" name="location">
  <input type="submit" name="submit" value="View Results">
</form>

<a href="index.php">Back to home</a>

<?php require "templates/footer.php"; ?>

要使4个额外字段显示任何结果。此时,位置字段将显示结果。

0 个答案:

没有答案