moonshine Posted December 13, 2014 Share Posted December 13, 2014 (edited) I'm trying to figure it out why my code it's only updating the last menu or submenu of a menu. The menu and submenu are stored in the same database and the parent_id is equal to the id for the sub-menus . Parent_id = 0 means it's a main menu. Help! $con = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $con); if(isset($_POST['update'])) { $sql = "UPDATE `menus` SET Menu_bar = '".$_POST['menu_bar']."' WHERE ID = ".$_POST['hidden']; mysql_query( $sql, $con ); } $resultMainMenu = mysql_query("SELECT * FROM menus WHERE Parent_id=0 ") or die(mysql_error()); echo '<form action=page.php method=post>'; while($row = mysql_fetch_assoc($resultMainMenu)) { echo '<ul>'; echo "<li><input type=submit name=update value=update><input type=hidden name=hidden value='".$row['ID']."'><input type=text name=menu_bar value='".$row['Menu_bar']."'><br/>" ; // echo main menu $resultSubmenu = mysql_query("SELECT * FROM menus WHERE Parent_id = " . $row['ID']) or die(mysql_error()); echo '<ul>'; while($rowSub = mysql_fetch_assoc($resultSubmenu)) { echo "<li><input type=submit name=update value=update><input type=hidden name=hidden value='".$rowSub['ID']."'><input type=text name=menu_bar value='".$rowSub['Menu_bar']."'><br/></li>"; // echo sub menu } echo '</ul>'; echo '</li>'; echo'</ul>'; } echo '</form>'; ?> Edited December 13, 2014 by moonshine Quote Link to comment https://forums.phpfreaks.com/topic/293080-updating-a-dynamic-menu-problem/ Share on other sites More sharing options...
hansford Posted December 13, 2014 Share Posted December 13, 2014 Show us your database table structures. All of the parent menus in the menus table have a Parent_id = 0; You're getting the sub-menu of each parent menu by looking for the Parent_id = $row['ID']. So we can assume from this that the $row['ID'] of each parent matches the Parent_id of their sub-menu. Quote Link to comment https://forums.phpfreaks.com/topic/293080-updating-a-dynamic-menu-problem/#findComment-1499515 Share on other sites More sharing options...
moonshine Posted December 13, 2014 Author Share Posted December 13, 2014 (edited) CREATE TABLE `lab4`.`menus` ( `ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`Menu_bar` VARCHAR( 100 ) NOT NULL ,`Parent_id` INT NOT NULL ) ENGINE = MYISAM ; ID Menu_bar Parent_id 1 Home 0 2 News 0 3 Events 2 4 Places 2 5 Contact 0 So Events and Places are the sub-menus of News. Yes all of the parent menus have Parent_id=0 and the sub-menus have the Parent_id equal to their parent unique ID . With only one while loop it works perfect but all of the items are dispalyed like a table and it should look like a menu . Edited December 13, 2014 by moonshine Quote Link to comment https://forums.phpfreaks.com/topic/293080-updating-a-dynamic-menu-problem/#findComment-1499517 Share on other sites More sharing options...
hansford Posted December 13, 2014 Share Posted December 13, 2014 (edited) Here is the html structure you're after. <ul> <li>Home</li> <li>News <ul> <li>Events</li> <li>Places</li> </ul> </li> <li>Contact</li> </ul> Reworking your code. echo '<ul>'; echo "<li><input type=\"submit\" name=\"update\" value=\"update\"><input type=\"hidden\" name=\"hidden" value=\"".$row['ID']."\"><input type=\"text\" name=\"menu_bar\" value=\"".$row['Menu_bar']."\"><br/>"; // echo main menu $resultSubmenu = mysql_query("SELECT * FROM menus WHERE Parent_id = " . $row['ID']) or die(mysql_error()); if( ! mysql_num_rows($resultSubmenu)) { echo '</li>'; } else { echo '<ul>'; while($rowSub = mysql_fetch_assoc($resultSubmenu)) { echo "<li><input type=\"submit\" name=\"update\" value=\"update\"><input type=\"hidden\" name=\"hidden\" value="\". $rowSub['ID']."\"><input type=\"text\" name=\"menu_bar\" value="\".$rowSub['Menu_bar']."\"><br/></li>"; // ec ho sub menu } echo '</ul>'; echo '</li>'; } I believe I got that right - you'll have to test it - might have missed an escape or some other syntax error. Edited December 13, 2014 by hansford Quote Link to comment https://forums.phpfreaks.com/topic/293080-updating-a-dynamic-menu-problem/#findComment-1499524 Share on other sites More sharing options...
moonshine Posted December 13, 2014 Author Share Posted December 13, 2014 Update it's not working as it should because now it only displays the first menu and his sub-menus and the update works only for the menu and his first sub-menu . Quote Link to comment https://forums.phpfreaks.com/topic/293080-updating-a-dynamic-menu-problem/#findComment-1499530 Share on other sites More sharing options...
hansford Posted December 13, 2014 Share Posted December 13, 2014 Try this - if it doesn't work, then I'll have to create a dummy db structure on my local and run the code myself to see what's going on. $con = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $con); if(isset($_POST['update'])) { $sql = "UPDATE `menus` SET Menu_bar = '".$_POST['menu_bar']."' WHERE ID = ".$_POST['hidden']; mysql_query( $sql, $con ); } $resultMainMenu = mysql_query("SELECT * FROM menus WHERE Parent_id=0 ") or die(mysql_error()); echo '<form action=\"page.php\" method=\"post\">'; echo '<ul>'; while($row = mysql_fetch_assoc($resultMainMenu)) { echo "<li><input type=\"submit\" name=\"update\" value=\"update\"><input type=\"hidden\" name=\"hidden" value=\"".$row['ID']."\"><input type=\"text\" name=\"menu_bar\" value=\"".$row['Menu_bar']."\"><br/>"; // echo main menu $resultSubmenu = mysql_query("SELECT * FROM menus WHERE Parent_id = " . $row['ID']) or die(mysql_error()); if( ! mysql_num_rows($resultSubmenu)) { echo '</li>'; } else { echo '<ul>'; while($rowSub = mysql_fetch_assoc($resultSubmenu)) { echo "<li><input type=\"submit\" name=\"update\" value=\"update\"><input type=\"hidden\" name=\"hidden\" value=\"". $rowSub['ID']."\"><input type=\"text\" name=\"menu_bar\" value="\".$rowSub['Menu_bar']."\"><br/></li>"; // echo sub menu } // end sub-menu loop echo '</ul>'; // end sub-menu ul echo '</li>'; // end main-menu li } } // end main-menu loop echo '</ul>'; // end main-menu ul echo '</form>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/293080-updating-a-dynamic-menu-problem/#findComment-1499533 Share on other sites More sharing options...
moonshine Posted December 14, 2014 Author Share Posted December 14, 2014 Still not working . Quote Link to comment https://forums.phpfreaks.com/topic/293080-updating-a-dynamic-menu-problem/#findComment-1499555 Share on other sites More sharing options...
hansford Posted December 14, 2014 Share Posted December 14, 2014 I ran the code myself. It works - all menu items and sub-menu items display. It will not update as designed. You would need to make each one of those menu items with the "update" button, its own separate form. Otherwise, it's just defaulting to the last form element - which is the contact menu item. You can change that one, but the others get ignored because they all have the same form element names. $con = mysql_connect($server, $user_name, $password); $db_found = mysql_select_db($database, $con); if(isset($_POST['update'])) { $sql = "UPDATE `menus` SET Menu_bar = '".$_POST['menu_bar']."' WHERE ID = ".$_POST['hidden']; if(!mysql_query( $sql, $con )) { echo 'update error: ' . mysql_error(); } } $resultMainMenu = mysql_query("SELECT * FROM menus WHERE Parent_id=0 ") or die(mysql_error()); echo '<form action="page.php" method="post">'; echo '<ul>'; while($row = mysql_fetch_assoc($resultMainMenu)) { echo '<li><input type="submit" name="update" value="update"><input type="hidden" name="hidden" value="'.$row['ID'].'"><input type="text" name="menu_bar" value="'.$row['Menu_bar'].'"><br/>'; // echo main menu $resultSubmenu = mysql_query("SELECT * FROM menus WHERE Parent_id=" . $row['ID']) or die(mysql_error()); if( ! mysql_num_rows($resultSubmenu)) { echo '</li>'; } else { echo '<ul>'; while($rowSub = mysql_fetch_assoc($resultSubmenu)) { echo '<li><input type="submit" name="update" value="update"><input type="hidden" name="hidden" value="'. $rowSub['ID'].'"><input type="text" name="menu_bar" value="'.$rowSub['Menu_bar'].'"><br/></li>'; // echo sub menu } // end sub-menu loop echo '</ul>'; // end sub-menu ul echo '</li>'; // end main-menu li } } // end main-menu loop echo '</ul>'; // end main-menu ul echo '</form>'; ?> And I know you're just getting your feet wet, but it would be better to get off on the right one. You should be using PDO or mysqli to make your database connections/interactions as the way you're doing it has been deprecated because of serious security related issues and slated for removal. Quote Link to comment https://forums.phpfreaks.com/topic/293080-updating-a-dynamic-menu-problem/#findComment-1499561 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.