我似乎想不出一个合适的方法来向数据库表中添加可变数量的字段。假设我正在构建一个网络应用程序,访问者可以在其中注册和管理他们的联系人。他们可以通过具有以下文本输入字段的表单添加个人联系人:
- 联系人姓名
- 电话号码
- 电子邮件地址
- 公司
- 地址
- 首页
在此表单中,只有“联系人姓名”字段是必需的,因此完全可以在不知道电话号码的情况下添加联系人的电子邮件地址。我怎样才能在 PHP 中正确地实现它?
我以前用过的方法涉及遍历 $_POST 数组并检查每个特定字段是否已提交。如果一个字段已经提交,它的字段名将被添加到 SQL 语句中。然后我们对值执行相同的操作。
我的直觉告诉我,这种工作方式是非常错误的,但我想不出任何其他方式...
function generateSQLStatement($_POST) {
// Add field names to SQL statement
foreach ($_POST as $field => $value) {
if (!empty($value)) {
$sql .= $field . ', ';
}
}
// Remove last comma and space
$sql = substr($sql, 0, -2);
// Add values to SQL statement
$sql .= ') VALUES (';
foreach ($_POST as $field => $value) {
if (!empty($value)) {
$sql .= $value . ', ';
}
}
// Remove last comma and space
$sql = substr($sql, 0, -2);
$sql .= ')';
return $sql;
}
最佳答案
请注意,在下面的代码中,SANITIZE_ME
是 mysqli::real_escape_string
等方法的占位符。 ,或适合您情况的任何内容。您需要酌情调整此答案。
function generateSQLStatement($_POST) {
$fieldNames = "";
$values = "";
foreach ($_POST as $field => $value) {
if (!empty($value)) {
if (!empty($fieldNames)) {
$fieldNames .= ',';
$values .= ',';
}
$fieldNames .= SANITIZE_ME($field);
$values .= "'" . SANITIZE_ME($value) . "'";
}
}
return "($fieldNames) VALUES ($values)";
}
这种方法只使用一个循环,所以速度更快。但是您可能希望根据预定义的可接受字段数组验证您的字段名称,以防万一有人编辑了发布到您的脚本的表单并输入了无效的字段名称。
编辑
可以使用一种更通用的方法来创建一个实用函数,您可以在整个应用程序中轻松地将其与其他表一起重用:
这些可以放在一些通用的包含文件中:
// An array whose keys are valid table names and
// whose values are arrays of valid field names
// within the table named in the key
$acceptableFields = array(
'contacts' => array(
// valid fields in the 'contacts' table
'name', 'address' //...
)
// ... other mappings, if desired
);
function isValidField($table, $field) {
if (!isset($acceptableFields[$table]))
return false;
return in_array($field, $acceptableFields[$table]);
// Note that in_array is case-sensitive, so you may want
// to just manually loop through $acceptableFields[$table]
// and compare strings yourself.
}
function insertData($table, array $fieldValuesMap, mysqli $mysqli) {
// First, some self-explanatory validation:
if ($table === null)
throw new InvalidArgumentException('$table cannot be null');
if (!is_string($table))
throw new InvalidArgumentException('$table must be a String');
if (empty($table))
throw new InvalidArgumentException('$table cannot be an empty String');
if (!isset($acceptableFields[$table]))
throw new InvalidArgumentException("\"$table\" is an invalid table name");
$fieldNames = "";
$values = "";
foreach ($fieldValuesMap as $field => $value) {
// check the field name is valid for the given table
// and that the value is not empty. You may want to
// add a logging mechanism for invalid field names to
// help track bugs or even malicious use
if (isValidField($table, $field) && !empty($value)) {
// check to see whether there are any items in
// the lists already
if (!empty($fieldNames)) {
// yes, so add commas:
$fieldNames .= ',';
$values .= ',';
}
// no need to escape the field name as we have already
// checked that it is valid
$fieldNames .= $field;
// but we do need to escape the value
$values .= "'" . $mysqli->real_escape_string($value) . "'";
}
}
// check whether we've actually got anything to insert:
if (empty($fieldNames))
return NULL;
return $mysqli->query("INSERT INTO $table ($fieldNames) VALUES ($values)");
}
在页面上添加联系人的示例用法:
require_once "above.file"; // whatever it's called
if ($_POST) {
$mysqli = new MySQLi(/*...*/);
if (mysqli_connect_errno()) {
// handle connection error
} else {
// set your charset
$mysqli->set_charset("utf8"); // or whatever you use
$result = insertData('contacts', $_POST, $mysqli);
if ($result === NULL) {
// There was nothing to insert
} elseif ($result === FALSE) {
// An error occurred, handle it here
} else {
// Success! Use $mysqli->insert_id to get your new
// record's ID (if appropriate).
}
}
}
//
//==============================================
一些额外的工作,您最终会得到一些灵活且可重复使用的东西。不过,就我个人而言,我更喜欢更面向对象的(事件记录)方法。
关于php - 使用 PHP 向数据库表添加可变数量的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11557737/