Welcome Guest, Not a member yet? Register   Sign In
Extra content being inserted using Active Records
#1

[eluser]dtrnr[/eluser]
Hi, I'm something of a new user to the whole CodeIgniter experience, but I've been using PHP for a while so I have a rough understanding of what's happening and I'm encountering a strange bug.

I'm trying my hand at creating a basic CMS so that I can add, edit and purge content. My problem lies with the inserting content into my table. My content is being added but, in addition to this, there is an additional row being inserted made up of the number 0 being inserted into each row. I have put together a fix to remove such entries after the fact, but I would rather resolve the problem than make use of a workaround.

I've done some searching around both here and elsewhere online, and I've not found anything useful, so I apologise if this is something that has been resolved before. Any help would be most appreciated.
#2

[eluser]WanWizard[/eluser]
Activate the profiler, so you can see exactly which queries are generated. This might give some insight into why this is happening.

Also, it would be handy if we could examine the code you use to insert new records, to avoid us having to guess...
#3

[eluser]dtrnr[/eluser]
Hi, thanks for the reply. The code I'm using looks like the following.

Form to Add Content
Code:
<?php
echo form_open('admin/create');
echo form_input('title', 'Title');
echo form_input('author', 'Author');
echo form_input('content', 'Content');
echo form_submit('submit', 'Create Post');
echo form_close();
?>

Function to get inputted data and pass to the model function that inserts it
Code:
function create(){
    $data = array(
        'title' => $this->input->post('title'),
        'author' => $this->input->post('author'),
        'content' => $this->input->post('content')
    );
    $this->site_model->add_record($data);
    $this->index();
}

Model Function to insert data into table
Code:
function add_record($data){
    print_r($data);
    $this->db->insert('content', $data);
    $this->db->delete('content', array('title' => '0')); //fix to purge extra row that is added
    return;
}

I've also activated the profiler, per your suggestion, and I've attached a screenshot of what it's produced. It seems that not even it is seeing the second row being inserted, but it's still happening. Any suggestions?
#4

[eluser]WanWizard[/eluser]
The AR code doesn't insert records twice, so something else must be causing this.

Activate logging by setting the log_threshold in config/config.php to 4.
Delete or empty any logfiles that are present, perform the action again, and check your CI logs. Anything unusual? Redirects or reloads of the page?

Another test: directly after the insert, add a 'die("stop here");' to make sure your script doesn't continue. Do you still have two records inserted?
#5

[eluser]dtrnr[/eluser]
I've done as you suggested, and I'm noticing that things seem to be getting loaded twice, at least that's what the attached file seems to suggest. Putting the die() command in also, as you'd expect, prevents the script from re-running and adding empty values. I'm not sure what could be causing that to occur though, so if you can see anything that might be causing it, or have any suspicions, I'm all ears.

Edit

Playing about a bit more with this, I've noticed where the problem is, though I've yet to figure out the why. I have, however, noticed two things:

1. It doesn't happen if I enter the url in manually, though submitting content still results in duplicats
2. The whole thing is meant to be passworded, which is checked with a __construct() function. When this is disabled, everything functions perfectly. When it is turned on, things start duplicating.

This leads me to believe that something I've done in the checking process is causing views to load twice. How? I'm not sure. The code checks for a session value that is created upon login. I'll add the code I use for checking in below, in case somebody else can see what I've done wrong:

__controller()
Code:
function __construct(){
    parent::Controller();
    $this->is_logged_in();
}

is_logged_in()
Code:
function is_logged_in(){
    $is_logged_in = $this->session->userdata('is_logged_in');
    if(!isset($is_logged_in) || $is_logged_in != true){    
        echo 'You do not have permission to access this page. <a href="../login/">Click here to log in</a>.';
        die();    
    }
}
#6

[eluser]WanWizard[/eluser]
I assume the insert is the result of a form POST? In which case you shouldn't be able to simulate it with manually typing the URL.

I think there is something seriously wrong in your logic. Where and when do you call the create() method?
#7

[eluser]dtrnr[/eluser]
Yep, form is passing the data using POST. Which is why I think the second load inserts 0 into all the rows. If I monitor resources for the pages loading, I see my CSS and JS files load (but they're doing nothing) a total of two times, and the site seems to as well. You can see a screenshot of it here. The content only shows on screen once, and viewing the source shows a single copy of my source code.

With regards to the create(), the form for handling blog posts is set to go straight to it when the user/I submit the form. So it's going straight to the function that processes it. It's just being processed twice, once with actual data and once without.

Having just typed all of this out, I've actually gone and played with it some more, and after seeing what I'm now seeing, I believe that the fault wasn't lying with the code I was using at all, as it's now perfectly fine. I got frustrated with the code earlier, and purged it so I could start from scratch. The current setup is identical in every way... except one. .htaccess rewriting isn't being used now and everything seems to be running smoothly.

After looking into it, it seems that this is a problem that others have encountered before, but there's never been a solution that works for everyone. I don't suppose you could provide some help/pointers on how to deal with them at all?
#8

[eluser]WanWizard[/eluser]
It's very difficult for me to do some debugging not having access the the environment.

But the most common error when it comes to rewriting is a URL request of something in the page that doesn't exist, p.e. an image. The rewrite engine would rewrite that to a call to index.php, causing a second page request.
#9

[eluser]dtrnr[/eluser]
That's weird then that I'm having it happen as a cause of that. I have no images included in any part of my code. I'll see what I can work out though. Thanks, once again, for your assistance.




Theme © iAndrew 2016 - Forum software by © MyBB