Welcome Guest, Not a member yet? Register   Sign In
Inserts Extra Blank Record Into Database
#1

[eluser]al042077[/eluser]
I just started learning CI and have a problem. I've searched the web and forums, but found nothing similar. I even read through the manual. If I missed something, please redirect me and accept my sincerest apologies.
I have a simple controller, model, and views for a Quotes database table. When I add a quote, it inserts, shows the insert ID, provides a redirect link, and even reports the SQL of the last query. But when I look at the database table (via phpMyAdmin), there is an extra record added after my entry that has values of 0 (zero) in each field.
I tried profiling the code. I added two lines to the controller to monitor the flow:
Code:
echo 'Stopping';
exit;
I added the lines to the beginning of the add_quote_submitted() function in the controller. I reloaded the page in my browser, inserted a record, and it worked fine - only the record I wanted and no the extra.
I stepped these two lines down through the controller function and reviewed the database after each reload. I even got to where they were the last lines in the controller function - after all the load->view lines. The database showed only the one record. But, when I delete the debug lines, I get the extra DB entry again. So, it seems that there is something about when the controller function closes that it tries to insert a blank record.
Here is the code.
quotes.php (controller)
Code:
<?php
class Quotes extends Controller {
    function Quotes()
    {
        parent::Controller();
    }
    function add_quote()
    {
        $this->load->helper('form');
        $data_header = array(
                'title' => 'ALn PIM: Quotes'
            );
        $this->load->view('elements/header', $data_header);
        $this->load->view('quotes/add_quote');
        $this->load->view('elements/footer');
    }
    function add_quote_submitted()
    // Handle form submitted by add_quote function
    {
        $this->load->model('Quotes_model');
        $this->Quotes_model->create();
        $flash_message = array(
                'msg' => 'Added Quote #' . $this->db->insert_id(),
                'redirect' => anchor('quotes', 'Quotes')
            );
        $data_header = array(
                'title' => 'ALn PIM: Quotes'
            );
        $this->load->view('elements/header', $data_header);
        $this->load->view('elements/flash_message', $flash_message);
        $this->load->view('elements/footer');
    }
}
?>
quotes_model.php (model)
Code:
<?php
class Quotes_model extends Model {
    var $id;
    var $quote;
    var $source;
    var $tags;
    function Quotes_model()
    {
        // Call the Model constructor
        parent::Model();
    }  
    function create()
    {
        $data = array(
            'quote' => $this->input->post('quote'),
            'source' => $this->input->post('source'),
            'tags' => $this->input->post('tags')
        );
        $this->db->insert('quotes', $data);
        return 1;
    }
}
?>
add_quote.php (view)
Code:
<h1>ALn PIM: Add Quote</h1>
&lt;?php echo form_open('quotes/add_quote_submitted'); ?&gt;
    <table>
        <tr>
            <td class="form"><label for="quote">Quote</label>
            <td class="form">&lt;textarea name="quote" id="quote" cols="60" rows="20" &gt;&lt;/textarea>
        </tr>
        <tr>    
            <td class="form"><label for="source">Source</label>
            <td class="form">&lt;input type="text" name="source" id="source" maxlength="255" size="50" /&gt;
        </tr>
        <tr>    
            <td class="form"><label for="tags">Tags</label>
            <td class="form">&lt;input type="text" name="tags" id="tags" maxlength="255" size="50" /&gt;
        </tr>
        <tr>
            <td class="form">&lt;input type="submit" name="submit_add_quote" value="Submit" /&gt;&lt;/td>
        </tr>
    </table>
&lt;/form&gt;
flash_message.php (view)
Code:
&lt;?php
echo '<p><strong>', $msg, '</strong></p>';
echo $redirect;
?&gt;
Database Results
Code:
id     quote     source     tags
92     Testing test     test
93     0     0     0
#2

[eluser]ggoforth[/eluser]
I'm having the exact same problem, only my records aren't blank. I'm just getting two records inserted when I should only get one. No idea why. My thread is here:

http://ellislab.com/forums/viewthread/101066/




Theme © iAndrew 2016 - Forum software by © MyBB