Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否有其他选项可以使用CodeIgniter在数据库中插入数据?_Php_Codeigniter - Fatal编程技术网

Php 是否有其他选项可以使用CodeIgniter在数据库中插入数据?

Php 是否有其他选项可以使用CodeIgniter在数据库中插入数据?,php,codeigniter,Php,Codeigniter,我有一个包含30多个字段的单表单。我知道如何使用CodeIgniter提交数据库中的数据。我只是想知道,有没有其他选择,提交数据,而不写整个输入邮政编码。我的意思是检查下面的代码。它看起来很大 $reg_dat = array( 'surname' => $this->input->post('surname'), 'name' => $this->input->post('firstname'), 'age' => $this-

我有一个包含30多个字段的单表单。我知道如何使用CodeIgniter提交数据库中的数据。我只是想知道,有没有其他选择,提交数据,而不写整个输入邮政编码。我的意思是检查下面的代码。它看起来很大

$reg_dat = array(
    'surname' => $this->input->post('surname'),
    'name' => $this->input->post('firstname'),
    'age' => $this->input->post('age'),
    'school' =>$this->input->post('school'),
    'course' =>$this->input->post('course'),
    'email' => $this->input->post('email')

    'a' => $this->input->post('a'),
    'b' => $this->input->post('b'),
    'c' => $this->input->post('c'),
    'd' =>$this->input->post('d'),
    'e' =>$this->input->post('e'),
    'f' => $this->input->post('f')

    'g' => $this->input->post('g'),
    'h' => $this->input->post('h'),
    'i' => $this->input->post('i'),
    'j' =>$this->input->post('j'),
    'k' =>$this->input->post('k'),
    'l' => $this->input->post('l')

     and so on..
);

在小代码中是否有其他选项来生成上述代码?

如果您的POST字段名称和MySQL列名称完全相同,那么只需执行此操作即可

$reg_dat = $_POST
并将该数组输入数据库

$all_data = $_POST;
上面提到的行将生成一个数组,其中的键与您在问题中提到的列名相同,因此不需要在帖子中显示这么多行

然后在DB中插入所有数据,我们在Codeigniter中的语法是:

$this->db->insert('table_name',$all_data);  // Write this in your model.

因此,在
table\u name
中,所有数据都将被插入。

虽然使用所有
$\u POST
元素可以很快,但最好将要插入的元素列为白名单

$whitelist = array('a', 'b', 'c', 'd' ...);

$reg_dat = array();

foreach($key => $value in $whitelist){
    $reg_dat[$value] = $this->input->post($value);
}
在安全性方面,请确保在插入之前使用XSS过滤器和您可能希望对这些元素执行的任何其他验证。另外,确保您的数据库驱动程序是mysqli、pdo或其他比mysql更安全的驱动程序

根据下面关于
$\u POST
键未与数据库列对齐的评论,您可以执行以下操作

$whitelist = array();
$whitelist['a'] = 'database_column_for_a';
$whitelist['b'] = 'database_column_for_b';

$reg_dat = array();

foreach($postKey => $databaseColumn in $whitelist){
    $reg_dat[$databaseColumn] = $this->input->post($postKey);
}
简单地做:

$reg_dat = $this->input->post();
就这些。您将获得
$reg_dat
数组,该数组包含所有post输入字段,您可以直接将其插入Codeigniter中

注意:对于使用,这将直接确保输入名称和列名必须相同

对于
获取

$this->input->get();

当然@表单字段名称和表列名称必须相同吗?这不是必须的,但如果它们不相同,那么您就不能采用我们在答案中建议的方法。实际上,要传递给插入Codeigniter代码的数组应该具有与列名相同的键。所以,如果表单元素与列同名,这将很有帮助。你能在这个@Hybreeder中帮助我吗,我只谈论这个。我的意思是,既然我的答案解决了你的问题,那么你能接受吗?现在我很困惑,我得到了两个不同的答案,这是现在最好的@Himanshu或Keith?可能是@ankitsuthar的重复,如果问题id重复,那么你为什么在这里更新你的答案。你能在这个链接中帮助我吗
$this->db->insert('table_name',$reg_dat);
$this->input->get();