Php 是否将数据存储在以复选框为因素的数组中?

Php 是否将数据存储在以复选框为因素的数组中?,php,mysql,Php,Mysql,脚本: 将复选框结果存储在与哈希标记顺序相同的数组中,然后为更新添加条件 <?php include("connect.php"); ?> <?php /* * SELECT THE ROW BY THE "THREAD_ID" */ $tqs = "SELECT * FROM `thread` WHERE `id` = '" . $_GET['thread_id'] . "'"; $tqr = mysqli_que

脚本:



将复选框结果存储在与哈希标记顺序相同的数组中,然后为更新添加条件

<?php

    include("connect.php");

?>
<?php

    /*
     * SELECT THE ROW BY THE "THREAD_ID"
     */

    $tqs = "SELECT * FROM `thread` WHERE `id` = '" . $_GET['thread_id'] . "'";
    $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
    $row = mysqli_fetch_assoc($tqr);

?>
<?php
    /*
     * FETCH HASHTAG NAMES
     * PRINT HASHTAG NAMES INSIDE THE INPUT FIELDS
     */

    // Store the hashtag ID numbers inside an array.
    $explode_hashtags = explode(", ", $row['hashtag_id']);

    // Fetch the hashtag names from the table.
    // And store the hashtag names inside the array "$fetch_array_hashtags".
    $fetch_array_hashtags = array();
    for($i = 0; $i < count($explode_hashtags); $i++){
        $tqs_hashtags = "SELECT `hashtag` FROM `hashtags` WHERE `id` = '" . $explode_hashtags[$i] . "'";
        $tqr_hashtags = mysqli_query($dbc, $tqs_hashtags) or die(mysqli_error($dbc));
        $row_hashtags = mysqli_fetch_array($tqr_hashtags);
        $fetch_array_hashtags[] = $row_hashtags['hashtag'];
    }

?>
<?php

    echo "<form method='POST' action='" . $_SERVER['PHP_SELF'] . "'>";

    echo "<input type='hidden' name='thread_id' value='" . $_GET['thread_id'] . "' />";

    for ($i = 0; $i < count($fetch_array_hashtags); $i++) {
        echo "<input type='checkbox' name='hashtag_modify_checkbox[]' value='" . $explode_hashtags[$i] . "' />";
        echo "Modify";
        echo "<input type='text' name='hashtag[]' value='" . $fetch_array_hashtags[$i] . "' />";
    }

    echo "<input type='submit' name='submit' />";
    echo "</form>";

?>
<?php

    if(isset($_POST['submit'])){

        $tqs = "SELECT `hashtag_id` FROM `thread` WHERE `id` = '" . $_POST['thread_id'] . "'";
        $tqr = mysqli_query($dbc, $tqs) or die(mysqli_error($dbc));
        $row = mysqli_fetch_assoc($tqr);

        // Store the hashtag ID numbers inside an array.
        $explode_hashtags = explode(", ", $row['hashtag_id']);

        // Store the hashtag names inside an array.
        $hashtags = array();
        for($i = 0; $i < count($_POST['hashtag']); $i++) {
            if (!empty($_POST['hashtag'][$i])) {
                $hashtags[] = $_POST['hashtag'][$i];
            }
        }

        for($i = 0; $i < count($explode_hashtags); $i++){
            $tqs_hashtags_update = "UPDATE `hashtags` SET `hashtag` = '" . $hashtags[$i] . "' WHERE `id` = '" . $explode_hashtags[$i] . "'";
            //$tqs_hashtags_update = "UPDATE `hashtags` SET `hashtag` = '#blue_topy' WHERE `id` = '116'";
            $tqr_hashtags_update = mysqli_query($dbc, $tqs_hashtags_update) or die(mysqli_error($dbc));
        }

    }

?>
for($i=0;$i

然后,它将只更新标记了复选框的哈希标记

我认为最好为旧哈希标记添加隐藏值,以检查它们是否已更改。然后只需更新更改的hashtag

for($i = 0; $i < count($explode_hashtags); $i++){

        if ($checkArray[$i] === true){
        $tqs_hashtags_update = "UPDATE `hashtags` SET `hashtag` = '" . $hashtags[$i] . "' WHERE `id` = '" . $explode_hashtags[$i] . "'";
        //$tqs_hashtags_update = "UPDATE `hashtags` SET `hashtag` = '#blue_topy' WHERE `id` = '116'";
        $tqr_hashtags_update = mysqli_query($dbc, $tqs_hashtags_update) or die(mysqli_error($dbc));
    }
    }
for($i=0;$i
据我所知,
hashtag\u modify\u复选框
未定义为POST变量。您只是在复选框的值中使用了
$explode\u hashtags[$i]
。我很好奇,一旦发布表单,GET['thread\u id']变量(在隐藏输入中)会发生什么变化。你不会失去价值吗?(我知道这与你的问题并不完全相关……只是一个观察。GhostRider,快速阐述:当点击“提交”按钮时,URL从“.php?thread\u id=number”变为“.php”,因此我也通过POST方法使用“thread\u id”。非常感谢!这个建议对我自己有效!
for($i = 0; $i < count($fetch_array_hashtags); $i++) {
    echo "<input type='hidden' name='hashtag_old[]' value='".$fetch_array_hashtags[$i]."' />";
    echo "Modify";
    echo "<input type='text' name='hashtag[]' value='".$fetch_array_hashtags[$i]."' />";
}
# ... some more code ...
$hashtags = array();
for($i = 0; $i < count($_POST['hashtag']); $i++) {
    if (!empty($_POST['hashtag'][$i]) and $_POST['hashtag'][$i]<>$_POST['hashtag_old'][$i]) {
        $hashtags[] = $_POST['hashtag'][$i];
    }
}