making a notify me of replies code

Posted on 2008-11-13
Last Modified: 2012-05-05
ok i have made my own forum and im currently creating a notify me of replies part for my forum...

i have come up with the idea of this..

Make a forum_email table..... this has:
mailed (enum y/n)

If a user wants an email when this thread is replied to, insert a row and set mailed to default = n.

Now, when someone makes a reply to this topic, do...

$query = "SELECT fe.*,, FROM forum_email as fe
LEFT JOIN users as u on u.user_id = fe.user_id
WHERE topic_id = '".$this_topic_id."' AND fe.user_id != '".$thepostinguser."' AND mailed='n'";
$res = @mysql_query($query,$link) or die ("sendNotification query fails :".mysql_error());

//nobody wants an email, stop now
if(count($user_id) == 0):

foreach($user_id as $value) {
send emails

//set emailed to 'y' so that they don't get numerous emails... set it back to 'n' when they view this topic
$query_noted = "UPDATE forum_email SET mailed='y' WHERE topic_id = '".$this_topic_topic_id."'";

the problem is if the user is vreating the post and the post id is auto inc how can i get the topic id if its not yet created :@S
Question by:runnerjp
    LVL 15

    Expert Comment

    you can get the ID of the last insert by using the mysql_insert_id() function.. so... just run the INSERT to create the topic and THEN run

    $newPostID = mysql_insert_id();

    and now $newPostID has the ID of the newly created topic. You'll just have to run your "email me of replies" code after the topic creation.

    Author Comment

    how would i do this

    i tried the code below but it didnt work
    		$insertpost = "INSERT INTO forumtutorial_posts(forum,author,title,post,showtime,realtime,lastrepliedto,lastposter) values('$forum','$username','$theSubject','$thePost','$thedate','$thedate','$thedate','$username')";
    												mysql_query($insertpost) or die("Could not insert post"); //insert post
    												//$insertemail = "INSERT INTO forum_email(user_id,topic_id,mailed) values('$id','$forumpostid','1')";
    												//mysql_query($insertemail) or die("Could not insert email"); //insert  email
    												$updatepost = "UPDATE `users` SET `post_count`=`post_count`+'1' WHERE `Username`='$username'";
    												mysql_query($updatepost) or die("Could not update post");
    												$newPostID = mysql_insert_id($insertpost);
    												echo $newPostID;

    Open in new window

    LVL 107

    Accepted Solution

    See if this helps... ~Ray

    SP: if the query fails to insert, $xyz == FALSE
    $insertpost = "INSERT INTO ...";
    $xyz = mysql_query($insertpost) or die("Could not insert post"); // RESOURCE ID IS IN $xyz
    $newPostID = mysql_insert_id($xyz);
    echo $newPostID;

    Open in new window


    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    This is a general how to create your own custom plugin system for your PHP application that you designed (or wish to extend a third party program to have plugin functionality that doesn't have it yet).  This is not how to make plugins for existing s…
    Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now