我正在尝试更新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;
}
?>
就像我说的那样,它不会更新,也没有错误。我在这里做错了吗?
答案 0 :(得分:1)
HTML表单存在一些缺陷:
<form id="tablePrice"...></form>
因此,在需要提交的任何表单元素之后移动结束标记(即</form>
)。<td><input type="Number" name="ItemCode" value="<?php echo $res['ItemCode'];?>" readonly /></td>
/
)添加到每个输入标记的末尾 - <input type="text" name="NewMX" id="newMX" class="form-control" value="<?php if(!empty($res['NewMX'])){echo $res['NewMX'];}?>" />
< / LI>
醇>
只要能够一次更新多个记录(根据您的问题标题),为了做到这一点,您可能需要更新UPDATE SQL查询以获得一些基于的逻辑ItemCode值(例如CASE statements)。表单字段需要采用数组格式(例如 CustomerIncrease [] )或具有唯一名称(可能附加了ItemCode值-eg CustomerIncrease_1 )以便关联要更新的行的各种值。
正如@Magnus Eriksson建议的那样,您应该使用预准备语句(带有绑定参数)来避免SQL注入攻击。因此,您可以使用sqlsrv_prepare()的第3个参数(参数数组)来简化您的PHP代码,如下例所示。
params :
执行参数化查询时指定参数信息的数组。
注意:您需要更新<input>
字段中名称属性的大小写,以匹配$字段中的名称 - 例如<input type="text" name="NewNet"...>
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);
}
答案 1 :(得分:0)
我在html代码中看不到ItemCode
,这使$itemCode
为空。
所以,这部分问题是:
WHERE ItemCode = '$itemCode'
变为:
WHERE ItemCode = ''
这是一个正确的查询,因此没有错误,虽然它没有更新记录。