murtz Posted June 10, 2008 Share Posted June 10, 2008 Hey all. Im really stuck at the moment! Im building a simple discussion board to go on my website. At the moment.. I have 2 tables.. forum_topics (topic_id, topic_title, topic_create_time, topic_owner) forum_posts (post_id, topic_id, post_text, post_create_time, post_owner) Im using the form below.. At the moment.. Im able to insert new topic data into the forum_topics table.. and Im also able to enter the text data and post owner data into the forum_posts table. However.. the problem im getting is that I cant pass the topic_id to the posts table. Everytime I create a new topic.. the topic ID getting passed to the forum_posts table is '0'. I would have thought the $topic_id = mysql_insert_id; would have sorted this out for me! Heres my code.. <?php ini_set('session.gc_maxlifetime', 10); session_start(); //check for required fields from the form if ((!$_POST["topic_owner"]) || (!$_POST["topic_title"]) || (!$_POST["post_text"])) { header("Location: addtopic.html"); exit; } include('connection.php'); $tbl_name = "forum_topics"; mysql_connect("", "", "") or die ("Cannot Connect To The Database"); mysql_select_db("$dbname") or die("Cannot Select Database"); $topic_title=mysql_real_escape_string(trim($_POST['topic_title'])); $topic_owner=mysql_real_escape_string(trim($_POST['topic_owner'])); //create and issue the first query $add_topic = "INSERT INTO forum_topics (topic_title, topic_create_time, topic_owner) VALUES ('$topic_title',NOW(),'$topic_owner')"; mysql_query($add_topic) or die ('... TOPIC was Unsuccessful, Please go back and try again'); //create and issue the second query $topic_id = mysql_insert_id; $post_text = mysql_real_escape_string(trim($_POST['post_text'])); $topic_owner = mysql_real_escape_string(trim($_POST['topic_owner'])); $add_post = "INSERT INTO forum_posts(topic_id,post_text, post_create_time, post_owner) VALUES ('".$topic_id."','$post_text', NOW(),'$topic_owner')"; mysql_query($add_post) or die ('... POST was Unsuccessful, Please go back and try again'); // Check if the form has been submitted. $display_block = "<P>The <strong>".$_POST["topic_title"]."</strong> topic has been created.</p>"; ?> <html> <head> <title>New Topic Added</title> </head> <body> <h1>New Topic Added</h1> <?php echo $display_block; ?> </body> </html> Please help. The thing thats annoying me the most is that this used to work!. ive just had to reformat my laptop and Ive reinstalled everything.. but its no longer working! Everything else is working (registration log on etc etc). Im 90% sure I havent changed the file or anything. Any Help is really REALLY appreciated! Link to comment https://forums.phpfreaks.com/topic/109572-basic-discussion-forum-help-topic-id-not-being-passed-to-post-table/ Share on other sites More sharing options...
mhodge_txs Posted June 10, 2008 Share Posted June 10, 2008 have you got auto increment on your forum_topics table for topic_id ? Link to comment https://forums.phpfreaks.com/topic/109572-basic-discussion-forum-help-topic-id-not-being-passed-to-post-table/#findComment-562021 Share on other sites More sharing options...
murtz Posted June 10, 2008 Author Share Posted June 10, 2008 No i havent got auto increment.. I think I figured it out! I had to put $connect next to my connection.. and then put msql_insert_id($connect). Thanks anyway Link to comment https://forums.phpfreaks.com/topic/109572-basic-discussion-forum-help-topic-id-not-being-passed-to-post-table/#findComment-562459 Share on other sites More sharing options...
murtz Posted June 13, 2008 Author Share Posted June 13, 2008 Ok so now Im stuck on something else! Im trying to create a table that will show the Topic Title, Post Count, Topic Creator.. and Topic Create Date. Im doing all of the above apart from the Post Count part. Ive got my query for it.. I just dont know how to echo the value in the table! heres my code.. <?php //show all the topics $show_topics = mysql_query("SELECT topic_id, topic_title, DATE_FORMAT(topic_create_time, '%d %M %y at %r') AS topic_time, topic_owner FROM forum_topics ORDER BY topic_create_time DESC"); $get_owner = mysql_query("SELECT full_name FROM student WHERE student.student_id = forum_topics.topic_owner"); //$show_topic_res = mysql_query($show_topics) or die ('Query Failed: <br>'.$show_topics.'<br><br>MySQL Said:<br>'.mysql_error()); $topic_id = $topic_info['topic_id']; $topic_title = stripslashes($topic_info['topic_title']); $topic_time = $topic_info['topic_time']; $topic_starter = stripslashes($topic_info['topic_owner']); //get number of posts $num_posts = "SELECT COUNT(post_id) AS post_count FROM forum_posts WHERE topic_id = '".$topic_id."'"; $num_post_res = mysql_query($num_posts) or die ('Query Failed: <br>'.$num_posts.'<br><br>MySQL Said:<br>'.mysql_error()); //free results mysql_free_result($get_topics_res); mysql_free_result($get_num_posts_res); ?> <table cellpadding=\"3\" cellspacing=\"1\" border=\"1\"> <tr> <th width="199">Topic Title</th> <th width="100">#Posts</th> <th width="91">Topic Creator</th> <th width="129">Date Created</th> </tr> <?php while ($row = mysql_fetch_array($show_topics)){ ?> <tr> <td><span class="style20"><?php echo"<a href=\"showtopic.php?topic_id={$row['topic_id']}\">{$row['topic_title']}"?></span></td> <td><span class="style20"><?php echo $row["topic_owner"]; ?></span></td> <td><span class="style20"><?php echo $row["topic_time"]; ?></span></td> <?php } ?> </table> Im trying to do something like this.. $num_post_res = mysql_query($num_posts) or die ('Query Failed: <br>'.$num_posts.'<br><br>MySQL Said:<br>'.mysql_error()); while ($posts_info = mysql_fetch_array($num_post_res)) { $num_posts = $posts_info['post_count']; But I dont know how to echo the value in the table above. Please help! Link to comment https://forums.phpfreaks.com/topic/109572-basic-discussion-forum-help-topic-id-not-being-passed-to-post-table/#findComment-565023 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.