Welcome Guest, Not a member yet? Register   Sign In
Help updating one stubborn piece of info in a database
#1

[eluser]mindSmile[/eluser]
****Update: This question may have been simplified by my latest post,****

In an application I'm developing, there are pages where, when the user visits them, some information is processed and stored in the database with their userinfo.

One of the items to be updated is "last_book", where the id of a book being viewed on a page is stored each time any book is view, clearing out the last stored bookID. Sounds pretty typical, right?

Wrong! In firefox and opera, it just isn't updating [Edit: except when I log the variable using firephp, which makes things magically work]. In chrome and safari it works perfectly. In FF and Opera, I'll access the page, check the table in phpMyAdmin, and it just remains blank, every time except when the book id is 12, or i'm logging the variable with firephp. In those specific cases it'll update fine, but if I visit another book (not book 12) or comment out all of the firephp logging, it becomes blank again. I've tried echoing every variable along the way, and it looks like it *should* work. Also, I'm not entirely sure what type of field I should have for this piece of information in the database, as it's just going to be a number.

Books are stored in a table called "books", and members are stored in a table called "members". Both have a primary index auto-incrementing 'id' field.

Both the Books_model and Members_model are autoloaded.

Enough of my yapping, here's my code (trying not to bug you with unrelated parts):

Members_model
Code:
<?php

class Members_model extends Model{
    
    /****************************/
    /*    Account Functions   */
    /****************************/    
    
    function update_member($bookID, $langCode){
        $id = $bookID;
        
        $userdata = $this->get_user_data();
        $email = $this->session->userdata('email');
        $bookString = $userdata['books_accessed'];
        $langString = $userdata['langs_accessed'];
        $lastBook = $bookID; //This is the piece for the last book
        
        $accessedBook = $this->add_to_books_accessed($bookString, $id);
        $accessedLang = $this->add_to_langs_accessed($langString, $langCode);
        $data = array(
            'langs_accessed' => $accessedLang,
            'books_accessed' => $accessedBook,
'last_book' => $lastBook        
            );
        $this->db->where('email',$email);
        $this->db->update('members',$data);
        }


function get_user_data(){
        $useremail = $this->session->userdata('email');
        $this->db->select('id,email,langs_accessed,books_accessed,shelf,last_book,last_spot');
        $this->db->where('email',$useremail);
        $query = $this->db->get('members');
            $row = $query->row();
            $userdata = array(
                'id' => $row->id,
                'lang_nat' => $row->lang_nat,
                'langs_accessed' => $row->langs_accessed,
                'books_accessed' => $row->books_accessed,
                'shelf' => $row->shelf,
                'last_spot' => $row->last_spot
                );
        $query->free_result();
        return $userdata;
        }

function add_to_langs_accessed($langString, $lang){        
        if(isset($langString) && $langString != ''){            
            $cleaned = $this->clean_imploded_string($langString);
            $result = $cleaned['exploded'];
            $in_langs = array_keys($result,$lang);
            $matches = count($in_langs);
            if($matches != 0){
                $addLang = $cleaned['imploded'];        
            } else {
                $addLang = $cleaned['imploded'] .'~'. $lang;
                }
            } else {
                $addLang = $lang;
                }
                return $addLang;
            }

function add_to_books_accessed($bookString, $id){

        if(isset($bookString) && $bookString != ''){
            $cleaned = $this->clean_imploded_string($bookString);
            $result = $cleaned['exploded'];
            $in_books = array_keys($result,$id);
            $matches = count($in_books);            
            if($matches != 0){
                $addbook = $cleaned['imploded'];                
                } else {
                $addbook = $cleaned['imploded'] .'~'. $id;
                }
            }    else    {
                $addbook = $id;
                            
            return $addbook;
        }

}
#2

[eluser]mindSmile[/eluser]
Books_model
Code:
function getBook($lang,$title_slug){
    $lang = ucfirst(strtolower($lang));
    $langCode = $this->langToLangCode($lang);
    
    $this->db->where('prepped','yes');
    $this->db->where('lang',$langCode);
    $this->db->where('title_slug',$title_slug);
    $q = $this->db->get('books');
    if($q->num_rows() == 1){
        $row = $q->row();
        }
    return $row;
    }

Site Controller
Code:
function books(){
        $is_logged_in = $this->session->userdata('is_logged_in');
        $acceptEn = $this->agent->accept_lang('en');
            if($acceptEn){    //Set variable enOK for whether or not English is accepted by browser
                $data['enOk'] = TRUE;
                    } else {
                $data['enOk'] = FALSE;
                }
        $data['is_logged_in'] = $is_logged_in;
        
        $lang = $this->uri->segment(2);
        $lang = ucfirst(strtolower($lang));
        $langCode = $this->Books_model->langToLangCode($lang);
        $title_slug = $this->uri->segment(3);
        $pageType = $this->uri->segment(1);
        
        
        if(!isset($lang) || $lang == ''){
            redirect('browse');
            }    elseif(($title_slug == '' || !isset($title_slug)) && (isset($lang) || $lang != '')){
                redirect('browse/language/'.$lang);
            } else    {        
                $book = $this->Books_model->getBook($lang,$title_slug);
                $data['page'] = 'book';
                $data['main_content'] = 'books/book';            
                $data['book'] = $book;
                $bookid = $book->id;
                if($book->title_eng != ''){
                    $title = $book->title_eng;
                    $data['page_title'] = $title.' in '.$lang;
                }    else    {
                    $title = $book->title_nat;
                    $data['page_title'] = $title;
                    }
                if(isset($book->inc_charset)){
                    $data['charset'] = $book->inc_charset;
                } else {
                    $data['charset'] = 'UTF-8';
                    }
                $data['pageType'] = $pageType;
                $data['epubDir'] = $title_slug;
                $data['book'] = $book;
                $data['addLink'] = 'members/addToBookshelf/'.$bookid;
                $returnto = $pageType . '/' . $lang . '/' . $title_slug;
                $this->session->set_userdata('return_to', $returnto);
                
                if(isset($is_logged_in) && $is_logged_in == TRUE){
                    $this->Members_model->update_member($bookid, $langCode);
                    };

                $this->load->view('includes/template', $data);
            }
        }

Geez Louise, I hope that's enough info for you! I really appreciate any help, and let me know if anything up there ^ is unclear. Thanks!
#3

[eluser]mindSmile[/eluser]
*crickets*
#4

[eluser]mindSmile[/eluser]
In case anyone dare try to read this and see if they can think of some sort of explanation, I updated the original post with some new information I gathered while doing some more testing.
#5

[eluser]mindSmile[/eluser]
Another update, possibly simplifying the question.

I'm using firePHP, and the whole functionality works perfectly if, at any point in the process after the get_book function is called, I log the $bookid variable. This made me think that perhaps logging it had some effect on whether it was an integer or a string, so I tried playing with those ideas to no avail. Does anyone have any idea of why an update query would work for some variables, and not for another unless that variable is logged by firephp? I'd leave the $this->firephp->log($bookID); in but that doesn't sound like a good practice.




Theme © iAndrew 2016 - Forum software by © MyBB