单个表单上的多个更新按钮

时间:2015-04-12 21:14:23

标签: php mysql forms

请原谅我结构严密的代码。我正在尝试使用单独的提交按钮更新每行的不同客户。删除功能很有用。我正在尝试在每行单击提交按钮时传递行的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 个答案:

答案 0 :(得分: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>