【问题标题】:Update multiple records at once with PHP使用 PHP 一次更新多条记录
【发布时间】:2016-12-12 15:58:53
【问题描述】:

我正在尝试更新 HTML 表单中的多个记录,但它没有写回任何数据,而且我没有收到任何错误。

表已经存在并且已经有一半的数据,所以我需要更新记录,而不是插入。

这是我的表格:

<form method="post" action="test.php" id="price-increase"></form>
<div class="x_panel">
<div class="x_content">             
    <table id="tablePrice" class="display table table-striped table-bordered dt-responsive">
        <thead>
            <tr>
                <th>Item Code</th>
                <th>Customer Increase</th>
                <th>New Invoice</th>
                <th>New Net</th>
                <th>New Matrix</th>
                <th>New Band A</th>
                <th>Incresed Date</th>
            </tr>
        </thead>
        <tbody>
            <?php while($res = sqlsrv_fetch_array($sql, SQLSRV_FETCH_ASSOC)) : ?>
                <tr>
                    <td><?php echo $res['ItemCode'];?></td>
                    <td>
                        <input type="text" name="customerIncrease" id="customerIncrease" class="form-control" value="<?php if(!empty($res['CustomerIncrease'])){echo $res['CustomerIncrease'];}?>">
                    </td>
                     <td>
                        <input type="text" name="newInvoice" id="newInvoice" class="form-control" value="<?php if(!empty($res['NewInvoice'])){echo $res['NewInvoice'];}?>">
                    </td>
                    <td>
                        <input type="text" name="newNet" id="newNet" class="form-control" value="<?php if(!empty($res['NewNet'])){echo $res['NewNet'];}?>">
                    </td>
                    <td>
                        <input type="text" name="newMX" id="newMX" class="form-control" value="<?php if(!empty($res['NewMX'])){echo $res['NewMX'];}?>">
                    </td>
                    <td><?php echo $res['NewBandA'];?>
                        <input type="text" name="newBandA" id="newBandA" class="form-control" value="<?php if(!empty($res['NewBandA'])){echo $res['NewBandA'];}?>">
                    </td>
                    <td>
                        <input id="increaseDate" name="increaseDate" data-date-format="dd/mm/yyyy" class="form-control col-md-7 col-xs-12" required="required" type="text" value="<?php if(!empty($res['IncreaseDate'])){echo $res['IncreaseDate'];}?>">
                    </td>
                </tr>
            <?php endwhile; ?>
        </tbody>                        
    </table>    
    <a href="test.php">
        <button type="submit" id="submit" name="submit" class="btn btn-success pull-right" value="Submit">Save</button>
    </a>
</div>

这是我的 PHP:

<?php 
if(isset($_POST['submit'])){
    $itemCode = (isset($_POST['ItemCode']) && !empty($_POST['ItemCode']))?$_POST['ItemCode'] : NULL;
    $customerIncrease = (isset($_POST['CustomerIncrease']) && !empty($_POST['CustomerIncrease']))?$_POST['CustomerIncrease'] : NULL;
    $newInvoice = (isset($_POST['NewInvoice']) && !empty($_POST['NewInvoice']))?$_POST['NewInvoice'] : NULL;
    $newNet = (isset($_POST['NewNet']) && !empty($_POST['NewNet']))?$_POST['NewNet'] : NULL;
    $newMX = (isset($_POST['NewMX']) && !empty($_POST['NewMX']))?$_POST['NewMX'] : NULL;
    $newBandA = (isset($_POST['NewBandA']) && !empty($_POST['NewBandA']))?$_POST['NewBandA'] : NULL;
    $increaseDate = (isset($_POST['IncreaseDate']) && !empty($_POST['IncreaseDate']))?$_POST['IncreaseDate'] : NULL;
    $processed = (isset($_POST['Processed']) && !empty($_POST['Processed']))?$_POST['Processed'] : NULL;

    $query = "  UPDATE po_SupplierPriceIncrease 

                SET CustomerIncrease = '$customerIncrease',
                    NewInvoice = '$newInvoice',
                    NewNet = '$newNet',
                    NewMX = '$newMX',
                    NewBandA = '$newBandA',
                    IncreaseDate = '$increaseDate',
                    Processed = '$processed'

                WHERE ItemCode = '$itemCode';
                    ";
    $stmt = sqlsrv_prepare($sapconn2, $query);
    sqlsrv_execute($stmt);   
    return $stmt;

    }
?>

就像我说的,它不会更新,也没有错误。我在这里做错了吗?

【问题讨论】:

标签: php sql sql-server-2008 forms http-post


【解决方案1】:

HTML 表单有几个缺陷:

  1. 表单标签在打开后立即关闭 - 即&lt;form id="tablePrice"...&gt;&lt;/form&gt; 因此,将结束标记(即&lt;/form&gt;)移到需要提交的任何表单元素之后。
  2. ItemCode 未与表单一起提交。创建一个输入(也许它应该被隐藏和/或只读)以提交该值 - 例如&lt;td&gt;&lt;input type="Number" name="ItemCode" value="&lt;?php echo $res['ItemCode'];?&gt;" readonly /&gt;&lt;/td&gt;
  3. 因为input element 不能有允许的内容,它们是自闭合的,所以在每个输入标签的末尾添加(正向)斜杠(即/) -&lt;input type="text" name="NewMX" id="newMX" class="form-control" value="&lt;?php if(!empty($res['NewMX'])){echo $res['NewMX'];}?&gt;" /&gt;

就能够一次更新多条记录(根据您的问题标题),为了做到这一点,您可能需要更新您的 UPDATE SQL 查询以具有一些基于ItemCode 值(例如,CASE statements)。并且表单字段需要采用数组格式(例如 CustomerIncrease[])或具有唯一名称(可能附加 ItemCode 值 - 例如 CustomerIncrease_1)以便关联要更新的行的各种值。

正如@Magnus Eriksson 建议的那样,您应该使用准备好的语句(带有绑定参数)来避免 SQL 注入攻击。因此,您可以使用sqlsrv_prepare() 的第三个参数(一个参数数组)来简化您的 PHP 代码,如下例所示。

参数
执行参数化查询时指定参数信息的数组。

注意:您需要更新 &lt;input&gt; 字段中 name 属性的大小写以匹配 $fields 中的名称 - 例如&lt;input type="text" name="NewNet"...&gt;

if(isset($_POST['submit']) && $_POST['ItemCode']) {
    //these fields should match the name attribute of the inputs in the form
    $fields = array('CustomerIncrease','NewInvoice','NewNet','NewMX','NewBandA','IncreaseDate'    );
    $params = array();
    $setFields = array();
    foreach($fields as $field) {
        if (isset($_POST[$field]) && !empty($_POST[$field])) {
            $params[] = &$_POST[$field];
            $setFields[] = $field.' = ?';
        }
        else {
            $setFields[] = $field.' = NULL';
        }
    }

    //optional : add ProcessedDate to setFields with value from GetDate()?
    $params[] = &$_POST['ItemCode'];

    $query = "  UPDATE po_SupplierPriceIncrease 
            SET ".implode(', ',$setFields)."
            WHERE ItemCode = ?";
    $stmt = sqlsrv_prepare($connection, $query,$params);
    sqlsrv_execute($stmt);
}

【讨论】:

    【解决方案2】:

    我在 html 代码中没有看到 ItemCode,这使得 $itemCode 为空。

    所以,这部分查询:

    WHERE ItemCode = '$itemCode'
    

    变成:

    WHERE ItemCode = ''
    

    这是一个正确的查询,因此没有错误,尽管它不更新任何记录。

    【讨论】:

      猜你喜欢
      • 2015-01-13
      • 2011-02-14
      • 1970-01-01
      • 1970-01-01
      • 2021-09-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-11
      相关资源
      最近更新 更多