Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 单个窗体上的多个更新按钮_Php_Mysql_Forms - Fatal编程技术网

Php 单个窗体上的多个更新按钮

Php 单个窗体上的多个更新按钮,php,mysql,forms,Php,Mysql,Forms,请原谅我的代码结构糟糕。我试图用一个单独的提交按钮来更新每一行的不同客户。删除功能非常有效。当为每一行单击submit按钮时,我试图传递该行的ID。但是,无论单击哪一行,我都会获取最后一行的ID。我附上了一个截图作为例子。感谢您的帮助 名称 电子邮件 电话 地址 产品 固件版本 购买日期 删除 您是否尝试过每行创建一个表单,而不是一个包装所有行的表单(我知道您要求以一个表单创建表单,所以您可能只想以这种方式创建)?它可能是你想要的,也可能不是。当我需要每行更新的多行时,我会这样做 <?

请原谅我的代码结构糟糕。我试图用一个单独的提交按钮来更新每一行的不同客户。删除功能非常有效。当为每一行单击submit按钮时,我试图传递该行的ID。但是,无论单击哪一行,我都会获取最后一行的ID。我附上了一个截图作为例子。感谢您的帮助


名称
电子邮件
电话
地址
产品
固件版本
购买日期
删除

您是否尝试过每行创建一个表单,而不是一个包装所有行的表单(我知道您要求以一个表单创建表单,所以您可能只想以这种方式创建)?它可能是你想要的,也可能不是。当我需要每行更新的多行时,我会这样做

<?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>

<?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>