CodeIgniter Forums
Update Database Code Problems - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Update Database Code Problems (/thread-19640.html)



Update Database Code Problems - El Forum - 06-13-2009

[eluser]Gwarrior[/eluser]
Hey guys and thanks in advance for taking a look!

I'm having trouble with this pretty simple script. I basically just want it to pull from a database, and populate a form (which it does fine), then have it where I can edit the data from that form and update the database upon submission.

The whole thing seems to work, with no errors anywhere, yet the database does NOT update when I submit the form.

Controller:
Code:
<?php
class Edit extends Controller {
    function Edit() {
        parent::Controller();
        $this->load->library('session');
        $this->load->database();
    }
        
    function banners() {
        if ($this->session->userdata('logged_in') == TRUE) {
            $data['logged_in'] = $this->session->userdata('logged_in');
            $query = $this->db->query('SELECT * FROM banners');
            $data['banner'] = '';
            if (@$_POST['banner_num'] == 'one') {
                $data['banner'] = $query->row(1);
                }
            if (@$_POST['banner_num'] == 'two') {
                $data['banner'] = $query->row(2);
                }
            
            if (isset($_POST['submitted_2'])) {
                    $id = $this->input->post('id');
                    $title = $this->input->post('title');
                    $desc = $this->input->post('desc');
                    $image = $this->input->post('image');
                    
                    $query_2 = $this->db->query('UPDATE banners SET title = \'$title\', description = \'$desc\', image = \'$image\' WHERE id = \'$id\'');
                    
                             }
            $this->load->view('banners', $data);
            } else {
            $this->load->view('login');        
            }
            
    }

}
?>

View:
Code:
<form action="" method="post">
<h3>Which Banner to Replace:</h3>
&lt;input type="radio" name="banner_num" value="one" /&gt;One <br />
&lt;input type="radio" name="banner_num" value="two" /&gt;Two <br />
&lt;input type="radio" name="banner_num" value="three" /&gt;Three <br />

&lt;input type="radio" name="banner_num" value="four" /&gt;Four <br />
&lt;input type="submit" name="submitted" /&gt;
&lt;/form&gt;

&lt;form action="" method="post"&gt;
<h3>Title:</h3>
&lt;input type="text" name="title" value="&lt;?php echo $banner-&gt;title; ?&gt;" />
<h3>Description:</h3>
&lt;textarea name="desc" style="width: 400px; height: 200px; overflow: scroll;"&gt;&lt;?php echo $banner->description; ?&gt;&lt;/textarea&gt;
<h3>Image URL:</h3>
&lt;input type="text" name="image" value="&lt;?php echo $banner-&gt;image; ?&gt;" />
<br />
&lt;input type="hidden" name="id" value="&lt;?php echo $banner-&gt;id; ?&gt;" />
&lt;input type="submit" name="submitted_2" /&gt;
&lt;/form&gt;

Thanks!


Update Database Code Problems - El Forum - 06-13-2009

[eluser]n0xie[/eluser]
[quote author="Gwarrior" date="1244960652"]Hey guys and thanks in advance for taking a look!
if (isset($_POST['submitted_2'])) {
echo 'hurray!';
}
[/quote]
1. Have you checked if the scrip actually gets to this point?
2. What values, if any, are inserted into the query string
3. Was the query executed and if so, what did it execute?
4. What does $_query2 return?

A little bit more information is needed...

Also, why don't you use a model for your database actions? And is there a reason you don't use AR? Wouldn't it be easier to do this?
Code:
$id = $this->input->post('id');
                    $data['title'] = $this->input->post('title');
                    $data['desc'] = $this->input->post('desc');
                    $data['image'] = $this->input->post('image');
                    
                    $this->db->where('id',$id);
                    $query_2 = $this->db->update('banners',$data);



Update Database Code Problems - El Forum - 06-13-2009

[eluser]Gwarrior[/eluser]
Well the data was making it into the script, but I changed the longhand to AR and it works beautifully.

Thanks for all your help!