0

Is it possible to retrieve values from database as an array and save the same values back to the database?

For example, In my table1 it returns an array:

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

I wanted to save test1 back to my database.

Codes:

View

  <?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;'));
?>

Controller

    $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);
}

Model

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


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

Table 2: foreign key

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

enter image description here

I want the test1 in the dropdown to be saved in my database, however when I click next, it results to

enter image description here

Instead of test1, it's returning 0.

14
  • You mean the VALUE within position 0? Here being "test1" Commented Mar 7, 2017 at 15:37
  • @clearshot66 yes sir. Commented Mar 7, 2017 at 15:43
  • mysqli_fetch_array: If you set up a database table with a column, just run a foreach($array as $value) then run an insert query to that column inserting $value Commented Mar 7, 2017 at 15:44
  • w3schools.com/php/func_mysqli_fetch_array.asp Commented Mar 7, 2017 at 15:45
  • @clearshot66 i did, still returning they key = 0. Commented Mar 7, 2017 at 15:45

3 Answers 3

0

serialize the array and store that in the dbase instead

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

Sign up to request clarification or add additional context in comments.

2 Comments

The serialize, it's saving the literal value
yes, I serialized and echoed, it's giving me this result "a:1:{i:0;s:5:"test1";}"
0
<?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.. */ } 
} 

?>

NOTE: If you want to get this data from database then use unserializ() , which help you to convert your string into array

Comments

0

Your foreach is overwriting the $data1 array on each pass, the controller should be like this:

   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);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.