Jump to content

Recommended Posts

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]";
                ?>

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
}

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.

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.
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.