从html表单错误中读取输入

时间:2015-02-09 16:32:12

标签: php html mysql database crud

所以我试图从TABLE页面读取记录,但它总是导致我出错404,我的id名称是headid。我试图学习粗鲁。我想查看非表格形式的记录,但是看起来像输入表单的可打印表单。

这是我的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>

这是我的文件与我的数据库的链接,因此您可以尝试:https://onedrive.live.com/?cid=ED1942ABAA45D6A5&id=ED1942ABAA45D6A5%21105

1 个答案:

答案 0 :(得分:0)

导致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

请注意此重定向,您必须避免无限重定向到同一页面。