我正在尝试做这样的事情,这样我就不必输入所有的帖子条目。但我似乎无法让它发挥作用。
编辑:添加了一些更改。
foreach($_POST as $key => $value)
{
$key = "'".mysql_real_escape_string($key)."'";
$value = "'".mysql_real_escape_string($value)."'";
$qstring = "UPDATE load_test SET ".$key."=".$value." WHERE Id = '".$_POST['id']."'";
mysql_query($qstring);
}
最佳答案
您在这里尝试做的事情是令人难以置信的、危险的不安全。
// List the fields that may be updated here
$expectedFields = array('fielda', 'fieldb');
// Updated values to be stored here
$updates = array();
// Generate the update strings
foreach ($_POST as $key => $value) {
if (in_array($key, $expectedFields)) {
$updates[] = "`$key` = '".mysql_real_escape_string($key)."'";
}
}
// Do all updates at once
$qstring = "UPDATE load_test SET " . join(', ', $updates) . " WHERE Id = '" . mysql_real_escape_string($_POST['id']) . "'";
mysql_query($qstring);
这改进了一些事情
- 所有更新都发生在一个查询中,而不是每个字段一次
- 这些字段经过验证(并经过清理,因为只有在有效列表中才会接受它们)
- ID 值也已清理
关于php - 如何处理大型 php mysql $_POST UPDATE/INSERT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4362847/