使用PHP将可变数量的字段添加到数据库表

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了使用PHP将可变数量的字段添加到数据库表脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我似乎无法想出一个将可变数量的字段添加数据库表的正确方法.假设我正在构建一个Web应用程序,访问者可以在其中注册和管理他们的联系人.他们可以通过表单添加单个联系人,该表单包含以下文本输入字段:

>联系人姓名
>话号码
>电子邮件地址
>公司
>地址
>主页

在此表单中,只需要“联系人姓名”字段,因此完全可以在不知道其电话号码的情况下添加联系人的电子邮件地址.我怎样才能在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,-2);

    // Add values to sql statement
    $sql .= ') VALUES (';
    foreach ($_POST as $field => $value) {
        if (!empty($value)) {
            $sql .= $value . ',-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 .= ',';
            }

            // 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将可变数量的字段添加到数据库表所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。