3

Again I find myself at the mercy of the stackoverflow community!

I've gone over to use CodeIgniter for my PHP projects and it's been a breeze so far, hoever I've gotten stuck trying to update a database field with some post data.

My array is the usual: array(name => value, name => value, name => value); which again is populated from the submitted $_POST data.

Similarly to the array, I have a database table with 2 fields: setting and value, where the names under setting corresponds to the array keys and value to the array keys' value.

(Did I explain that right?)

Nonetheless, I've been trying for a little while now to get this to work as it should, but, I'm really just waving my hands around in the dark.

I hope some of you bright minds out there can help me with this annoying issue!

Edit:

Thanks to everyone who replied! I managed to produce the result that I wanted with the following code:

    foreach ($form_data as $key => $val)
    {
        $this->db->where ('setting', $key);
        $this->db->set ('value', $val);
        $this->db->update ('recruitment');
    }

Now, I tried following up with this by adding:

    if ($this->db->affected_rows() >= '1') { return true; }
    return false;

To my model, and

        if ($this->RecruitmentSettings->Update($form_data) == TRUE)
        {
            redirect('recruitment/success');
        }

to my controller, but it's not working as expected at all. Any ideas what I'm doing wrong?

0

2 Answers 2

2

There are a lot of questions here. Do you already have values in the database and you want to update them? Or do you want to put in new data every time? The answer depends on that.

What you want is the insert_batch() or update_batch() methods of the active record class (if that's what you're using for the db).

foreach($post_array as $key => $value)
{
    $settings[] = array(
        'setting' => $key,
        'value' => $value
    );
}

$this->db->insert_batch('your_db_table', $settings);

OR, for updates:

$this->db->update_batch('your_db_table', $settings, 'setting');

You could do a query to check for settings and do insert_batch or update_batch depending on if there are results. If you wanted to insert every time, you could delete the rows in the table before you do the insert. I wouldn't do that without a transaction, however.

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

4 Comments

I tried this method, but it produced the error "You must specify an index to match on for batch updates." which I really don't understand. Hmm.
See localhost/user_guide/database/active_record.html#update. update_batch takes a third param which is the 'where' clause... (the index to match on). In this case, set it to 'setting': $this->db->update_batch('your_db_table', $settings, 'setting');
That's it! It worked perfectly. Together with the affected_rows() code I added above, it's the perfect solution to my needs.
I just realized I copied the link from my local copy of the CI user guide! Oops! Correct link: codeigniter.com/user_guide/database/active_record.html#update
0

So you want to store the array data in the database? You could do this

Model

foreach ($data as $key => $item)
{
      $this->db->set ('setting', $key);
      $this->db->set ('value', $item);
      $this->db->insert ('table_name');
}
return ($this->db->affected_rows() > 0);

Controller

if ($this->RecruitmentSettings->Update($form_data))
{

 redirect('recruitment/success');
}
else
{
     echo "error";
}

3 Comments

This won't work because you're not using an object. You need to do foreach($data as $key => $value) and then use $key for setting and $value for value. See my answer. The arrow notation doesn't work for arrays AFAIK. You'd get the error "attempting to access property of a non-object" or something.
Actually, if you want it to work, you need to do sub arrays within the main array. I.e. array( array('name'=>name,'value'=>value), array('name'=>name,'value'=>value)) etc. with the way your code is written currently. But then you could just use foreach($array as $key => $value)
no you dont need to do sub-arrays. if you have a simple array and do a 'foreach' - php splits the simple array for each item. I just tried it to confirm - it works

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.