jmlab Posted January 13, 2015 Share Posted January 13, 2015 (edited) Hello my good people I'm doing a content manager system in PHP and mySQLi, following a serie of video tutorials. But in my project I am using a tree menu. In the front office all works smoothly (the tree menu displays all results - menu and submenus - and each button carries the information of the respective page). The problem is in the back office. The menu is there, in the url appears its id page but the CRUD, I can not put it to work. ======= CODE START ======= <!-- Gestor de Conteúdos Start --> <div class="container-fluid"> <!-- Conteúdo Fluido Start --> <div class="row-fluid row-offcanvas row-offcanvas-left"> <?php // INSERT QUERY if(isset($_POST['enviado']) == 1) { $header = mysqli_real_escape_string($dbc, $_POST['header']); $url = mysqli_real_escape_string($dbc, $_POST['url']); $user = $_POST['user']; $idPai = $_POST['idPai']; $menuNomePT = mysqli_real_escape_string($dbc, $_POST['menuNomePT']); $conteudo_pagina_PT = mysqli_real_escape_string($dbc, $_POST['conteudo_pagina_PT']); $menuNomeEN = mysqli_real_escape_string($dbc, $_POST['menuNomeEN']); $conteudo_pagina_EN = mysqli_real_escape_string($dbc, $_POST['conteudo_pagina_EN']); $q = "INSERT INTO menuCAL (header, url, user, idPai, menuNomePT, conteudo_pagina_PT, menuNomeEN, conteudo_pagina_EN) VALUES ('$header', '$_POST[url]', $_POST[user], '$_POST[idPai], '$menuNomePT, '$conteudo_pagina_PT', '$menuNomeEN', '$conteudo_pagina_EN'"; $r = mysqli_query($dbc, $q); ?> <div> <?php if($r) { $message = '<p>A página foi adicionada!</p>'; } else { $message = '<p>A página não foi adicionada, devido ao seguinte erro: '.mysqli_error($dbc); $message .= '<p>' .$q.'</p>'; } ?> </div> <?php } ?> <!-- Menu CAL Start --> <div class="col-sm-3 sidebar-offcanvas"> <?php //call the recursive function to print category listing category_tree(0); //Recursive php function function category_tree($menuPai){ global $dbc; $q = "SELECT * FROM menuCAL WHERE idPai ='".$menuPai."'"; $r = mysqli_query($dbc, $q); while($btnMenu = mysqli_fetch_assoc($r)): $i = 0; if ($i == 0) echo '<ul class="menuCAL">'; echo '<li><a href="?page='.$btnMenu['id'],'">' . $btnMenu['GlyPrincipal'] . $btnMenu['GlySecundario'] . $btnMenu['menuNomePT'], '</a>'; category_tree($btnMenu['id']); echo '</li>'; $i++; if ($i > 0) echo '</ul>'; endwhile; } ?> </div> <!-- Menu CAL End --> <!-- Conteúdo Start --> <div class="span10"> <div class="col-sm-12"> <!-- Título Start --> <h1 class="page-header"> <i class="fa fa-file"></i> Adicionar Página </h1> <ol class="breadcrumb"> <li> <a href="#"><i class="fa fa-pencil"></i> Conteúdos</a> </li> <li class="active"> <i class="fa fa-file"></i> Nova página </li> </ol> <!-- Título End --> </div> <!-- Textos & Formulários Start --> <div class="row"> <div class="col-lg-12"> <p><?php if(isset($message)) { echo $message; } ?></p> <?php // SELECT QUERY if(isset($_GET['id'])) { $q = "SELECT * FROM menuCAL WHERE id = $_GET[id]"; $r = mysqli_query($dbc, $q); $opened = mysqli_fetch_assoc($r); } ?> <!-- Formulário Start --> <form action="adicionar_pagina.php" method="post" role="form"> <!-- Campo header Start --> <div class="form-group"> <label for="header">Header:</label> <input type="text" class="form-control" name="header" id="header" value="<?php echo $opened['header']; ?>" placeholder="Texto descritivo a colocar no topo do website"> </div> <!-- Campo header End --> <!-- Campo Label Start --> <div class="form-group"> <label for="url">URL:</label> <input type="text" class="form-control" name="url" id="url" value="<?php echo $opened['url']; ?>" placeholder="Texto a colocar na URL (SEO)"> </div> <!-- Campo Label End --> <!-- Campo User Start --> <div class="form-group"> <label for="user">Administrador:</label> <select class="form-control" name="user" id="user"> <option value="0">›› Nenhum administrador</option> <?php $q = "SELECT id FROM users ORDER BY first ASC"; $r = mysqli_query ($dbc, $q); while ($user_list = mysqli_fetch_assoc($r)) { $user_data = data_user($dbc, $user_list['id']); ?> <option value="<?php echo $user_data['id'] ?>" <?php if($user_data['id'] == $opened['id']) { echo 'selected';} ?>><?php echo $user_data['fullname']; ?></option> <?php } ?> </select> </div> <!-- Campo User Start --> <!-- Campo ID Menu Pai Start --> <div class="form-group"> <label for="idPai">Adicionar a:</label> <select class="form-control" name="idPai" id="idPai"> <option value="0">›› Seleccione onde adicionar a nova página:</option> <?php $q = "SELECT menuNomePT FROM menuCAL WHERE idPai = 0"; $r = mysqli_query ($dbc, $q); while ($submenus = mysqli_fetch_assoc($r)) { ?> <option value="<?php echo $submenus['idPai']; ?>"><?php echo $submenus['menuNomePT']; ?></option> <?php } ?> </select> </div> <!-- Campo ID Menu Pai Start --> <!-- Campo menuNomePT Start --> <div class="form-group"> <label for="menuNomePT">Título PT:</label> <input type="text" class="form-control" name="menuNomePT" id="menuNomePT" value="<?php echo $opened['menuNomePT']; ?>" placeholder="Insira o título em Português"> </div> <!-- Campo menuNomePT End --> <!-- Campo conteudo_pagina_PT Start --> <div class="form-group"> <label for="conteudo_pagina_EN">Conteúdos PT:</label> <textarea class="form-control" name="conteudo_pagina_PT" rows="12" id="conteudo_pagina_PT" placeholder="Insira os textos em Português"><?php echo $opened['conteudo_pagina_PT']; ?></textarea> </div> <!-- Campo conteudo_pagina_PT End --> <!-- Campo menuNomeEN Start --> <div class="form-group"> <label for="menuNomeEN">Título EN:</label> <input type="text" class="form-control" name="menuNomeEN" id="menuNomeEN" value="<?php echo $opened['menuNomePT']; ?>" placeholder="Insira o título em Inglês"> </div> <!-- Campo menuNomeEN End --> <!-- Campo conteudo_pagina_EN Start --> <div class="form-group"> <label for="conteudo_pagina_EN">Conteúdos EN:</label> <textarea class="form-control" name="conteudo_pagina_EN" rows="12" id="conteudo_pagina_EN" placeholder="Insira os textos em Inglês"><?php echo $opened['conteudo_pagina_PT']; ?></textarea> </div> <!-- Campo conteudo_pagina_EN End --> <button type="submit" class="btn btn-default adic_concluir">Gravar</button> <input type="hidden" name="enviado" value="1"> </div> </form> <!-- Formulário End --> </div> <!-- Debug Panel Start --> <?php if($debug == 1) { include('widgets/debug.php'); } ?> <!-- Debug Panel End --> </div> <!-- Textos & Formulários End --> </div> <!-- Conteúdo End --> </div> <!-- Conteúdo Fluido Start --> </div> <!-- Gestor de Conteúdos End --> ======= CODE END ======= I can not do the INSERT or UPDATE query, using the form so that the "user" and the "idPai" (a category id), can be created or changed in the database. I tried to echo the database result, but nothing append And in the second query (in red in the code), gives me an error (Undefined variable: opened in …). Obviously something is wrong, but i can't see what! :/ The database: Can Someone help me? Please !! Thank U Edited January 13, 2015 by jmlab Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/ Share on other sites More sharing options...
QuickOldCar Posted January 13, 2015 Share Posted January 13, 2015 (edited) Your query is failing because of quotes missing You need to concatenate those like below or wrap them with curly braces. You omitted quotes for the key variables and also missing end parenthesis $q = "INSERT INTO menuCAL (header, url, user, idPai, menuNomePT, conteudo_pagina_PT, menuNomeEN, conteudo_pagina_EN) VALUES ('".$header."', '".$_POST['url']."', '".$_POST['user']."', '".$_POST['idPai']."', '".$menuNomePT."', '".$conteudo_pagina_PT."', '".$menuNomeEN."', '".$conteudo_pagina_EN."')"; Edited January 13, 2015 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1502865 Share on other sites More sharing options...
QuickOldCar Posted January 13, 2015 Share Posted January 13, 2015 As for the error, error (Undefined variable: opened in …) does not help us at all, show the full error But is most likely you are using a variable that was never defined and does not exist. Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1502867 Share on other sites More sharing options...
QuickOldCar Posted January 14, 2015 Share Posted January 14, 2015 I also wanted to add you don't even check if all the post values are set and the expected content If assign a variable why aren't you using it in the query? Make sure every input is escaped going into your query $user = $_POST['user'];$idPai = $_POST['idPai']; Adding some error checking in this could help as well, don't do any mysql queries if any errors $errors = array(); if(isset($_POST['user']) && trim($_POST['user']) !=''){ $user = mysqli_real_escape_string($dbc, trim($_POST['user'])); } else { $errors[] = "No user"; } //and the rest of your POST values if(!empty($errors)){ //do the mysql query } else { foreach($errors as $error){ echo "$error <br />"; } } Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1502869 Share on other sites More sharing options...
jmlab Posted January 14, 2015 Author Share Posted January 14, 2015 As for the error, error (Undefined variable: opened in …) does not help us at all, show the full error But is most likely you are using a variable that was never defined and does not exist. Am I not creating it here? <?php // SELECT QUERY if(isset($_GET['id'])) { $q = "SELECT * FROM menuCAL WHERE id = $_GET[id]"; $r = mysqli_query($dbc, $q); $opened = mysqli_fetch_assoc($r); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1502937 Share on other sites More sharing options...
Barand Posted January 14, 2015 Share Posted January 14, 2015 Yes, you are, but only if $_GET['id'] is set Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1502942 Share on other sites More sharing options...
jmlab Posted January 14, 2015 Author Share Posted January 14, 2015 Yes, you are, but only if $_GET['id'] is set Yes, I think so! That was my first thought! So the $_GET['id'] is not set because of the error you mentioned earlier, right? Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1502946 Share on other sites More sharing options...
jmlab Posted January 15, 2015 Author Share Posted January 15, 2015 (edited) Or Im not even making it? Can u explain me please? This is how I see it in Back-office: Edited January 15, 2015 by jmlab Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1502976 Share on other sites More sharing options...
QuickOldCar Posted January 15, 2015 Share Posted January 15, 2015 (edited) What is line 199 and 206 in index.php You can try adding this top of index page and get different error messages that may help. error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); Edited January 15, 2015 by QuickOldCar Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1502978 Share on other sites More sharing options...
jmlab Posted January 15, 2015 Author Share Posted January 15, 2015 What is line 199 and 206 in index.php You can try adding this top of index page and get different error messages that may help. error_reporting(E_ALL | E_NOTICE); ini_set('display_errors', '1'); Line 199 and 206 are the title and content in Portuguese (same for title and content in English). I already have that code in the index. Nothing happened. This is my debug panel: Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1502979 Share on other sites More sharing options...
QuickOldCar Posted January 15, 2015 Share Posted January 15, 2015 Where does $menuPai come from Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1503070 Share on other sites More sharing options...
jmlab Posted January 15, 2015 Author Share Posted January 15, 2015 Where does $menuPai come from Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1503080 Share on other sites More sharing options...
jmlab Posted January 15, 2015 Author Share Posted January 15, 2015 (edited) Is the category id from tree menu Is the "father id" Edited January 15, 2015 by jmlab Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1503081 Share on other sites More sharing options...
jmlab Posted January 15, 2015 Author Share Posted January 15, 2015 Where does $menuPai come from Sorry, $menuPai is the tree menu function Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1503084 Share on other sites More sharing options...
jmlab Posted January 16, 2015 Author Share Posted January 16, 2015 This is the recursive menu function. Shouldn't be a function? Is because of this, that we can't "$_GET['id']? How can I solved this? <?php //call the recursive function to print category listing category_tree(0); //Recursive php function function category_tree($menuPai){ global $dbc; $q = "SELECT * FROM menuCAL WHERE idPai ='".$menuPai."'"; $r = mysqli_query($dbc, $q); while($btnMenu = mysqli_fetch_assoc($r)): $i = 0; if ($i == 0) echo '<ul class="menuCAL">'; echo '<li><a href="?page='.$btnMenu['id'],'">' . $btnMenu['GlyPrincipal'] . $btnMenu['GlySecundario'] . $btnMenu['menuNomePT'], '</a>'; category_tree($btnMenu['id']); echo '</li>'; $i++; if ($i > 0) echo '</ul>'; endwhile; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1503101 Share on other sites More sharing options...
Solution QuickOldCar Posted January 16, 2015 Solution Share Posted January 16, 2015 Think this out and look what it's doing. $menuPai has no value unless define something right there or call on the function and insert it function category_tree($menuPai){ still has no value unless was set $q = "SELECT * FROM menuCAL WHERE idPai ='".$menuPai."'"; this could possibly use the function and have a value...the problem is you are within the same function you are trying to call upon category_tree($btnMenu['id']) Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1503115 Share on other sites More sharing options...
jmlab Posted January 16, 2015 Author Share Posted January 16, 2015 Think this out and look what it's doing. $menuPai has no value unless define something right there or call on the function and insert it function category_tree($menuPai){ still has no value unless was set $q = "SELECT * FROM menuCAL WHERE idPai ='".$menuPai."'"; this could possibly use the function and have a value...the problem is you are within the same function you are trying to call upon category_tree($btnMenu['id']) So, what is the best thing t do? Quote Link to comment https://forums.phpfreaks.com/topic/293903-cms-crud-treerecursive-menu/#findComment-1503176 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.