Welcome Guest, Not a member yet? Register   Sign In
Database not being updated
#1

[eluser]GamingFusion[/eluser]
Im trying to get my app to update data in the database. When i submit the changes it says successful and lists the changes made. But when i go to the database it hasnt changed.

Whats Wrong?

heres all my code to do with this part of the app.

model
Code:
//Show Update Function
    function updateShow($id)
    {
        $data = array('title' => $this->input->post('title'), 'desc' => $this->input->post('desc'), 'showtimes' => $this->input->post('times'), 'cast' => $this->input->post('cast'), 'director' => $this->input->post('director'));

        $this->db->where('id', $id);
        $query = $this->db->update('shows', $data);
        
        if ($query) {
            return $data['updated'] = TRUE;
            return $query;
        }else{
            return $data['updated'] = FALSE;
        }
    }

View - updateShow.php
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;?php
    
    if ($updated = TRUE) {
        $title = $this->input->post('title');
    $desc = $this->input->post('desc');
    $times = $this->input->post('times');
    $cast = $this->input->post('cast');
    $direct = $this->input->post('director');
    echo '<h1>Success!</h1>
          <h3>The Show has been edited!</h3>';
    echo '<b>Show Title: </b><br />', $title, '<br /><br />';
    echo '<b>Show Description: </b><br />', $this->typography->nl2br_except_pre($desc), '<br /><br />';
    echo '<b>Show Times: </b><br />', $this->typography->nl2br_except_pre($times), '<br /><br />';
    echo '<b>Cast: </b><br />', $this->typography->nl2br_except_pre($cast), '<br /><br />';
    echo '<b>Director: </b><br />', $direct, '<br />';;
    }else{
        echo '<h1>Failure!</h1>
        <h3>The Show was not been updated.</h3>';
    }
?&gt;
&lt;/body&gt;
&lt;/html&gt;

view - editShow.php

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
&lt;html &gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /&gt;
&lt;title&gt;&lt;?=$title?&gt;&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
  
&lt;?=form_open('theater/updateShow');?&gt;

<table>
    <tr>
        <td>Show Title: </td><td>&lt;input name="title" class="input" type="text" value="&lt;?=$query-&gt;title?&gt;" /></td>
    </tr>
    <tr>
        <td>Show Description: </td><td>&lt;textarea name="desc" class="input" cols="30" rows="5"&gt;&lt;?=$query->desc?&gt;&lt;/textarea&gt;&lt;/td>
    </tr>
    <tr>
        <td>Show Times: </td><td>&lt;textarea name="times" class="input" cols="30" rows="5"&gt;&lt;?=$query->showtimes?&gt;&lt;/textarea&gt;&lt;/td>
    </tr>
    <tr>
        <td>Cast: </td><td>&lt;textarea name="cast" class="input" cols="30" rows="5"&gt;&lt;?=$query->cast?&gt;&lt;/textarea&gt;&lt;/td>
    </tr>
    <tr>
        <td>Director: </td><td>&lt;input name="director" class="input" type="text" value="&lt;?=$query-&gt;director?&gt;" /></td>
    </tr>
    <tr>
        <td>&lt;?=form_submit('submit', 'Update Show');?&gt;</td><td>&lt;?=form_reset('reset', 'reset');?&gt;</td>
    </tr>
</table>

&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
#2

[eluser]jedd[/eluser]
No idea - don't use the Active Record queries myself - but you should start by turning on the [url="http://ellislab.com/codeigniter/user-guide/general/profiling.html"]Profiler[/url] and seeing what your actual queries to the database are being translated into.

As an aside, this code:

Quote:
Code:
if ($query) {
    return $data['updated'] = TRUE;
    return $query;
    }
else ...

.. is quite broken - the second return will never be reached, and the first one will return TRUE regardless of the value of of $data['updated']. In other words, it's equivalent to:

Code:
if ($query)
    return TRUE
else ...
#3

[eluser]GamingFusion[/eluser]
any other suggestions people

i have been playing with the model a bit and heres the completely up to date

Code:
//Show Update Function
    function updateShow()
    {
        $data = array('title' => $this->input->post('title'), 'desc' => $this->input->post('desc'), 'showtimes' => $this->input->post('times'), 'cast' => $this->input->post('cast'), 'director' => $this->input->post('director'));
        
        $this->db->set($data);

        $this->db->where('id', $this->uri->segment(3));
        $query = $this->db->update('shows');
        
        if ($query) {
            return $data['updated'] = TRUE;
            return $query;
        }else{
            return $data['updated'] = FALSE;
        }
    }
#4

[eluser]n0xie[/eluser]
Output what the query is that is being generated. Then try that query in whatever Database client you are using:

Code:
$query = $this->db->update('shows');
echo $this->db->last_query();
exit();
#5

[eluser]GamingFusion[/eluser]
ok i really dont know whats wrong here, i even tried a properly formatted UPDATE MYSQL statement and it didnt work here is my statement.

Code:
$title = $this->input->post('title');
        $desc = $this->input->post('desc');
        $times = $this->input->post('times');
        $cast = $this->input->post('cast');
        $director = $this->input->post('director');
        $id = $this->uri->segment(3);

        $query = mysql_query("UPDATE shows SET title = '$title', desc = '$desc', showtimes = '$times', cast = '$cast', director = '$director' WHERE id = '$id'");

and it did say it updated it

and heres a statement that PHPMyAdmin Gave me.
Code:
UPDATE `theater`.`shows` SET `title` = 'Sabers' WHERE `shows`.`id` =2 LIMIT 1 ;
#6

[eluser]Derek Jones[/eluser]
Are you sure you're connecting to the right database?
#7

[eluser]GamingFusion[/eluser]
yes because i can insert and delete from the database
#8

[eluser]jedd[/eluser]
Well, two people have already suggested you examine the query that is actually being sent to the DB .. is there a reason you don't want to do this?
#9

[eluser]GamingFusion[/eluser]
i found the problem its not getting the $id value it says in the query WHERE id = 0
#10

[eluser]GamingFusion[/eluser]
ok i fixed it i forgot to tack on $query->id to the form so that the id could equal the third segement




Theme © iAndrew 2016 - Forum software by © MyBB