0

I was working on a forum script when i encounter this error

Notice: Undefined variable: userids in C:\xampp\htdocs\myfolder\discussion\post_reply_parse.php on line 19 Unknown column 'email' in 'field list'

Please, help me. This is the code

<?php
session_start(); 
if ($_SESSION['uid']) {
    if (isset($_POST['reply_submit'])) {
        include_once("connect.php");
        $creator = $_SESSION['uid'];
        $cid = $_POST['cid'];
        $tid = $_POST['tid'];
        $reply_content = $_POST['reply_content'];
        $sql = "INSERT INTO posts (category_id, topic_id, post_creator, post_content, post_date) VALUES ('".$cid."', '".$tid."', '".$creator."', '".$reply_content."', now())";
        $res = mysql_query($sql) or die(mysql_error());
        $sql2 = "UPDATE categories SET last_post_date=now(), last_user_posted='".$creator."' WHERE id='".$cid."' LIMIT 1";
        $res2 = mysql_query($sql2) or die(mysql_error());
        $sql3 = "UPDATE topics SET topic_reply_date=now(), topic_last_user='".$creator."' WHERE id='".$tid."' LIMIT 1";
        $res3 = mysql_query($sql3) or die(mysql_error());
        $sql4 = "SELECT post_creator FROM posts WHERE category_id='".$cid."' AND topic_id='".$tid."' GROUP BY post_creator";
        $res4 = mysql_query($sql4) or die(mysql_error());
        while ($row4 = mysql_fetch_assoc($res4)) {
            $userids[] .= $row4['post_creator'];
        }
        foreach ($userids as $key) {
            $sql5 = "SELECT id, email FROM users WHERE id='".$key."' AND forum_notification='1' LIMIT 1";
            $res5 = mysql_query($sql5) or die(mysql_error());
            if (mysql_num_rows($res5) > 0) {
                $row5 = mysql_fetch_assoc($res5);
                if ($row5['id'] != $creator) {
                    $email .= $row5['email'].", ";
                }
            }
        }
        $email = substr($email, 0, (strlen($email) - 2));
        $to = "noreply@somewhere.com";
        $from = "YOUR_SITE_EMAIL_HERE";
        $bcc = $email;
        $subject = "YOUR_SUBJECT_HERE";
        $message = "YOU MESSAGE CONTENT HERE";

        $headers = "From: $from\r\nReply-To: $from";
        $headers .= "\r\nBcc: {$bcc}";
        mail($to, $subject, $message, $headers);

        if (($res) && ($res2) && ($res3)) {
            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>";
        } else {
            echo "<p>There was a problem posting your reply. Try again later.</p>";
        }

    } else {
        exit();
    }
} else {
    exit();
}
?>
Qirel
  • 25,449
  • 7
  • 45
  • 62
Yomi
  • 63
  • 1
  • 2
  • 6
  • `mysql_*` functions are deprecated since PHP 5.5 (and **removed entirely** in PHP 7) and you should [stop using them](http://stackoverflow.com/q/12859942) if you can. You should choose another API that allows you to use prepared statements (which you *really should*), like `mysqli_*` or PDO - see [choosing an API](http://php.net/manual/en/mysqlinfo.api.choosing.php). – Qirel Jun 09 '16 at 21:28
  • About the undefined variable notice: Before the `while` loop, just define it. `$userids = array();` - and for the column-error, simply spell the name of the columns in your database correctly? Appearantly, there is no `email` column in the users table. – Qirel Jun 09 '16 at 21:29
  • 1
    And what's that dot(`.`) doing there in `$userids[] .= $row4['post_creator'];`? – Rajdeep Paul Jun 09 '16 at 21:30
  • 1
    @Yomi please post the structure of your `users` table, probably some spelling error – rsz Jun 09 '16 at 21:30
  • It's because you didn't get into the `while` cycle.... – MikeVelazco Jun 09 '16 at 21:31

1 Answers1

0

Look at the structure of the users table and make sure you use the correct name for the column containing the email address. After that, remove the period on the line

$userids[] .= $row4['post_creator']; 

...so it turns into:

$userids[] = $row4['post_creator'];
Christian Lundahl
  • 2,000
  • 3
  • 18
  • 29