当我没有';I don’我不知道将传递哪一个表单名称


How can I map form values to database column names when I don't know which of the form names will be passed?

我有一个HTML表单。我有一个MySQL表。

表单具有名称属性,如"last_name"、"first_name"answers"unique_id"等。

MySQL数据库表的列名为"last"、"first"answers"something_special_id"等。

我所做的是创建一个新数组,并将其从表单映射到表列名。然后我会发回新的数组,这样我就可以更新我的数据库了。

问题是我想让它充满活力。

我已经做到了:

if (isset($myArray['last_name']))
{
    //Gets the current value of the form array sent in POST
    //and puts that value into the new array which is labeled
    //with the correct table column name;
    //e.g. $myArray['last_name']= "Smith";
    //now $mappedColumnNames['LAST'] is equal to "Smith";
    $mappedColumnNames['LAST'] = $myArray['last_name'];
}
if (isset($myArray['first_name']))
{
    $mappedColumnNames['FIRST'] = $myArray['first_name'];
}
if (isset($myArray['diploma']))
{
    $mappedColumnNames['DIPLOMA'] = $myArray['diploma_value_from_form'];
}
if (isset($myArray['secret_number']))
{
    $mappedColumnNames['SECRET'] = $myArray['secret_number_from_form'];
}
unset($myFormValues);

$mappedColumnNames数组是数据库的列名。$myArray数组是表单发送的POST变量的数组

  • 我不知道每次表格出来后他们会点什么提交
  • 我不知道哪种形式的元素会一直存在提交
  • 我不希望表单属性的名称成为数据库的列名
  • 我不确定是否要将MySQL中的查询更改为使用别名。对于例如,使用LAST作为"LAST_name"
  • 我真的不想更改数据库模式,因为我希望这样"几乎"与数据库无关

有比我做的更好的方法吗?

这是我更新数据库的代码:

$mappedColumnNames = parent::mapColumnNames($_student_search);
        //What I use now but it won't work because the form names are not the column names.
        foreach($_student_search as $key => $value)
        {
            //echo '</br>key: ' . $key . ', value: ' . $value . '</br>';
            $columns[] = $key . ' = ?';
            $new_value[$key] =  $value;
        }
        //I don't need the text input button, which occurs first
        array_shift($columns);
        array_shift($new_value);
        $sql = 'UPDATE students ';
        $sql .= ' SET ' . implode(' AND ', $columns);
        $sql .= ' WHERE student_id = ' . $new_value['student_id'];
        $this->adapter->prepare($sql);
        $this->adapter->execute($new_value);

编辑:

我最终采纳了这些建议,并做到了:

public function getFormNameMapping()
    {
        $realColumnNames = parent::getColumnNames('students');
        $realKeyNames  = parent::getColumnNames('students');
        return array_combine($realColumnNames, $realKeyNames);
    }
    protected function getColumnNames($myTable)
    {
        $sql = "DESCRIBE " . $myTable; //mysql only
        $this->adapter->prepare($sql);
        $this->adapter->execute();
        $results = $this->adapter->fetchAll(PDO::FETCH_COLUMN);
        return $results;
    }

然后:

        $student_model = $this->model->getStudentModel();
        $student = $student_model->findByStudentID($studentID);
        $mappedColumnNames = $student_model->getFormNameMapping();
    <input name="<?PHP echo $mappedColumnNames['FIRST']; ?>" type="text" class="text" value="<?PHP echo $student->FIRST ?>"/>

谢谢。

可能是一个更好的方法,但对于问题:

$crossRef = array('FIRST'=>'first_name', 'LAST'=>'last_name'); //etc...
foreach($crossRef as $key => $val) {
    if(!empty($myArray[$val])) {
        $mappedColumnNames[$key] = $myArray[$val];
    }
}

我从数据库中提取列名,并使用列名创建表单。它与数据库无关,因为它根据数据库列动态创建表单。

相关文章: