mdmartiny Posted June 30, 2011 Share Posted June 30, 2011 This is a little project that I am working on. I keep receiving an error message and I don't see the error. I look over the code and everything looks fine and. it works like it is supposed to but I still have the error message at the top of the page. The error message that I am getting is Notice: Undefined index: act in C:\xampp\htdocs\forum\admin.php on line 63 This is the code that I have written and am currently using. <?php if (!isset($_SESSION['uid']) || (isset($_SESSION['uid']))) { $sql3 = "SELECT admin from `users` where `id` = '" . $_SESSION['uid'] . "'"; $result3 = mysql_query($sql3) or die(mysql_error()); if (mysql_num_rows($result3) == 0) { echo "You are not correctly logged in"; } else { $row2 = mysql_fetch_assoc($result3); if ($row2['admin'] != '1') { echo "You are not permitted to be here"; } else { $act = $_GET['act']; <------------ This is line #63 in my code-----------------------> $acts = array('create_cat', 'create_subcat'); $actions = array('create_cat' => 'Create a Forum Category', 'create_subcat' => 'Create a Forum sub category'); $x = 1; $c = count($actions); foreach ($actions AS $url => $link) { $bull = ($x == $c) ? "" : " • "; echo "<a href=\"admin.php?act=" . $url . "\">" . $link . "</a>" . $bull . ""; $x++; } echo "<br />"; if (!$act || !in_array($act, $acts)) { echo "Please choose an option from above to continue"; } else { if ($act == 'create_cat') { if (!isset($_POST['submit'])) { echo "<table border=\"0\" cellspacing=\"3\" cellpadding=\"3\">\n"; echo "<form method=\"post\" action=\"./admin.php?act=create_cat\">\n"; echo "<tr><td>Category Name</td><td><input type=\"text\" name=\"name\"></td></tr>\n"; echo "<tr><td>Admin Only?</td><td><input type=\"checkbox\" name=\"admin\" value=\"1\"></td></tr>\n"; echo "<tr><td colspan=\"2\" align=\"right\"><input type=\"submit\" name=\"submit\" value=\"Create Forum Category\"></td></tr>\n"; echo "</form></table>\n"; } else { $name = mss($_POST['name']); $admin = ($_POST['admin']); if ($name) { if (strlen($name) < 3 || strlen($name) > 32) { echo "The name of the category must be between 3 and 32 characters"; } else { $sql4 = "SELECT * FROM `category` WHERE `name` = '" . $name . "'"; $result4 = mysql_query($sql4) or die(mysql_error()); if (mysql_num_rows($result4) > 0) { echo "This Category all ready exsists"; } else { $admin_check = (admin == '1') ? "1" : "0"; $sql5 = "INSERT into `category` (`name`,`admin`) VALUES ('" .$name. "','" .$admin_check. "')"; $res5 = mysql_query($sql5) or die(mysql_error()); echo "This category has been successfully added!"; } } } else { echo "Category name field can not be left blank!"; } } } } } } } echo "$_SESSION[uid]"; ?> Link to comment https://forums.phpfreaks.com/topic/240810-undefined-index-how-do-i-keep-getting-thee/ Share on other sites More sharing options...
Pikachu2000 Posted June 30, 2011 Share Posted June 30, 2011 You need to check if a variable exists before attempting to access it, otherwise you'll get that notice if it doesn't. if( !empty($_GET['act']) ) { // do whatever it is you need to do with it. } else { // $_GET ['act'] hasn't been defined, or is empty } Link to comment https://forums.phpfreaks.com/topic/240810-undefined-index-how-do-i-keep-getting-thee/#findComment-1236847 Share on other sites More sharing options...
mdmartiny Posted June 30, 2011 Author Share Posted June 30, 2011 If I do that.. Then the error moves down to the other $act variables in the code on line 77 and line 88; Link to comment https://forums.phpfreaks.com/topic/240810-undefined-index-how-do-i-keep-getting-thee/#findComment-1236849 Share on other sites More sharing options...
Pikachu2000 Posted June 30, 2011 Share Posted June 30, 2011 Then evidently $_GET['act'] is not defined, or is empty. Since that entire block of code appears to rely on $_GET['act'] having a value, it shouldn't be allowed to run at all if that condition is not met. Link to comment https://forums.phpfreaks.com/topic/240810-undefined-index-how-do-i-keep-getting-thee/#findComment-1236852 Share on other sites More sharing options...
mdmartiny Posted June 30, 2011 Author Share Posted June 30, 2011 I got it figured out.. I did what you said and made a few other small changes to the code Link to comment https://forums.phpfreaks.com/topic/240810-undefined-index-how-do-i-keep-getting-thee/#findComment-1236879 Share on other sites More sharing options...
xyph Posted June 30, 2011 Share Posted June 30, 2011 A quick and dirty way to fix this, or to avoid a ton of isset checks, is to simply insert this around the top of your script $_GET['var'] = isset( $_GET['var'] ) ? $_GET['var'] : FALSE; In english, you're defining $_GET['var'] variably. If $_GET['var'] is set, you want to define it as itself, otherwise, you define it as FALSE. This gets rid of any notice errors involving the variable. The not-so-dirty way of doing this is to enclose everything requiring $_GET['var'] in an if statement, requiring that $_GET['var'] is set. if( isset($_GET['var']) ) { // the code blocks involving $_GET['var'] } else { // anything you want to happen if the var isn't set. } Link to comment https://forums.phpfreaks.com/topic/240810-undefined-index-how-do-i-keep-getting-thee/#findComment-1236917 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.