2011-12-30 72 views
2

我创建了一个html表单,使用户能够更新数据。但是输入数据(数组值)不会传递到php SUBMIT,因此单击SUBMIT不会更新表。当我进入脚本的SUBMIT部分并将SET更改为特定的数字或文本时,表格会更新。这意味着来自html输入数据数组的值不能正确传递到脚本的SUBMIT部分。任何帮助赞赏。HTML输入表单数组值不通过PHP提交

<?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(); 
?> 

谢谢!

+0

您正在用循环创建HTML,并且您的输入具有'id'。如果while循环执行多次,您将拥有重复的id – Mikhail 2011-12-30 20:07:09

回答

1

您在SQL语句中缺少围绕您的$name[$i]的单引号。如果id不总是数字,则还需要用单引号括住$id[$i]

$sql1="UPDATE contacts SET name= '".$name[$i]."' WHERE id= ".$id[$i].""; 
//------------------------------^^^-----------^^^ 

您的mysql_query()调用中的一些错误检查会使这个更清晰。见下文。

而且在将它们传递给查询之前,您必须针对SQL注入进行过滤。

for($i=0;$i<$num;$i++) { 
    // Call mysql_real_escape_string() to sanitize these... 
    $id[$i] = mysql_real_escape_string($id[$i]); 
    $name[$i] = mysql_real_escape_string($name[$i]); 

    $sql1="UPDATE contacts SET name= '".$name[$i]."' WHERE id= ".$id[$i].""; 

    $result1 = mysql_query($sql1); 
    // Error checking: 
    if (!$result1) { 
    echo mysql_error(); 
    } 
} 
+0

问题已修复。感谢你的协助。我将在今天晚些时候发布最终脚本,供任何稍后需要它的人使用。 – 2011-12-30 21:50:42

0

我的错误。我没有看足够的表格。你在这里分配一个数组。 PHP是很容易调试 -

就在这里:

 $id=$_POST['id']; 
    $name=$_POST['name']; 

这些线路后用var_dump($ ID)或print_r的($ ID)在您的变量,以检查出的内容。

+0

修正了问题。感谢你的协助。我将在今天晚些时候发布最终脚本,供任何稍后需要它的人使用。 – 2011-12-30 21:50:50

0

非常感谢您的及时答复和提供的帮助。在实施建议的更改后,最终工作脚本(使用PHP 5.2)如下所示(适用于任何可能需要它的人)。

<?php 
    Mysql connection, initial query and then close Mysql connection 
    ?> 

<table width="500" border="0" cellspacing="1" cellpadding="0"> 
<form name="Contacts" method="post" action=""> 
<tr> 
<td> 
<table width="500" 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 height="75" colspan="4" align="center"><input type="submit" name="Submit" value=" save for later "></td> 
</tr> 
</table> 
</td> 
</tr> 
</form> 
</table> 

<?php 
// Check if button name "Submit" is active, do this 
if(isset($_POST['Submit'])){ 
mysql_connect($dbhost,$username,$password); 
@mysql_select_db($database) or die("Unable to select database"); 

$name=$_POST['name']; 

for($i=0;$i<$num;$i++) { 

// sanitize... 
    $id[$i] = mysql_real_escape_string($id[$i]); 
    $name[$i] = mysql_real_escape_string($name[$i]); 

    $sql1="UPDATE test_mysql SET name= '".$name[$i]."' WHERE id= ".$id[$i].""; 

    $result1 = mysql_query($sql1); 
    // Error checking: 
    if (!$result1) { 
    echo mysql_error(); 
    } 
} 
if($result1){ 
header("location:updated.php"); 
} 
} 

mysql_close(); 
?>