codeigniter:如何将数组的值放入特定的表列?

时间:2015-05-23 06:37:16

标签: php mysql sql codeigniter insert

我有一个像这样的数组

array(12) {
            [0]=> string(1) "3"
            [1]=> string(1) "3"
            [2]=> string(1) "5"
            [3]=> string(1) "3"
            [4]=> string(1) "4"
            [5]=> string(1) "3"
            [6]=> string(1) "3"
            [7]=> string(1) "3"
            [8]=> string(1) "2"
            [9]=> string(1) "3"
            [10]=> string(1) "2"
            [11]=> string(1) "3" 
        } 

数组是动态的,因此,可以随时更改数组值

我也有一个像这样的结构的mysql表

|id|bobot_c1|bobot_c2|bobot_c3|bobot_c4|bobot_c5|bobot_c6|bobot_c7|bobot_c8|bobot_c9|bobot_c10|bobot_c11|bobot_c12|
-------------------------------------------------------------------------------------------------------------------
|  |        |        |        |        |        |        |        |        |        |         |         |         | 
-------------------------------------------------------------------------------------------------------------------

id auto_increment

如何在数据库表中输入密钥数组:"3", "3", "5", "3"和其他数组?

所以对于值 index [0] ,我进入 bobot_c1 列,索引[1] ,进入 bobot_c2 < / strong>专栏和其他人。

谢谢你!

5 个答案:

答案 0 :(得分:1)

我假设您已经了解了如何传递数据的codeigniter的基础知识 从控制器到模型

以下是CodeIgniter

的用户指南
// construct an array with indexes like this
// the indexes pertains to the table columns 
// and the value will be the indexes value
$data = array(
    "bobot_c1" => "3",
    "bobot_c2" => "3",
    "bobot_c3" => "5",
    "bobot_c4" => "3",
    "bobot_c5" => "4",
    "bobot_c6" => "3",
    "bobot_c7" => "3",
    "bobot_c8" => "3",
    "bobot_c9" => "2",
    "bobot_c10" => "3",
    "bobot_c11" => "2",
    "bobot_c12" => "3"
);

// table name you want to insert data

$table = "table_name"

// use codeigniter active record to
// insert data
$this->db->insert($table,  $data);

答案 1 :(得分:1)

像这样构造sql:

   $sql = "INSERT INTO table (columns here) " .
            "VALUES (".implode($array,",").")";
  $this->db->query($sql);// no need to add id column in sql since it is auto increatented value

答案 2 :(得分:1)

创建另一个这样的数组:

$dataArray = (
       'bobot_c1' => $array[0],
       'bobot_c2' => $array[1],
       'bobot_c3' => $array[2],
       'bobot_c4' => $array[3],
        .
        .
        .
       );

同样添加更多列并将$ dataArray插入数据库。

答案 3 :(得分:1)

<强>第一

  • 您的 bobot_ columns 类型应为 double int else number

<强>第二

  • 因为您的数据是字符串,所以当插入
  • 时,您应该将字符串数据解析为数字

这是查询

INSERT INTO table_name (id, bobot_c1, bobot_c2, bobot_c3, bobot_c4, bobot_c5, bobot_c6, bobot_c7, bobot_c8, bobot_c9, bobot_c10, bobot_c11, bobot_c12)
VALUES (NULL, CONVERT(INT, $index[0]), CONVERT(INT, $index[1]), CONVERT(INT, $index[2]), CONVERT(INT, $index[3]), CONVERT(INT, $index[4]), CONVERT(INT, $index[5]), CONVERT(INT, $index[6]), CONVERT(INT, $index[7]), CONVERT(INT, $index[8]), CONVERT(INT, $index[9]), CONVERT(INT, $index[10]), CONVERT(INT, $index[11]));

这是转换 CodeIgniter

的查询的教程
/*
    class Myclass {
        var $title = 'My Title';
        var $content = 'My Content';
        var $date = 'My Date';
    }
*/

$object = new Myclass;

$this->db->insert('mytable', $object); 

// Produces: INSERT INTO mytable (title, content, date) VALUES ('My Title', 'My Content', 'My Date')

答案 4 :(得分:1)

试试这个:

$cols = 11;
 $sql = "INSERT INTO table_name ( ";

  for($i=0; $i<$cols; $i++){
      if($i!=0)
          $sql .=", bobot_c" . ($i+1);
      else
          $sql .="bobot_c" . ($i+1);
  }

$sql .=" ) VALUES( ";

for($i=0; $i<$cols; $i++){
    if($i!=0)
        $sql .=", '" .$array[$i] . "'";
    else
        $sql .="'" .$array[$i] . "'";
}
$sql .=" )";

$query = $this->db->query($sql);