Re: edit function is not working

2009-01-18 Thread Webweave

OK, I've responded about this exact code before. Just rewrite the
function with Cake conventions, and remove the direct MySQL updates
and you should be fine.

Your code is updating the record after the save, when you could simply
do the save with the query value from the count query, which should
look like:

$max_counter = $this->Entry->find('list', array('fields' => array('(max
(Entries.counter)) as max_counter')' )));

Take the value from $max_counter, and update your $this->data['Entry']
['counter'], then do your save and Cake will do the update according
to the data that is returned from the form.

I also would move the finds inside your check for empty($this->data),
since you don't need to repeat the find after the form is posted.

On Jan 16, 8:37 am, mona  wrote:
> Here is my edit function code i don't know where i mistaken if i edit
> my record it added automatically one new record and i didn't get
> validation messages also below is my edit function code
> function edit($id = null){
>         $this->set('sections', $this->Entry->Section->find
> ('list',array
> ('fields'=>'Section.section','Section.id','recursive' => 1,'page' =>
> 1,)));
>         if (empty($this->data)){
>         if (!$id){
>         $this->Session->setFlash('Invalid id for Entry');
>         $this->redirect('/entries/index');
>         }
>         $this->data = $this->Entry->read(null, $id);
>         }
>         else{
>     $query=mysql_query("select max(counter) from entries");
>    $row=mysql_fetch_array($query);
>         $co=$row[0]+1;
>     $q=mysql_query("update entries set counter=$co where id=$id");
>         if ($this->Entry->save($this->data)){
>         $this->Session->setFlash('The Entry has been saved');
>         $this->redirect('/entries/index');
>         }
>         else{
>         $this->Session->setFlash('Please correct errors below.');
>         }
>         }
>         }
>
> can anybody tell me where i m mistaken this function doesn't edit my
> record instead of that it will added one new record in my table
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: edit function is not working

2009-01-18 Thread AD7six



On Jan 16, 5:37 pm, mona  wrote:

>         if (empty($this->data)){
>         if (!$id){
>         $this->Session->setFlash('Invalid id for Entry');

What is that supposed to do? You won't find that sort of check in the
blog tutorial or baked code.

>         $this->redirect('/entries/index');
>         }
>         $this->data = $this->Entry->read(null, $id);
>         }
>         else{
>     $query=mysql_query("select max(counter) from entries");
>    $row=mysql_fetch_array($query);
>         $co=$row[0]+1;
>     $q=mysql_query("update entries set counter=$co where id=$id");

why is your code using mysql_query at all, and in a controller to
boot?

AD
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



Re: edit function is not working

2009-01-18 Thread RichardAtHome

Are you including the Model.id field in your edit form view?

create("Model"); ?>
input("Model.id"); ?> <-- important line

end("Submit"); ?>

On Jan 16, 4:37 pm, mona  wrote:
> Here is my edit function code i don't know where i mistaken if i edit
> my record it added automatically one new record and i didn't get
> validation messages also below is my edit function code
> function edit($id = null){
>         $this->set('sections', $this->Entry->Section->find
> ('list',array
> ('fields'=>'Section.section','Section.id','recursive' => 1,'page' =>
> 1,)));
>         if (empty($this->data)){
>         if (!$id){
>         $this->Session->setFlash('Invalid id for Entry');
>         $this->redirect('/entries/index');
>         }
>         $this->data = $this->Entry->read(null, $id);
>         }
>         else{
>     $query=mysql_query("select max(counter) from entries");
>    $row=mysql_fetch_array($query);
>         $co=$row[0]+1;
>     $q=mysql_query("update entries set counter=$co where id=$id");
>         if ($this->Entry->save($this->data)){
>         $this->Session->setFlash('The Entry has been saved');
>         $this->redirect('/entries/index');
>         }
>         else{
>         $this->Session->setFlash('Please correct errors below.');
>         }
>         }
>         }
>
> can anybody tell me where i m mistaken this function doesn't edit my
> record instead of that it will added one new record in my table
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---



edit function is not working

2009-01-16 Thread mona

Here is my edit function code i don't know where i mistaken if i edit
my record it added automatically one new record and i didn't get
validation messages also below is my edit function code
function edit($id = null){
$this->set('sections', $this->Entry->Section->find
('list',array
('fields'=>'Section.section','Section.id','recursive' => 1,'page' =>
1,)));
if (empty($this->data)){
if (!$id){
$this->Session->setFlash('Invalid id for Entry');
$this->redirect('/entries/index');
}
$this->data = $this->Entry->read(null, $id);
}
else{
$query=mysql_query("select max(counter) from entries");
   $row=mysql_fetch_array($query);
$co=$row[0]+1;
$q=mysql_query("update entries set counter=$co where id=$id");
if ($this->Entry->save($this->data)){
$this->Session->setFlash('The Entry has been saved');
$this->redirect('/entries/index');
}
else{
$this->Session->setFlash('Please correct errors below.');
}
}
}

can anybody tell me where i m mistaken this function doesn't edit my
record instead of that it will added one new record in my table
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to 
cake-php+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~--~~~~--~~--~--~---