Jump to content

Undefined variable


witchy478

Recommended Posts

Hi

 

I have an undefined variable called todo and I'm not sure how to fix it. I have looked at the tutorials and everything but I'm not sure on how to apply those to my problem.

 

Here is my itemscontroller.php

 

<?php

class ItemsController extends Controller {

function view($id = null,$name = null) {

	$this->set('title',$name.' - My Todo List App');
	$this->set('todo',$this->Item->select($id));

}

function viewall() {

	$this->set('title','All Items - My Todo List App');
	$this->set('todo',$this->Item->selectAll());


}

function add() {
	$todo = $_POST['todo'];
	$this->set('title','Success - My Todo List App');
	$this->set('todo',$this->Item->query('insert into items (item_name) values (\''.mysql_real_escape_string($todo).'\')'));	
}

function delete($id = null) {
	$this->set('title','Success - My Todo List App');
	$this->set('todo',$this->Item->query('delete from items where id = \''.mysql_real_escape_string($id).'\''));	
}

}

 

and this is my viewall.php where the error is showing up

<form action="../items/add" method="post">
<input type="text" value="I have to..." onclick="this.value=''" name="todo"> <input type="submit" value="add">
</form>
<br/><br/>
<?php $number = 0?>

<?php foreach ($todo as $todoitem):?>
<a class="big" href="../items/view/<?php echo $todoitem['Item']['id']?>/<?php echo strtolower(str_replace(" ","-",$todoitem['Item']['item_name']))?>">
<span class="item">
<?php echo ++$number?>
<?php echo $todoitem['Item']['item_name']?>
</span>
</a><br/>
<?php endforeach?>

Link to comment
https://forums.phpfreaks.com/topic/265044-undefined-variable/
Share on other sites

I'd start by making sure that $this->Item->selectAll() is returning rows by adding a log statement and checking the logs after the call.

 

This:

 

$this->set('todo',$this->Item->query('insert into items (item_name) values (\''.mysql_real_escape_string($todo).'\')'));

 

and this:

 

$this->set('todo',$this->Item->query('delete from items where id = \''.mysql_real_escape_string($id).'\''));

 

are definitely very wrong. You don't really want to set the view variable to the results of an INSERT or a DELETE query do you?. And I'm quite sure you're doing it wrong by using query() instead of the model's create()/save() and delete() methods.

Link to comment
https://forums.phpfreaks.com/topic/265044-undefined-variable/#findComment-1358198
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.