Undefined error when creating php forum

Hey guys!

I am trying to make a php forum and almost got to the end but have the following errors:

This is my code:

<?php
include_once 'header.php';
if (!isset($_SESSION['u_uid'])) {
	header ("Location: index.php?post_reply_parse=notlogin");
	exit();
	} else {
	if (!isset($_POST['reply_submit'])) {
		header("Location: post_reply.php?post_reply_parse=error");
		exit();

	} else {
		include_once 'includes/dbh.php';
		$date = date("Y-m-d H:i:s");
		$creator = $_SESSION['u_uid'];
		$reply_content = $_POST['reply_content'];
		$cid = $_POST['cid'];
		$tid = $_POST['tid'];
		$limit = 1;

		$sql = "INSERT INTO posts (category_id, topic_id, post_creator, post_content, post_date) VALUES (?,?,?,?,?);";

		if (!mysqli_stmt_prepare($stmt, $sql)) {
				   echo 'SQL error';
				   
				} else {
				    mysqli_stmt_bind_param($stmt, "iisss", $cid, $tid, $creator, $reply_content, $date);
				    mysqli_stmt_execute($stmt);

				    $sql2 = "UPDATE categories
                             SET last_post_date = ?, last_user_posted = ?
                             WHERE id = ?
                             LIMIT ?
				             ";

				    if (!mysqli_stmt_prepare($stmt, $sql2)) {
				   echo 'SQL error';
				   
				} else {
				    mysqli_stmt_bind_param($stmt, "ssii", $date, $creator, $cid, $limit);
				    mysqli_stmt_execute($stmt);

				    $sql3 = "UPDATE topics
                             SET topic_reply_date = ?, topic_last_user = ?
                             WHERE id = ?
                             LIMIT ?
				            ";
                 	if (!mysqli_stmt_prepare($stmt, $sql3)) {
				      echo 'SQL error';
				   
				} else {
				    mysqli_stmt_bind_param($stmt, "ssii", $date, $creator, $tid, $limit);
				    mysqli_stmt_execute($stmt);

				    // Email Sending 

				    $sql4 = "SELECT post_creator FROM posts WHERE category_id = ? AND topic_id = ? GROUP BY post_creator;";

				    if (!mysqli_stmt_prepare($stmt, $sql4)) {
				   echo 'SQL error';
				   
				} else {
				    mysqli_stmt_bind_param($stmt, "ii", $cid, $tid);
				    mysqli_stmt_execute($stmt);
				    $result4 = mysqli_stmt_get_result($stmt);

				    while ($row4 = mysqli_fetch_assoc($result4)) {
                       $userids[] .= $row4['post_creator'];
				    }

				    foreach ($userids as $key) {
				    	$forum_notification = 1;
				    	$limit = 1;

				    	$sql5 = "SELECT user_id, user_email FROM users WHERE user_id = ? AND forum_notification = ? LIMIT ?;";

                        if (!mysqli_stmt_prepare($stmt, $sql5)) {
				   echo 'SQL error';
				   
				} else {
				    mysqli_stmt_bind_param($stmt, "iii", $key, $forum_notification, $limit);
				    mysqli_stmt_execute($stmt);
				    $result5 = mysqli_stmt_get_result($stmt);
				    $resultCheck5 = mysqli_num_rows($result5);

				    if ($resultCheck5 > 0) {
                       $row5 = mysqli_fetch_assoc($result5);
                       if ($row5['user_id'] != $creator) {
                       	   $email .= $row5['user_email'].", ";

                       }
                       
				    } 
				    }

				    $email = substr($email, 0, (strlen($email) - 2));

				    $company = "pianocourse101@hotmail.com";
                    $subject = "Forum reply";
                    $mailTo = $email;
                    $bcc = $email;
                    $headers = "From: ".$company;
                    $headers .= "\r\nBcc: {$bcc}";
                    $txt = "Hello ".$_SESSION['u_first']." ".$_SESSION['u_last']."! \n\n Someone has replied to a topic you were apart of.";

                    mail($mailTo, $subject, $txt, $headers);


	}

	echo "<p>Your reply has been successfully posted. <a href='view_topic.php?cid=".$cid."&tid=".$tid."'>Click here to return to the topic.</a></p>";
}
}
 }
}
}
}

The error says email and userids are undefined variables. Find them and check why you’re calling them before defining .

thanks… I think in the video tutorial, he didn’t define the variables… should I do something like $userids=array();? Will that work before calling $userids[]?

I am still getting the email undefined problem but it should work because i have stored $row [‘user_email’] into the variable

It should store the post creator username in an array… i have got that working by defining it as $userid = array () but for some reason, the $email variable is not defined?

I think the video tutorial forgot to define the variable but if I just add $email=""; at the very top of the page, then it works… I hope this is correct?