【发布时间】:2011-12-30 18:17:50
【问题描述】:
我创建了一个 html 表单以使用户能够更新数据。但是输入数据(数组值)不会传递给 php SUBMIT,因此单击 SUBMIT 不会更新表。当我进入脚本的 SUBMIT 部分并将 SET 更改为特定数字或文本时,表格会更新。这意味着来自 html 输入数据数组的值未正确传递到脚本的 SUBMIT 部分。任何帮助表示赞赏。
<?php
//Mysql connection and initial select query placed above
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<form name="Contacts" method="post" action="">
<tr>
<td>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><?php $id[]=$rows['id']; ?><?php echo $rows['id']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<?php echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname[]" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>"></td>
<td align="center"><input name="email[]" type="text" id="email" value="<?php echo $rows['email']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if(isset($_POST['Submit'])){
$id=$_POST['id'];
$name=$_POST['name'];
for($i=0;$i<$num;$i++){
$sql1="UPDATE contacts SET name= ".$name[$i]." WHERE id= ".$id[$i]."";
$result1=mysql_query($sql1);
}
}
if($result1){
header("location:updated.php");
}
mysql_close();
?>
谢谢!
【问题讨论】:
-
您正在使用循环创建 HTML,并且您的输入有一个
id。如果 while 循环多次执行,您将拥有重复的 id