2015-04-12 48 views
2

请原谅我的结构严重的代码。我试图用个人提交按钮更新每行上的不同客户。删除功能很好。我试图在每个行单击提交按钮时传递该行的ID。但是,无论我点击哪一行,我都会得到最后一行的ID。我附上了一个截图作为例子。任何帮助表示赞赏!单个表单上的多个更新按钮

Screenshot of output

<form action="" method="post"> 
<table class="table table-striped table-bordered table-responsive"> 
<thead> 
<tr> 
<th>Name</th> 
<th>Email</th> 
<th>Phone</th> 
<th>Address</th> 
<th>Product</th> 
<th>Firmware Version</th> 
<th>Purchase Date</th> 
<th>Delete</th> 
</tr> 
</thead> 
<?php 
$pdo = new PDO("mysql:host=localhost;dbname=project", $username, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION 
)); 
$query = $pdo->prepare("select * from customers"); 
$query->execute(); 
while($customers = $query->fetch()){ 
$ID = $customers['ID']; 
echo '<tr><td><input type="text" name="name" value="'; 
echo $customers['name']; 
echo '"></td>'; 
echo "<td>" . $customers['email'] . "</td>"; 
echo "<td>" . $customers['phone'] . "</td>"; 
echo "<td>" . $customers['address'] . "</td>"; 
echo "<td>" . $customers['product'] . "</td>"; 
echo "<td>" . $customers['firmware'] . "</td>"; 
echo "<td>" . $customers['purchase_date'] . "</td>"; 
echo '<td align="center"><input type="hidden" name="id" value="'; 
echo $ID; 
echo '"><input type="submit" name="delete" value="X"> </td></tr>'; 
echo '<tr><td colspan="8"><input type="hidden" name="id_update" value="'; 
echo $ID; 
echo '"><input type="submit" name="update" value="Update">'; 
echo $ID . '<--This is the ID for each row'; 
echo '</td></tr>'; 
} 

// Delete customer 
if(isset($_POST['delete'])) { 

try{ 
$ID = $_POST['id']; 
$query = $pdo->prepare("delete from customers where ID = :ID"); 
$query->bindParam(':ID', $ID); 
$query->execute(array(
':ID' => $ID 
)); 
echo "Customer successfully deleted."; 
echo '<META http-equiv="refresh" content="1;URL=view_edit.php">'; 
}catch(PDOException $e){ 
echo "Failed to delete the MySQL database table ... :".$e->getMessage(); 
} //end of try 
} //end of isset delete 


// Edit customer 
if(isset($_POST['update'])) { 
echo "Update " . $_POST['id_update'] . '<-- This is the result of clicking update for each row'; 
} //end of isset update 

?> 
</table> 
</form> 

回答

1

你有没有尝试过做每行一个形式,而不是用于包装所有行一种形式(我知道你是问有关在1点的形式做,所以你可能只是想只是这样做)?它可能会或可能不会是你想要的。当我需要多行更新时,我会这样做。

<?php 
    function UserForm($customers = array()) 
     { 
      ob_start(); ?> 
      <form action="" method="post"><?php 
       $ID = $customers['ID']; ?> 
       <tr> 
        <td><input type="text" name="name" value="<?php echo $customers['name']; ?>"></td> 
        <td><?php echo $customers['email']; ?></td> 
        <td><?php echo $customers['phone']; ?></td> 
        <td><?php echo $customers['address']; ?></td> 
        <td><?PHP echo $customers['product']; ?></td> 
        <td><?php echo $customers['firmware']; ?></td> 
        <td><?php echo $customers['purchase_date']; ?></td> 
        <td align="center"> 
         <input type="hidden" name="id" value="<?php echo $ID; ?>"> 
         <input type="submit" name="delete" value="X"> 
        </td> 
       </tr> 
       <tr> 
        <td colspan="8"> 
        <input type="hidden" name="id_update" value="<?php echo $ID; ?>" /> 
        <input type="submit" name="update" value="Update" /> 
        <?php echo $ID; ?><--This is the ID for each row --> 
        </td> 
       </tr> 
      </form> 
      <?php 
      $data = ob_get_contents(); 
      ob_end_clean(); 
      return $data; 
     } ?> 


<table class="table table-striped table-bordered table-responsive"> 
    <thead> 
     <tr> 
      <th>Name</th> 
      <th>Email</th> 
      <th>Phone</th> 
      <th>Address</th> 
      <th>Product</th> 
      <th>Firmware Version</th> 
      <th>Purchase Date</th> 
      <th>Delete</th> 
     </tr> 
    </thead> 
<?php 
$pdo = new PDO("mysql:host=localhost;dbname=project", $username, $password, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); 
$query = $pdo->prepare("select * from customers"); 
$query->execute(); 

while($customers = $query->fetch()){ 
    echo UserForm($customers); 
} 

// Delete customer 
if(isset($_POST['delete'])) { 
    try{ 
      $ID  = $_POST['id']; 
      $query = $pdo->prepare("delete from customers where ID = :ID"); 
      $query->bindParam(':ID', $ID); 
      $query->execute(array(':ID' => $ID)); 
      echo "Customer successfully deleted."; 
      echo '<META http-equiv="refresh" content="1;URL=view_edit.php">'; 
     }catch(PDOException $e){ 
      echo "Failed to delete the MySQL database table ... :".$e->getMessage(); 
     } //end of try 
    } //end of isset delete 

// Edit customer 
if(isset($_POST['update'])) { 
    echo "Update " . $_POST['id_update'] . '<-- This is the result of clicking update for each row'; 
} //end of isset update 

?> 
</table> 
+0

That worked !!!非常感谢!你的方式肯定更有意义。 – eod

+0

嘿没问题!很高兴我能帮助它工作! – Rasclatt