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

表单中的值未通过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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 CSS实现渐隐虚线边框
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题