Welcome Guest, Not a member yet? Register   Sign In
Completely Perplexed - multiple queries running , profiler only shows one
#1

[eluser]ggoforth[/eluser]
I'm totally perplexed with this one. I'm trying to implement a very simple captcha for a web form I'm designing. When the blog post page loads, it generates a captcha, and saves it in the database. For some reason, It's inserting two records (two different captcha's), but the CI profiler only shows one insert query being run on the page. Any ideas? Code is below:

My controller that initiates the captcha creation
Code:
public function index($blog_id = NULL,$add_comment = false){
            if(!is_null($blog_id)){
                //are we adding a comment?
                if($add_comment){
                    if($this->_validate_comments()){
                        //add the comments
                        $this->_add_comment($blog_id);
                    }else{
                        $view_data['validation_errors'] = $this->validation_errors;
                    }
                }else{
                    //we are not adding comments
                    $this->_captcha_code();
                }
                
                
                //view data
                $view_data['blog_object']         = $this->blog_model->get_blog_entry($blog_id);
                $view_data['comment_object']    = $this->comment_model->select_comments($blog_id);
                $view_data['captcha_code']         = $this->captcha_code;
                
                //load the article view
                $data['view'] = $this->load->view('article_view',$view_data,true);
                $data['page_title'] = $view_data['blog_object']->blog_title;
                
                //load the view
                $this->load->view('default_layout',$data);
            }else{
                redirect('home');
            }
            //enable the profiler
            $this->output->enable_profiler(TRUE);
        }

So if we are not adding a comment, a captcha is generated. The captcha function is below:

Code:
private function _captcha_code(){
            //generate the captcha code
            $code = md5(uniqid(rand(), true));
            $this->captcha_code = substr($code, 0, 7);
            
            //save the captcha
            $this->blog_model->save_captcha($this->captcha_code,$this->input->ip_address());
        }

And the blog_model save captcha function - THIS IS WHERE THE TWO INSERTS ARE HAPPENING:

Code:
public function save_captcha($captcha = NULL,$ip = NULL){
            //delete any existing captchas
            $this->db->where('ip_address',$ip);
            $this->db->delete('captcha');
            
            //save the captcha
            $arr = array('ip_address'=>$ip,'word'=>$captcha);
            $this->db->insert('captcha',$arr);
        }

There are no loops, and even the profiler only shows the one insert....yet I get two records each time the page loads....

Very bizarre.
#2

[eluser]darkhouse[/eluser]
in your model, have you tried echo'ing something to see if the method is being called twice?
#3

[eluser]ggoforth[/eluser]
I've tried just about everything, I tried $this->db->last_query() as well as turning on the profiling which tells you all the queries being run.
#4

[eluser]ggoforth[/eluser]
This gets more and more crazy....so I was thinking maybe it had something to do with using 1.7.0...so I switch back to use a previous version. The code below uses no models, loops, anything...just a one off query, and it creates two records in mysql. Could this be something with my server? I've restarted mysql and apache and the results persist. The controller below gives me two inserts every time.

Code:
<?

    class Test extends Controller{
    
        public function __construct(){
            parent::Controller();
        }
        
        public function index(){
            
            $sql = "INSERT INTO captcha (ip_address,word) VALUES (?,?)";
            $this->db->query($sql,array('192.168.1.1','testcaptcha'));

        }
    
    }

?>
#5

[eluser]sophistry[/eluser]
two ideas:

strip it way down to a new, completely fresh CI install and see what happens.

and/or

strip it even further to a raw PHP page and send it your sql query and see if it gives two db inserts - that would isolate the problem at least.
#6

[eluser]darkhouse[/eluser]
Do you have phpmyadmin installed or something else to manage your database? If so, try that sql statement in there and see if it produces 2 results. If it does, you know it's not CI, and it's gotta be an issue with mysql.
#7

[eluser]Derek Jones[/eluser]
Also check to make sure your browser isn't making that request twice. I've seen that happen with Firebug before.
#8

[eluser]ggoforth[/eluser]
[quote author="Derek Jones" date="1234323905"]Also check to make sure your browser isn't making that request twice. I've seen that happen with Firebug before.[/quote]

Hi Derek, you hit the nail on the head. It was a browser issue. I posted this here quite a while ago, so I appologize for not updating it. I had narrowed it down to a browser quirk, or one of my extensions. I've since removed the offending extension (though I'm not sure which one it was) and the problem stopped.

Thanks again for your input!

Greg
#9

[eluser]Derek Jones[/eluser]
Good to hear, Greg!




Theme © iAndrew 2016 - Forum software by © MyBB