duanlu1793
2013-03-09 00:18
浏览 38
已采纳

表单中的值未通过php html mysqli

I can't figure out why my values aren't being passed from the form. I can't spot an error.

The Form Code:

$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>";

The php file:

$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++;

}

I am avoiding displaying the id column with all the counts. The output of the echo-ed queries are:

Any ideas?

EDIT I'll take care of changing everything over to procedural style once I figure out this issue. If I get rid of the mysqli_real_escape_string, it passes all the data except those columns with spaces in them. I thought that's what backticks were for? Is there something else I can do to make the columns with two words pass data like those with one word?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dse84825 2013-03-09 00:26
    已采纳

    You need to switch these rows -

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

    to

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

    Having the <form> inside the <table> is invalid code. It either needs to wrap the <table> or be inside <td></td>.

    see also -
    form inside table
    Form inside a table


    Update #1-
    On your Edit
    Spaces in <input name=""> will be replaced with _ so your $_POST[] name will not match your <input name="">. from the manual - http://www.php.net/manual/en/language.variables.external.php

    Note:
    Dots and spaces in variable names are converted to underscores. For example <input name="a.b" /> becomes $_REQUEST["a_b"].

    see also -
    Can <input> elements have capital letters and spaces in PHP

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题