有没有一种方法可以使用PHP表单在数据库中的多个记录之间循环?

时间:2019-03-29 20:51:24

标签: php html

我正在尝试创建一个php页面,在其中可以逐行循环浏览数据库表。我创建了一个页面,可以执行此操作,但是似乎无法合并表单,因此单击按钮转到下一个/上一个表单也将提交数据输入表单。

我已经编写了一个非常简化的代码版本,没有附加数据库来向您显示我在做什么:

<?php
session_start();

// Create an array to cycle through
$a = array('1', '2', '3', '4');

if(isset($_POST['village']))
{
    $_SESSION['counter']++;
}
elseif(isset($_POST['village1']))
{
    $_SESSION['counter'] = $_SESSION['counter']-1;
}
elseif(isset($_POST['testVar']))
{
    $_SESSION['testVar'] = $_POST['testVar'];
}
else
{
    $_SESSION['counter'] = 0;
}
?>

<html>
<head>
    <meta name="viewport" content="width=device-width, initial-scale=1" http-equiv="Content Type" charset="utf-8"/>
    <title>TEST</title>
</head>
    <body>
        <!-- Previous value form -->
        <form name="prevValue_form" method="post" action="" enctype="multipart/form-data">
            <div style="float: left">
                <button class="btn btn-success" name="prev" id="prev" value="prev" style="font-size: 20px;"><</a>
            </div>
            <input type="text" name="village1" value="testing" />
        </form>

        <!-- Next value form -->
        <form name="nextValue_form" method="post" action="" enctype="multipart/form-data">          
            <div style="float: left">
                <button class="btn btn-primary" name="next" id="next" value="next" style="font-size: 20px;">></button>
            </div>
            <input type="text" name="village" value="testing" />
        </form>

        <!-- data input section -->
        <div id="main" accept-charset="UTF-8" class="form">
            <form name="dataEntry_form" method="post" action="" enctype="multipart/form-data">
                <!-- data input variable -->
                tester: <input name="testVar" value="" />

                <!-- Values to display if code is working -->
                <br clear="all" /><br />
                count: <?php echo $a[$_SESSION['counter']]; ?>
                <br clear="all" />
                test variable: <?php echo $_SESSION['testVar']; ?>

                <!-- submit the data entry form -->
                <p><input name="submit" type="submit" value="Submit" /></p>
            </form>
        </div>


    </body>

</html>

<script>

    $("#nextValue_form button").click(function (ev) {
        ev.preventDefault()
        if ($(this).attr("value") == "next") {
            $("#test_form").submit();
            $("#test_form2").submit();
        }
    });

    $("#prevValue_form button").click(function (ev) {
        ev.preventDefault()
        if ($(this).attr("value") == "prev") {
            $("#test_form").submit();
            $("#test_form1").submit();
        }
    });

</script>

就像我在上面说的那样,该页面有效。但是,我将一些人从一堆Access数据库移到了SQL Server,他们不喜欢当他们单击按钮转到下一条记录时输入不会自动提交。我以为这是有可能实现的,但是我比PHP的人更多的是数据库人,而我一直在遇到麻烦。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

使用此post作为指南,仅用两个按钮执行一种表单并仅获取所需行而不是将行数组存储到会话中可能是值得的。我可能只将当前ID存储到会话中,但仅此而已。

首先,我将创建一些基本功能来减少重复:

<?php
# General PDO query function. Needs to have the connection injected
function query($con, $sql, $bind = null)
{
    $query = $con->prepare($sql);
    $query->execute($bind);
    return $query->fetch(\PDO::FETCH_ASSOC);
}
# Create a next function that uses the current id to find the next record
function toNextRecord($id, $con)
{
    return query($con, 'select * from tablename where id = (select min(id) from components where id > ?)',[$id]);
}
# Use the current id to find the previous record
function toPrevRecord($id, $con)
{
    return query($con, 'select * from tablename where id = (select max(id) from components where id < ?)',[$id]);
}
# Check for a post
if(!empty($_POST['action']) && $_POST['action'] == 'walk_table') {
    # Fetch previous or next, depending on button press
    $row    =   (strtolower($_POST['button']) == 'next')?  toNextRecord($_POST['id'], $con) : toPrevRecord($_POST['id'], $con);
}
else
    # Set the default to whatever record id to start on
    $row    =   ['id' => 1];
?>

为一个表单添加一个动作(只是为了更容易区分动作)和当前ID。在其中添加两个按钮。单击时,只有一个单击将注册在帖子中。

<form method="post" action="#">
    <input type="hidden" name="action" value="walk_table" />
    <input type="hidden" name="id" value="<?php echo $row['id'] ?>" />
    <input type="submit" name="button" value="Prev" />
    <input type="submit" name="button" value="Next" />
</form>