从MySQL到每个条目的单独页面的PHP数据

时间:2015-02-19 18:59:43

标签: php mysql

我将数据库中的信息提取到.php页面。我希望将信息作为单独的条目(所有问题/答案一起,然后是下一组答案)而不是表格中。我的表格很长,所以把一切都放在一张桌子里,就像我拥有它一样,会让它变得非常长。我该如何改变?我知道我已将它设置在桌面上,但我不确定如何将其更改为单独的项目。

更棒的是(虽然我怀疑非常困难)是让用户点击显示ID和date_visit的行,这将把他们带到一个单独的页面,其中包含已完成的表单结果。所以要将每个表单视为一个单独的页面,差不多。我希望这可能非常复杂?

使用数据库非常新。这是我的代码:

<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "";

 // Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "SELECT * FROM survey";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
echo "<table><tr><th>ID</th><th>Store Name</th><th>Receipt #</th><th>Date of Store Visit</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
    echo "<tr><td>".$row["ID"]."</td><td>".$row["storename"]."</td><td>".$row["receipt"]."</td><td>".$row["date_visit"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?> 

1 个答案:

答案 0 :(得分:0)

好的我不确定你所做的一切似乎只包含4个数据,所以为什么你想要点击查看其余的2列是超出我的。

那里说的就是你本来要做的事情(注意:我没有测试任何这些,所以可能有一些语法错误 - 它不是剪切/粘贴代码,但它应该说明你的基本知识要求做)...

所以list.php基本上与你现有的相同,只是我会添加链接,就像我在评论中提到的那样:

<?php
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

// only selecting the fields you need is generally better
// its also a good practice to get into aliasing your tablenames
// and always using the table_or_alias.column_name to reference them
$sql = "SELECT s.ID, s.date_vist FROM survey s";

$result = $conn->query($sql);

if ($result === false) {
   die(sprintf(
      'An error occurred attempting to access the data: "%s"',
      $conn->error
   ));
}
?>

<?php if ($result->num_rows > 0): ?>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Date of Store Visit</th>
                <th>&nbsp;</th>
            </tr>
        </thead>
        <tbody>
            <?php while (false !== ($row = $result->fetch_assoc())): ?>
                <tr>
                    <td><?php echo $row['ID'] ?></td>
                    <td><?php echo $row['date_visit'] ?></td>
                    <td>
                       <?php pritntf(
                           '<a href="view.php?id=%s">View Details</a>', 
                           echo $row['ID']
                       ); ?>
                    </td>
                </tr>
            <?php endwhile; ?>
        </tbody>
    </table>
<?php else: ?>
   <p>0 Results</p>
<?php endif; ?>

<?php $conn->close(); ?>

现在为您view.php,您将从网址中获取参数id并使用它从数据库中选择完整信息。

<?php

    $id = isset($_GET['id']) ? (integer) $_GET['id'] : null;
    if (null === $id) {
        header("HTTP/1.0 404 Not Found", true, 404);
        exit;
    }

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// we will us a prepared statment to avoid SQL injection
// when use a ? to mark a placeholder for a value in the query
$sql = 'SELECT s.* FROM survey s WHERE s.ID = ? LIMIT 1';
$stmt = $conn->prepare($sql);

if (false === $stmt) {
    die(sprintf(
        'Error attempting to access data: "%s"',
        $conn->error
    ));
}

// bind the ID to the prepared statement
$stmt->bind_param('i', $id);

if (!$stmt->execute() || false === ($result = $stmt->get_result())) {
    die(sprintf(
        'Error attempting to access data: "%s"',
        $stmt->error
    ));
} elseif ($result->num_rows < 1) {
    // no results so 404
    header("HTTP/1.0 404 Not Found", true, 404);
    exit;

} else {
    $survey = $result->fetch_assoc();
    $labels = array(
        'ID' => 'ID',
        'storename' => 'Store Name',
        'receipt' => 'Receipt #',
        'date_visit' => 'Date of Store Visit'
    );
}

$stmt->close();
$result->close();
?>


<table>
    <tbody>
         <?php foreach($survey as $column => $value): ?>
             <tr>
                 <th><?php echo isset($labels[$column]) 
                         ? $labels[$column] 
                         : ucwords($column); ?>
                 </th>
                 <td><?php echo $value; ?>
             </tr>
         <?php endforeach; ?>
     </tbody>
</table>