0

我有一个更新表单,它使用循环来回显要更新的记录列表。它包括一个文本字段“句子”和一个复选框“可接受”。我的 foreach 努力没有奏效,并且正在吞噬数据列,所以是时候寻求帮助了。先生们,女士们,我该怎么写呢?

谢谢你的帮助。

<?php 
if( isset($_POST["Sentences_ID"]) ) {
    foreach($_POST['Sentences_ID'] as $key=>$value) {
        $updateSQL = sprintf("UPDATE Sentences SET Sentences=%s, Acceptable=%s WHERE   Sentences_ID=$value",
            GetSQLValueString($_POST['Sentences'], "text"),
            GetSQLValueString($_POST['Acceptable'], "text"),
            ($_POST['Sentences_ID']));

        mysql_select_db($database_name, $name);
        $Result1 = mysql_query($updateSQL, $name) or die(mysql_error());
    }
}
?>


<?php
do {
?>
Sentence:<input type="hidden" name="Sentences_ID[]" id="Sentences_ID[< ?php echo     $row_rsCounting['Sentences_Id'];?>]" value="<?php echo $row_rsCounting['Sentences_Id'];?>"   />

Acceptable:<input type="checkbox" name="Acceptable" id="Acceptable"     value="Acceptable"/></label> | 
<input type="text" name="Sentences" id="Sentences" value="< ?php echo     $row_rsCounting['Sentences'];?>" size="50" />
<?php
} while ($row_rsCounting = mysql_fetch_assoc($rsCounting));
?>
4

2 回答 2

1

Acceptable 和 Sentences 输入也需要是数组,例如 name="Acceptable[]"。然后你可以像 $_POST['Acceptable'][$key] 在你的 PHP 循环中访问它们。但是,我认为不能保证多个输入数组的排序相同,因此我建议在输入名称中使用 Sentence ID。对所有输入执行此操作。

像这样的东西...

<?php 
if( isset($_POST["Sentences_ID"]) ) {
    foreach($_POST['Sentences_ID'] as $key=>$value) {
        $updateSQL = sprintf("UPDATE Sentences SET Sentences=%s, Acceptable=%s WHERE   Sentences_ID=$value",
            GetSQLValueString($_POST['Sentences'][$key], "text"),
            GetSQLValueString($_POST['Acceptable'][$key], "text"),
            ($value));

        mysql_select_db($database_name, $name);
        $Result1 = mysql_query($updateSQL, $name) or die(mysql_error());
    }
}
?>


<?php
do {
?>
Sentence:<input type="hidden" name="Sentences_ID[< ?php echo  $row_rsCounting['Sentences_Id'];?>]" id="Sentences_ID[< ?php echo     $row_rsCounting['Sentences_Id'];?>]" value="<?php echo $row_rsCounting['Sentences_Id'];?>"   />

Acceptable:<input type="checkbox" name="Acceptable[< ?php echo     $row_rsCounting['Sentences_Id'];?>]" id="Acceptable" value="Acceptable"/></label> | 
<input type="text" name="Sentences[< ?php echo $row_rsCounting['Sentences_Id'];?>]" id="Sentences" value="< ?php echo $row_rsCounting['Sentences'];?>" size="50" />
<?php
} while ($row_rsCounting = mysql_fetch_assoc($rsCounting));
?>
于 2013-03-14T17:39:22.677 回答
0

我的第一个建议是做一些调试。

foreach($_POST['Sentences_ID'] as $key=>$value) {

print_r($_POST['Sentences_ID']);die;

这应该让您对尝试迭代的变量的结构有所了解

于 2013-03-14T17:15:48.613 回答