每个条目将MySQL中的PHP数据分成单独的页面

每个条目将MySQL中的PHP数据分成单独的页面,php,mysql,Php,Mysql,我正在将数据库中的信息拉入一个.php页面。我希望信息作为单独的条目(所有问题/答案放在一起,然后是下一组答案)而不是放在表格中。我的状态很长,所以像我一样把所有的东西都放在一张桌子上会让我的状态非常长。我如何改变这个?我知道我已经在桌子上设置了它,但我不知道如何将它更改为单独的项目 更棒的是(尽管我怀疑非常困难)让用户点击一行,该行只显示ID和访问日期,这将把他们带到一个单独的页面,并显示完成的表单结果。因此,几乎可以将每个表单看作一个单独的页面。我想这可能会非常复杂 我对使用数据库很陌生。这

我正在将数据库中的信息拉入一个.php页面。我希望信息作为单独的条目(所有问题/答案放在一起,然后是下一组答案)而不是放在表格中。我的状态很长,所以像我一样把所有的东西都放在一张桌子上会让我的状态非常长。我如何改变这个?我知道我已经在桌子上设置了它,但我不知道如何将它更改为单独的项目

更棒的是(尽管我怀疑非常困难)让用户点击一行,该行只显示ID和访问日期,这将把他们带到一个单独的页面,并显示完成的表单结果。因此,几乎可以将每个表单看作一个单独的页面。我想这可能会非常复杂

我对使用数据库很陌生。这是我的密码:

<?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();
?> 

好的,我一点也不确定你在做什么,因为你的数据集似乎只包含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(); ?>

您只需创建类似
echosprintf(',$row['ID'])的链接
然后在you view.php中,您将获得该
survey.ID
的所有问题/答案,并根据需要显示它们。无法就显示部分提供任何建议,因为您没有为该部分包含任何SQL。谢谢。您需要查看SQL的哪一部分?您运行什么SQL来显示表中的所有问题和答案?我在上面粘贴的代码…但这只是输出一些摘要数据,它没有给我任何关于表中其他字段的线索,或者您是否应该从不同的表或多个其他表中提取(通常类似的内容会在几个表中设置,如
调查
调查回答
调查问题
)。谢谢。我有4个以上的字段,我现在只使用4来测试所有内容。我会给你的代码一个测试。
<?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>