【问题标题】:Input text type into array only last row仅将文本类型输入数组的最后一行
【发布时间】:2016-05-17 06:16:31
【问题描述】:

我有一些text input,可以像这样编辑一些主题的值:

while($row = mysql_fetch_row($result2))
    {
       echo "<tr><td>" . $row['ID'] . "</td><td>" . $row['Name'] . "</td><td>" . $row['Status'] . 
       "</td><td><input type=\"text\" size=\"4\" id=\"editmath[]\" name=\"editwater\" value=" . $row['math'] . ">
       </td><td><input type=\"text\" size=\"4\" id=\"editeng[]\" name=\"editfod\" value=" . $row['english'] .  ">
       </td><td><input type=\"text\" size=\"4\" id=\"editscie[]\" name=\"editmob\" value=" . $row['science'] . ">
        }
<Input type="Submit" value=" Next " name="submit_edit">

在 PHP 中我有这个代码:

if (isset($_GET['submit_edit'])) {
        $math[] = $_GET['editmath'];
        $eng[] = $_GET['editeng'];
        $scie[] = $_GET['editscie'];

        sql = "UPDATE student SET math = $math, english = $eng, science = $scie";
        $query = mysql_query($sql);
}

但是当我尝试print_rmatheng 时,他们只保存了最后一行。如何解决这个问题?

Desired Output :
Math    English   Science
4       3        2
7       8        10
3       5        12

【问题讨论】:

  • name 应该在 array() 而不是 id。您需要更改代码。

标签: php sql arrays input


【解决方案1】:

这里的名称与输入框重复,这就是发生此问题的原因:

1) 将输入框的名称声明为一个数组,并在 php 中循环通过该数组来存储数据:

2) 使用 POST 表单而不是 GET 表单类型:

3) 如果要更新记录,还需要将 id 与所有数据一起传递

例如

while($row = mysql_fetch_row($result2))
    {
       echo "<tr><td>" . $row['ID'] . "</td><td>" . $row['Name'] . "</td><td>" . $row['Status'] . 
       "</td><td><input type=\"text\" size=\"4\" id=\"editmath[]\" name=\"editwater[]\" value=" . $row['math'] . ">
       </td><td><input type=\"text\" size=\"4\" id=\"editeng[]\" name=\"editfod[]\" value=" . $row['english'] .  ">
       </td><td><input type=\"text\" size=\"4\" id=\"editscie[]\" name=\"editmob[]\" value=" . $row['science'] . ">
        }
<Input type="Submit" value=" Next " name="submit_edit">

PHP

if (isset($_POST['submit_edit'])) {
        for($i = 0; i < sizeof($_GET['editmath']) ; $i++) {
           $math = $_POST['editmath'][$i];
           $eng = $_POST['editeng'][$i];
           $scie = $_GET['editscie'][$i];

           $sql = "UPDATE student SET math = $math, english = $eng, science = $scie";
           $query = mysql_query($sql);
        }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-22
    • 2018-11-14
    • 1970-01-01
    • 1970-01-01
    • 2020-05-04
    • 1970-01-01
    • 2016-01-17
    • 1970-01-01
    相关资源
    最近更新 更多