更新记录的流程表

时间:2015-07-05 11:10:29

标签: php mysql sql html5 forms

我构建了一个表单,它是一个表示mysql数据库中多个记录的字段表。该表是基于某些关系表动态生成的:

enter image description here

我想创建一个“保存”按钮,如果只需单击一次就可以对给定记录的任何字段进行更改,就会更新该表。

我的问题是如何在不创建围绕每个表行的单独表单标记的情况下执行此操作?什么是最好的方法?是否可以只使用html和php,或者是否需要javascript?

我想避免使用编辑按钮插入其他列。

以下是生成表格的代码:

function subjectResourcesForm($connection, $subjectId) {
    echo "<h2>ONLINE RESOURCES</h2>";
    echo "<div id='subject_resources_" . $section_row[$ID] . "'>\n";
    $resource_query = selectResourcesBySubject($subjectId);
    $resource_result = mysqli_query($connection, $resource_query);
    if ( mysqli_num_rows($resource_result) != 0 ) {
    echo "<table>";
    echo "<tr>";
    echo "<td>Position</td>";
    echo "<td>Label</td>";
    echo "<td>Link</td>";
    echo "<td>Visibility</td>";
    echo "</tr>";
        while($resource_row = mysqli_fetch_assoc($resource_result)) {
        echo "<tr>";
            echo "<td><input type=\"text\" " . 
                            "name=\"resourceItemPosition_" . $resource_row['id'] . 
                            "\" value=\"" . $resource_row['position'] . "\" />";
            echo "</td>";
            echo "<td><input type=\"text\" " . 
                            "name=\"resourceItemTitle_" . $resource_row['id'] . 
                            "\" value=\"" . $resource_row['resourceTitle'] . "\" />";
            echo "<td><input type=\"text\" " . 
                            "name=\"resourceItemLink_" . $resource_row['id'] . 
                            "\" value=\"" . $resource_row['link'] . "\" />";

            echo "<td><select name=\"resourceItemDisplay_" . $resource_row['id'] . "\">";
            if ( $resource_row['display'] == $TRUE ) {
                echo "<option value=\"1\" selected>Visible</option>";
                echo "<option value=\"0\">Hidden</option>";
            } else {
                echo "<option value=\"1\">Visible</option>";
                echo "<option value=\"0\" selected>Hidden</option>";
            }
            echo "</select></td>";
        echo "</tr>";
        }
        echo "</ul>";
    } else {
        echo "No Resources Found!";
    }
    // Save Button
    echo "<tr>" . 
            "<td colspan=\"2\">" . 
                "<button type=\"button\">Save Changes</button>" .
            "</td>" . 
    "</tr>";
    // Insert Button
    echo "<tr>" . 
            "<td colspan=\"2\">" . 
                "<button type=\"button\">Add Item</button>" .
            "</td>" . 
    "</tr>";

    echo "</table>";
    echo "</div><!-- End of subject_resources_" . $section_row[$ID] . "-->\n";

    // Free Result
    mysqli_free_result($resource_result);
}

这是SQL:

function selectResourcesBySubject($subjectId) {
        return "SELECT
                tbl_Resources.mId               AS  id,
                tbl_Resources.mTitle            AS  resourceTitle,
                tbl_Resources.mLink             AS  link,
                tbl_SubjectResources.mSubject   AS  subject, 
                tbl_SubjectResources.mResource  AS  resource,
                tbl_SubjectResources.mDisplay   AS  display,
                tbl_SubjectResources.mPosition  AS  position
            FROM
                tbl_Resources
            INNER JOIN
                tbl_SubjectResources
            ON
                tbl_SubjectResources.mResource = tbl_Resources.mId
            WHERE
                tbl_SubjectResources.mSubject = " . $subjectId .
        "   ORDER BY
                tbl_SubjectResources.mPosition ASC";
}

1 个答案:

答案 0 :(得分:0)

我不知道如何在不使用JavaScript的情况下执行此操作,但这里有一种使用JavaScript的方法。

首先,添加一个隐藏字段,跟踪行是否已更新:

echo '<input type="hidden" name="resourceItemUpdated_' . $resource_row['id'] . '" value="0" />';

然后,使用onchange事件,当修改行上的任何其他字段时,将隐藏字段的值设置为1.

最后,在PHP中,循环遍历行,只有在隐藏字段的值为1时才更新数据库。

这个解决方案通过将所有数据发送到服务器来浪费一些带宽,但它完成了工作。