2016-05-17 31 views
2

我有一些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 
+1

'name'应在'阵列()''未id'。您需要更改代码。 – Yash

回答

1

这名重复inputboxes这就是为什么这个问题是存在的:

1)通过数组声明输入框中的名称作为数组和PHP的循环来存储数据:

2)使用POST形式而不是GET形式类型:

3)如果要更新记录,你还需要与所有数据

EX一起传递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); 
     } 
} 
相关问题