表单中的值未通过php html mysqli

时间:2013-03-09 00:18:44

标签: php html forms mysqli

我无法弄清楚为什么我的值没有从表单中传递出来。我无法发现错误。

表格代码:

$table = $_POST['table'];
$id = $_POST['id'];
$count = 0;    
$query = "SELECT * FROM `" . $table . "` WHERE id = " . $id;
        $result1 = mysqli_query($link, $query);     
        echo '<center><table style="text-align:center">';
        echo '<form action="edit-process.php" method="post">';
        while($row = mysqli_fetch_assoc($result1)){
            foreach($row as $key => $val){
                if ($count > 0){
                    echo "<tr>";
                    echo "<td>" . $key . "</td>";
                        echo '<td><input type="text" name="' . $key . '" value="' . $val . '"></td>';
                    echo "</tr>";
                    $count++;
                }
                else $count++;
            }
        }
        echo '<input type="hidden" name="table" value="' . $table . '" />';
        echo '<input type="hidden" name="id" value="' . $id . '" />';
        echo '<tr><td><input type="submit" value="Save Changes" /></td></tr>';
        echo "</form>";
        echo "</table>";

php文件:

$table = $_POST['table'];
$id = $_POST['id'];
$count1 = 0;
$count2 = 0;

$result = mysqli_query($link, "SHOW COLUMNS FROM `" . $table . "`");
    if (mysqli_num_rows($result) > 0) {
        while ($row = mysqli_fetch_assoc($result)) {
            $keyNames[$count2] = $row['Field'];
        $count2++;
        }
    }

while ($count1 < $count2){
    if ($count1 > 0) {
    $value = mysqli_real_escape_string($_POST[$keyNames[$count1]]);
    $query2 = "UPDATE `" . $table . "` SET `" . $keyNames[$count1] . "` = '" . $value . "' WHERE id = " . $id;
    echo $query2 . "<br>";
    $result2 = mysqli_query($link, $query2);
    $count1++;
}
else $count1++;

}

我正在避免显示包含所有计数的id列。回送查询的输出为:

有什么想法吗?

修改 一旦我弄清楚这个问题,我会把一切都改为程序风格。如果我摆脱了mysqli_real_escape_string,它会传递除了包含空格的那些列之外的所有数据。我认为那是什么反引号?我还能做些什么来使用两个单词的列传递像一个单词那样的数据吗?

1 个答案:

答案 0 :(得分:4)

您需要切换这些行 -

echo '<center><table style="text-align:center">';
echo '<form action="edit-process.php" method="post">';
....
echo "</form>";
echo "</table>";

echo '<form action="edit-process.php" method="post">';
echo '<center><table style="text-align:center">';
....
echo "</table>";
echo "</form>";

<form><table>是无效代码。它要么需要包裹<table>,要么在<td></td>内。

另见 -
form inside table
Form inside a table


更新#1-
修改<input name="">中的空格将替换为_,因此您的$_POST[]名称将与您的<input name="">不匹配。来自手册 - http://www.php.net/manual/en/language.variables.external.php

  

注意:
  变量名中的点和空格将转换为下划线。例如, <input name="a.b" /> 变为 $ _ REQUEST [“a_b”]

另见 -
Can <input> elements have capital letters and spaces in PHP