PHP post总是返回最后一个值

时间:2014-06-23 14:46:35

标签: php html mysql

我试图创建一个简单的PHP页面来检索并显示来自数据库的信息,所以我这样做,这很好用:

echo "<form action='#' method='post'>";
if(!empty($result)){
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        echo "\t<tr>\n";
        echo "<td><input type = 'hidden' name = 'value_id' id = 'value_id' value = '" . $row['id'] . "'/></td>";
        echo "<td><input type = 'text' name = 'name_value' id = 'name_value' value = '" . $row['name'] . "'/></td>\n";
        echo "<td><input type = 'text' name = 'addr_value' id = 'addr_value' value = '" . $row['addr'] . "'/></td>\n";
        echo "<td><INPUT type='submit' name = 'modifyItem' value ='Modify'></td>\n";
        echo "<td><INPUT type='submit' name = 'deleteItem' value ='Delete'></td>\n";
        echo "\t</tr>\n";
    }
}
echo "</form>";

每行都有一个删除按钮,允许用户单独删除每一行,所以我看到是否单击了删除按钮,然后通过获取ID继续删除该行:

if(isset($_POST['deleteItem'])){
    $querydelete = "DELETE FROM address WHERE ID = " . $_POST["value_id"];
    mysql_query($querydelete) or die('Query failed: ' . mysql_error());
}

问题是在post之后ID值总是相同的(这是最后一行的ID值)。如果我事先为每一行显示ID,那么它就是正确的ID,因此问题必须是它在帖子中获得的价值。 我做错了什么?

7 个答案:

答案 0 :(得分:3)

这是因为您在表单中创建了许多具有相同名称的字段。您可以为每条记录创建单独的表单:

if(!empty($result)){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "<form action='#' method='post'>"
    echo "\t<tr>\n";
    echo "<td><input type = 'hidden' name = 'value_id' id = 'value_id' value = '" . $row['id'] . "'/></td>";
    echo "<td><input type = 'text' name = 'name_value' id = 'name_value' value = '" . $row['name'] . "'/></td>\n";
    echo "<td><input type = 'text' name = 'addr_value' id = 'addr_value' value = '" . $row['addr'] . "'/></td>\n";
    echo "<td><INPUT type='submit' name = 'modifyItem' value ='Modify'></td>\n";
    echo "<td><INPUT type='submit' name = 'deleteItem' value ='Delete'></td>\n";
    echo "\t</tr>\n";
    echo "</form>";
    }
}

答案 1 :(得分:1)

您正在为所有字段集使用 SAME 字段名称。由于您未在名称上使用特定于PHP的[]数组扩展/提示,因此PHP将仅处理提交的LAST字段名称集。

e.g。

<input type="text" name="foo" value="a" />
<input type="text" name="foo" value="b" />
<input type="text" name="foo" value="c" />

var_dump($_POST);

Array(1) {
   'foo' => 'c'
}

<input type="text" name="foo[]" value="a" />  <--note the [] here
<input type="text" name="foo[]" value="b" />  <--note the [] here
<input type="text" name="foo[]" value="c" />  <--note the [] here

var_dump($_POST);

Array(1) {
   'foo' => array(
       0 => 'a'
       1 => 'b'
       3 => 'c'
   )
}

答案 2 :(得分:1)

尝试:

if(!empty($result)){
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    echo "<form action='#' method='post'><td><input type = 'hidden' name = 'value_id' id = 'value_id' value = '" . $row['id'] . "'/></td>";
    echo "<td><input type = 'text' name = 'name_value' id = 'name_value' value = '" . $row['name'] . "'/></td>\n";
    echo "<td><input type = 'text' name = 'addr_value' id = 'addr_value' value = '" . $row['addr'] . "'/></td>\n";
    echo "<td><INPUT type='submit' name = 'modifyItem' value ='Modify'></td>\n";
    echo "<td><INPUT type='submit' name = 'deleteItem' value ='Delete'></td>\n";
    echo "\t</form></tr>\n";
    }
}

答案 3 :(得分:1)

您有一个包含更多name=value_id元素的表单。当您提交表单时,$_POST中的最后一个(有第一个,第二个覆盖等等,最后一个)。

用于每一行新表单,或在名称中使用数组,例如。 name="value[]"

答案 4 :(得分:1)

我已经对JS和PHP做了同样的事情:

HTML:[index]

echo "<td> <button name=\"$row['id']\" type=\"clear\" >Clear</button>  </td>";

JavaScript:[index]

<script>
$(document).ready(function(){
     $('[type="clear"]').on('click', function(){ 
         document.location.href = "./php/yourScript.php?param=" + $(this).attr("name");
     })
});
</script>

PHP:[。/ php / yourScript.php]

<?php
$querydelete = "DELETE FROM address WHERE ID = " . $_GET["param"];
    mysql_query($querydelete)
?>

我希望,这可以帮助你:)

答案 5 :(得分:1)

如果您想将所有控件放在一个表单中,请更改您的代码。

echo "<form action='#' method='post'>";
if(!empty($result)){
 while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    echo "\t<tr>\n";
    echo "<td><input type = 'hidden' name = 'field[" . $row['id'] . "][value_id]' id = 'value_id-" . $row['id'] . "' value = '" . $row['id'] . "'/></td>";
    echo "<td><input type = 'text' name = 'field[" . $row['id'] . "][name_value]' id = 'name_value-" . $row['id'] . "' value = '" . $row['name'] . "'/></td>\n";
    echo "<td><input type = 'text' name = 'field[" . $row['id'] . "][addr_value]' id = 'addr_value-" . $row['id'] . "' value = '" . $row['addr'] . "'/></td>\n";
    echo "<td><INPUT type='submit' name = 'field[" . $row['id'] . "][modifyItem]' value ='Modify'></td>\n";
    echo "<td><INPUT type='submit' name = 'field[" . $row['id'] . "][deleteItem]' value ='Delete'></td>\n";
    echo "\t</tr>\n";
 }
}
echo "</form>";

答案 6 :(得分:1)

要解决此问题,您需要将它们包装成单独的形式...

while(rows_available){
     echo "<form action='#' method='post'>"
     echo "<td><input type = 'hidden' name = 'value_id' id = 'value_id' value = '" . $row['id'] . "'/></td>";
        echo "<td><input type = 'text' name = 'name_value' id = 'name_value' value = '" . $row['name'] . "'/></td>\n";
        echo "<td><input type = 'text' name = 'addr_value' id = 'addr_value' value = '" . $row['addr'] . "'/></td>\n";
        echo "<td><INPUT type='submit' name = 'modifyItem' value ='Modify'></td>\n";
        echo "<td><INPUT type='submit' name = 'deleteItem' value ='Delete'></td>\n";
        echo "\t</tr>\n";
        echo "</form>";
}//end of while loop