opID, opDateAdded, opLastUpdated, opUser, opPropertySaleID, opArray, refurbID, opRefurbCost, opViewingArranged, opOfferAccepted, opOfferAcceptedID, opPaon, opStreet, opPostcode, opPropertyType, opViewingDate, opViewingBy, opViewingPersonName, opFloorArea, opbedrooms, opBathrooms, opReceptions, opAskingPrice, opValuation, opOptMatchingbedrooms, opOptMatchingBuild, opOptMatchingType, opOptSimilarFloor, opOptdistance, opLatitude, opLongitude, opNotes
我希望有一个函数允许我更新此表的列,但有时只需3-4个列就需要更新,而不是所有列都需要更新.
我只是想知道接近这个的最佳方法是什么?
function updateOpportunity($opID, $opDateAdded, $opLastUpdated, $opUser, $opPropertySaleID, $opArray, $refurbID, $opRefurbCost, $opViewingArranged, $opOfferAccepted, $opOfferAcceptedID, $opPaon, $opStreet, $opPostcode, $opPropertyType, $opViewingDate, $opViewingBy, $opViewingPersonName, $opFloorArea, $opbedrooms, $opBathrooms, $opReceptions, $opAskingPrice, $opValuation, $opOptMatchingbedrooms, $opOptMatchingBuild, $opOptMatchingType, $opOptSimilarFloor, $opOptdistance, $opLatitude, $opLongitude, $opNotes) {
//update
}
并将它们设置为可选,然后检查它们是否已设置,如果它们是更新那些行(在MysqL中使用IFNULL)
function updateOpportunity($property) {
//update
}
我只是想知道是否有一个标准来创建一个函数来更新大量的列,这些列并不总是需要同时更新.
解决方法:
function update($table, $data, $id)
{
$set= array();
foreach ($data as $key => $value)
{
$set[] = "{$key} ='".MysqLi_real_escape_string($value)."'";
}
$sql = "UPDATE {$table} SET ".implode(', ', $set)." WHERE ID = '$id'";
MysqLi_query($connection,$sql);
}
function update($table, $data, $id)
{
$setPart = array();
$bindings = array();
foreach ($data as $key => $value)
{
$setPart[] = "{$key} = :{$key}";
$bindings[":{$key}"] = $value;
}
$bindings[":id"] = $id;
$sql = "UPDATE {$table} SET ".implode(', ', $setPart)." WHERE ID = :id";
$stmt = $pdo->prepare($sql);
$stmt->execute($bindings);
}
这里$connection是一个MysqLi_connection对象,我们需要创建它来执行任何查询以了解这个click here.
在第二个函数$pdo是PDO连接对象,我们需要创建它来执行查询click here以获取更多信息.
有关更多信息,请参阅此link.
为了防止sql注入,你可以使用MysqLi_real_escape_string()函数
click here了解更多信息.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。