2016-04-05 80 views
1

我的PHP代码有问题。我有一个来自数据库的数据表。在表格中我有一个包含name属性的输入。当我已经从数据库加载数据时,属性名称具有相同的值。所以,如果我想发布它们,只需要输入最后一个输入名称。这是我的代码:获取不同的Post方法名称

<html> 
    <head> 
    <title>Table PHP</title> 
    </head> 
    <body> 
    <form action="execute.php" method="POST"> 
    <table> 
     <tr> 
      <th>No.</th> 
      <th>Registration Number</th> 
      <th>Customer Name</th> 
      <th>Action</th> 
     </tr> 
     <?php 
    include "connect.php"; 
    $sql = mysql_query("SELECT numreg,name FROM registration WHERE (numreg NOT IN(SELECT validation FROM tablevalidation)) "); 
    $no = 1; 
    while($result = mysql_fetch_array($sql)){ 
     echo " 
     <tr> 
      <td>$no.</td> 
      <td><input type='hidden' name='numreg' value='$result[numreg]' />$result[numreg]</td> 
      <td>$result[name]</td> 
      <td><input type='submit' name='validation' value='Validation' /></td> 
     </tr>"; 
     $no++; 
    } 
?> 
    </table> 
    </form> 
    </body> 
</html> 

通过上面的代码,输入name ='numreg'将为每个输入标签生成相同的值。因此,如果我在表单中使用POST方法,则即使选择第一条记录,也只会执行最后一条记录。我怀疑这是因为输入标签属性名称具有相同的值。那么,如何在我的输入标签中获得不同的属性名称来解决我的问题?请帮忙。

+0

改变输入类型名称numreg比尝试 – iOS

+0

感谢另一名建议@Darji Jigar,但我不认为它可以解决我的问题,因为即使我改变输入名称numreg到另一个名称将保持一样。我用while循环来从数据库加载数据,从而,其结果将是这样的:1.​​​​<输入名称= “numreg”/>,​​2.​​<输入名称= “numreg”/>,​​3.​​。 –

+0

你有没有试过下面的答案? @ Mike.Drinkwater – RJParikh

回答

0

使用下面的代码。您需要将numreg定义为数组,以便在后期显示所有记录。将name='numreg'更改为name='numreg_".$no."',以便为所有人定义唯一的numreg。

<html> 
    <head> 
    <title>Table PHP</title> 
    </head> 
    <body> 
    <form action="execute.php" method="POST"> 
    <table> 
     <tr> 
      <th>No.</th> 
      <th>Registration Number</th> 
      <th>Customer Name</th> 
      <th>Action</th> 
     </tr> 
     <?php 
    include "connect.php"; 
    $sql = mysql_query("SELECT numreg,name FROM registration WHERE (numreg NOT IN(SELECT validation FROM tablevalidation)) "); 
    $no = 1; 
    while($result = mysql_fetch_array($sql)){ 
     echo " 
     <tr> 
      <td>$no.</td> 
      **<td><input type='hidden' name='numreg_".$no."' value='$result[numreg]' />$result[numreg]</td>** 
      <td>$result[name]</td> 
      <td><input type='submit' name='validation' value='Validation' /></td> 
     </tr>"; 
     $no++; 
    } 
?> 
    </table> 
    **<input type='hidden' id='totalRecords' name='totalRecords' value='<?php echo $no; ?>' />** 
    </form> 
    </body> 
</html> 

<?php 

if($_POST['validation']) 
{ 
    for($i=1;$i<=$_POST['totalRecords'];$i++) 
    { 
     **echo $_POST['numreg_$i']."<br>";** 
    } 
} 
?> 
+0

尝试在你的代码中进行以上更改并给出你的正面评价@Mike。 Drinkwater – RJParikh

+0

我已经使用它,但它没有奏效。 –

+0

你能指定你的输出和欲望输出吗? @ Mike.Drinkwater – RJParikh