Undefined variable $act

Hey guys!

I have got an error when trying to do a php forum, which is an undefined variable $act = $_GET[‘act’]; but I do have a form below, that is outputing the name called act, which should work. I have also done an isset statement and I think the value is not set but not sure how to fix this: At the moment, my screen looks like this:

This is my code:

<?php
include_once 'header.php';
include_once 'includes/dbh.php';

$action = $_GET['act'];

$actions_array = array('forum');

?>

<!DOCTYPE html>
<html>
<head>
	<title>PianoCourse101 Forum</title>
	<link rel="stylesheet" type="text/css" href="style.css">


</head>
<body>
<center>
   <div id="holder">

      <div id="userInfo">
      <?php
         if($_SESSION['u_uid']) {
         	$sql = "SELECT * FROM users WHERE user_uid = ?;";

         	$stmt = mysqli_stmt_init($conn);
        if(!mysqli_stmt_prepare($stmt, $sql)) {
           echo "SQL error";
        } else {
          mysqli_stmt_bind_param($stmt, "s", $_SESSION['u_uid']);
          mysqli_stmt_execute($stmt);
          $result = mysqli_stmt_get_result($stmt);
          $resultCheck = mysqli_num_rows($result);
          
          
         }


     }

     if ($resultCheck == 0) {
          	 session_destroy();
          	 echo "<div class='forum_main_welcome'>Please <a href=\"./includes/login.php\">Login</a> to your account, or <a href=\"./signup.php\">Register</a> a new account \n</div>";
          } else {
          	$row = mysqli_fetch_assoc($result);
          	echo "<div class='forum_main_welcome'><h4>Welcome back, <a href=\"./forum_main.php?act=profile&id=".$row['user_id']."\">".$row['user_uid']."</a>!</h4></div>";
          	

          	if($row['admin'] == 1) {
              echo "<div class='forum_main_welcome'><h4><a href=\"./administrator.php\">Administrative Section</a></h4></div>\n";
          	}  else {
          	   echo "<div class='forum_main_welcome'>Please <a href=\"./login.php\">Login</a> to your account or <a href=\"./signup.php\"register a new account\n</div>";
          }
          	}
          
     
      ?>

      </div>
        <div id="content">
           <?php 
              if(!$action || !in_array($action, $actions_array)) {
                $admin = $row['admin'] + 1;
              	$sql2 = "SELECT * FROM forum_cats WHERE admin  < ?;";

              	$stmt = mysqli_stmt_init($conn);
		        if(!mysqli_stmt_prepare($stmt, $sql2)) {
		           echo "SQL error";
		        } else {
		          mysqli_stmt_bind_param($stmt, "s", $admin);
		          mysqli_stmt_execute($stmt);
		          $result2 = mysqli_stmt_get_result($stmt);
		          $i=1;
		          while ($row2 = mysqli_fetch_assoc($result2)) {
		          	echo "<div id=\"fcontent\">\n";
		          	echo "<div class=\"header\" id=\"header_".$i."\" onMouseOver=\"this.className='headerb'\" onMouseOut=\"this.className='header'\">".$row2['name']."</div>\n";
                    $id = $row2['id'];
                    $sql3 = "SELECT * FROM forum_sub_cats WHERE cid = ? AND admin < ?;";

                    $stmt = mysqli_stmt_init($conn);
			        if(!mysqli_stmt_prepare($stmt, $sql3)) {
			           echo "SQL error";
			        } else {
			          mysqli_stmt_bind_param($stmt, "ii", $id, $admin);
			          mysqli_stmt_execute($stmt);
			          $result3 = mysqli_stmt_get_result($stmt);
			          while ($row3 = mysqli_fetch_assoc($result3)) {
			          	echo "<div id=\"content\">\n";
			          	echo "<a href=\"./forum_main.php?act=forum&id=".$row3['id']."\">".$row3['name']."</a>\n";
			          	echo " ".$row3['des']."\n";
			          	echo "</div>\n";
			          }

		          	echo "</div>\n";
                    $i++;
		          }
              }
          }
      }

           ?>
        </div>
   </div>
</center>
</body>
</html>

I did a var_dump and it returns null…

Instead of
$action = $_GET['act'];
Use

if(isset($_GET['act'])){
$action = $_GET['act'];
}

I tried that but now I have this error:

But now my forum looks weird!

But I think for some reason, if I add something after the url… eg… forum_main.php?act=forum_main, then it does work… I am assuming that this set it…

You can set a variable if it exists, else an error is thrown, that’s why isset() or !empty() solves it

Thanks… In the video, he didn’t use isset but I tried that and it works… I just have to remember to use it the next time…