I think this is the problem, if there's no need to display (a checkbox) is selected the link to this page isn't supposed to show in the menu. But see what happens: only SELECT * FROM ...?? Shouldn't this be Insert Into etc...?
if($no_need_to_display==0)
{
$query= "SELECT * FROM ".$prefix."_pages WHERE parentid = 0";
$pages = $db->getAll($query);
$smarty->assign('pages', $pages);
$smarty->display("addpage.tpl");