【问题标题】:How to store the values of array in mysql?如何将数组的值存储在mysql中?
【发布时间】:2017-03-07 15:36:35
【问题描述】:

是否可以从数据库中检索值作为数组并将相同的值保存回数据库?

例如,在我的 table1 中,它返回一个数组:

array(1) { 
   [0]=> string(5) "test1" 
}

我想将 test1 保存回我的数据库。

代码:

查看

  <?php //for viewing data
  foreach ($users as $user) {
     echo $user->empID;
     echo '<br>';
  }
   ?>
   <?php //for inserting data
  foreach ($users as $row) {
     $rec_users[] = $row->empID;
  }
       echo form_multiselect('empRequired[]', $rec_users, $rec_users,  array('class' => 'chosen-select', 'multiple style' => 'width:785px;'));
?>

控制器

    $lid = $this->admin_model->getID();
    foreach ($lid as $id) {
        $last_id = $id['projectID'];
        $data['users'] = $this->admin_model->getUsers($last_id);
    }

    $this->load->view('admin/projects/rec-employee', $data);

$recommended = $this->input->post('empRequired');

foreach ($recommended as $row) {
   $data1 = array(
            'projectID' => $last_id,
            'username' => $row
   );

   $this->admin_model->insertRecEmp($data1);
}

型号

    public function insertRecEmp($data){
        return $this->db->insert('projectemp', $data);
    }


Table 1: primary key
+--------+
|username|
+--------+
| test1  |
+--------+

Table 2: foreign key

+--------+----------+
|empID   |projectID |
+--------+----------+
| test1  |          |
+--------+----------+

我希望将下拉列表中的 test1 保存在我的数据库中,但是当我单击下一步时,结果为

返回 0 而不是 test1。

【问题讨论】:

  • 您的意思是位置 0 内的 VALUE?这里是“test1”
  • @clearshot66 是的先生。
  • mysqli_fetch_array: 如果你设置了一个带有列的数据库表,只需运行一个 foreach($array as $value) 然后对插入 $value 的列运行一个插入查询
  • @clearshot66 我做了,仍然返回他们的 key = 0。

标签: php mysql arrays database codeigniter


【解决方案1】:

序列化数组并将其存储在数据库中

http://us.php.net/manual/en/function.serialize.php

【讨论】:

  • 序列化,是保存字面值
  • 是的,我序列化并回显,它给了我这个结果 "a:1:{i:0;s:5:"test1";}"
【解决方案2】:
<?php 
// $session_data contains a multi-dimensional array with session
// information for the current user. We use serialize() to store 
// it in a database at the end of the request. 
$conn = odbc_connect("webdb", "php", "chicken"); 
$stmt = odbc_prepare($conn, "UPDATE sessions SET data = ? WHERE id = ?");
$sqldata = array (serialize($session_data), $_SERVER['PHP_AUTH_USER']); 
if (!odbc_execute($stmt, $sqldata)) { 
$stmt = odbc_prepare($conn, "INSERT INTO sessions (id, data) VALUES(?,?)"); 
if (!odbc_execute($stmt, $sqldata)) { 
/* Something went wrong.. */ } 
} 

?>

注意:如果您想从数据库中获取此数据,请使用 unserializ() ,它可以帮助您将字符串转换为数组

【讨论】:

    【解决方案3】:

    您的 foreach 在每次传递时都会覆盖 $data1 数组,控制器应该是这样的:

       foreach ($lid as $id) {
        $last_id = $id['projectID'];
        $data['users'] = $this->admin_model->getUsers($last_id);
    }
    
    $this->load->view('admin/projects/rec-employee', $data);
    
    $recommended = $this->input->post('empRequired');
    
    foreach ($recommended as $row) {
       //this line is the changed one
       $data1[] = array(
                'projectID' => $last_id,
                'username' => $row
       );
    
       $this->admin_model->insertRecEmp($data1);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-21
      • 1970-01-01
      • 2014-08-18
      • 2011-03-25
      • 2015-08-10
      • 1970-01-01
      • 2020-11-18
      相关资源
      最近更新 更多