从 html 表单错误中读取输入

Reading inputs from a html form error

所以我试图从 TABLE 页面读取记录,但它总是导致我出现错误 404,我的 ID 名称是 headid。我正在努力学习垃圾。我想查看的记录不是table形式,而是像输入形式一样的printable形式。

这是我的 read.php 文件:

<?php

require 'crud_database.php';
$headid = null;
if ( !empty($_GET['headid'])) {
    $headid = $_REQUEST['headid'];
}

if ( null==$headid ) {
    header("Location: read.php");
} else {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * FROM table_head where headid = ?";
    $q = $pdo->prepare($sql);
    $q->execute(array($headid));
    $data = $q->fetch(PDO::FETCH_ASSOC);
    Database::disconnect();
}
?>


<?php echo"$headid"?>
        <!--Name-->
        <div class="form-group">
        <label class="col-sm-2 control-label">Name</label>
            <div class="col-sm-3">
                 <?php echo $data['fname'];?>
            </div>
            <div class="col-sm-3">
                <?php echo $data['mname'];?>
            </div>
            <div class="col-sm-3">
                 <?php echo $data['lname'];?>
            </div>
        </div>
        <!--Name ends-->

        <!-- B1 Relation to HH-->
        <div class="form-group">
            <label for="relation" class="col-sm-2 control-label">Relation to Household Head</label>
            <div class="col-sm-6">

                     <?php echo $data['relation'];?>

            </div>
        </div>

        <!-- B2 Age-->

        <!-- B3 Sex/Gender-->
        <div class="form-group">
            <label for="gender" class="col-sm-2 control-label">Gender</label>
            <div class="col-sm-6">
                <?php echo $data['gender'];?>
            </div>
        </div>

        <!--Birthdate-->
        <div class="form-group">
            <label class="col-sm-2 control-label">Birthdate</label>
            <div class="col-sm-3">
                 <?php echo $data['bdate'];?>
            </div>
        </div>

        <!--Birthplace-->
        <div class="form-group">
            <label class="col-sm-2 control-label">Birthplace</label>
            <div class="col-sm-6">
                <?php echo $data['bplace'];?>
            </div>
        </div>

        <!--B4 Marital Status-->
        <div class="form-group">
            <label for="maritalStatus" class="col-sm-2 control-label">Marital Status</label>
            <div class="col-sm-6">
                <?php echo $data['maritalstatus'];?>
            </div>
        </div>

        <!--B5 Educational Attainment-->
        <div class="form-group">
            <label class="col-sm-2 control-label">Education</label>
            <div class="col-sm-3">
                 <?php echo $data['Education'];?>
            </div>
        </div>


        <!--B6 Occupation-->
        <div class="form-group">
            <label for="occupation" class="col-sm-2 control-label">Cccupation</label>
            <div class="col-sm-6">
                 <?php echo $data['occupation'];?>
            </div>
        </div>

        <!--B7 Status of Occupation-->
        <div class="form-group">
            <label for="status_occupation" class="col-sm-2 control-label">Status of Cccupation</label>
            <div class="col-sm-6">
            <?php echo $data['status_occupation'];?>

            </div>
        </div>

        <!--B7 Place of Work-->
        <div class="form-group">
            <label for="placeofwork" class="col-sm-2 control-label">Place</label>
            <div class="col-sm-6">
                 <?php echo $data['placeofwork'];?>

            </div>
        </div>

        <!--B10 Skills-->
        <div class="form-group">
            <label for="skills" class="col-sm-2 control-label">skills</label>
            <div class="col-sm-6">
                 <?php echo $data['skills'];?>

            </div>
        </div>

        <!--B11 Membership-->
        <div class="form-group">
            <label for="membership" class="col-sm-2 control-label">Membership</label>
            <div class="col-sm-6">
                 <?php echo $data['membership'];?>

            </div>
        </div>

        <!--B12 Beneficiary-->
        <div class="form-group">
            <label for="beneficiary" class="col-sm-2 control-label">Beneficiary</label>
            <div class="col-sm-6">
                 <?php echo $data['beneficiary'];?>

            </div>
        </div>

        <!--B13 Registered Voter in Barangay?-->
        <div class="form-group">
            <label for="alterations" class="col-sm-2 control-label">Registered Voter in Barangay?</label>
            <div class="col-sm-6">
                 <?php echo $data['voter'];?>
            </div>
        </div>

这是我的 table.php 文件,我在其中读取记录:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link   href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>

<body>
<div class="container">
        <div class="row">
            <h3>PHP CRUD Grid</h3>
        </div>
        <div class="row">
            <p>
                <a href="create.php" class="btn btn-success">Create</a>
            </p>

            <table class="table table-striped table-bordered">
                  <thead>
                    <tr>

                      <th>First Name</th>
                      <th>Middle Name</th>
                      <th>Last Name</th>
                      <th>Relation</th>
                      <th>Gender</th>
                      <th>Age</th>
                      <th>Birthplace</th>
                      <th>Marital Status</th>
                      <th>Occupation</th>
                      <th>Occupation</th>
                      <th>Occupation</th>
                      <th>Skills</th>
                      <th>Membership</th>
                      <th>Ben</th>
                      <th>Voter</th>

                        </tr>
                      </thead>
                      <tbody>
                      <?php
                       include 'crud_database.php';
                       $pdo = Database::connect();
                       $sql = 'SELECT * FROM table_head ORDER BY headid DESC';
                       foreach ($pdo->query($sql) as $row) {
                                echo '<tr>';
                                echo '<td>'. $row['headid'] . '</td>';
                                echo '<td>'. $row['fname'] . '</td>';
                                echo '<td>'. $row['mname'] . '</td>';
                                echo '<td>'. $row['lname'] . '</td>';
                                echo '<td>'. $row['gender'] . '</td>';
                                echo '<td>'. $row['bdate'] . '</td>';
                                //echo '<td>'. $row['age'] . '</td>';
                                echo '<td>'. $row['bplace'] . '</td>';
                                echo '<td>'. $row['maritalstatus'] . '</td>';
                                echo '<td>'. $row['relation'] . '</td>';
                                echo '<td>'. $row['education'] . '</td>';
                                echo '<td>'. $row['status_occupation'] . '</td>';
                                echo '<td>'. $row['occupation'] . '</td>';
                                echo '<td>'. $row['placeofwork'] . '</td>';
                                echo '<td>'. $row['membership'] . '</td>';
                                echo '<td>'. $row['skills'] . '</td>';
                                echo '<td>'. $row['beneficiary'] . '</td>';
                                echo '<td>'. $row['voter'] . '</td>';

                                echo '<td width=250>';
                                echo '<a class="btn" href="crud_read.php?id='.$row['headid'].'">Read</a>';
                                echo ' ';
                                echo '<a class="btn btn-success" href="update.php?id='.$row['headid'].'">Update</a>';
                                echo ' ';
                                echo '<a class="btn btn-danger" href="delete.php?id='.$row['headid'].'">Delete</a>';
                                echo '</td>';
                                echo '</tr>';

                       }
                       Database::disconnect();
                      ?>
                      </tbody>
                </table>
        </div>
    </div> <!-- /container -->
  </body>
</html>

这是我的数据库文件的link,这样你就可以试试:https://onedrive.live.com/?cid=ED1942ABAA45D6A5&id=ED1942ABAA45D6A5%21105

有两个错误导致404错误:

echo '<a class="btn" href="crud_read.php?id='.$row['headid'].'">Read</a>';

参数 id 应该是 headid,所以它在你的 crud_read.php

中被正确检索
$headid = null;
if ( !empty($_GET['headid'])) {
    $headid = $_REQUEST['headid'];
}

if ( null==$headid ) {
    header("Location: read.php");
}

在你的例子中 $headidnull 所以这段代码重定向到不存在的页面 read.php

小心这个重定向,你必须避免无限重定向到同一页面。