luvlemonade Posted April 17, 2007 Share Posted April 17, 2007 im making a simple forum for my skool assignment. im having problem to query the sql. juz like this forum, i allow the registered user only to add topic or add the answer, i have made the session for it, but i dont know how to insert the name and email which are taken from the table of "user" as they have logged in, into forum table. can anyone help me plz... sowiee im stil newbie :-\ Quote Link to comment Share on other sites More sharing options...
HeyRay2 Posted April 17, 2007 Share Posted April 17, 2007 Post the code that you already have and we can try to help you along from there. Quote Link to comment Share on other sites More sharing options...
cyrixware Posted April 17, 2007 Share Posted April 17, 2007 hello im interested in this. i search so many tutorials here but i cant see the tutorial codes in making a simple forum. can you pls post your codes. thanks Quote Link to comment Share on other sites More sharing options...
luvlemonade Posted April 17, 2007 Author Share Posted April 17, 2007 Alrite i will post 3 php files here. 1st file named main_forum.php <?php session_start(); include('../connect/configdb.php'); $sql="SELECT * FROM forum_question ORDER BY question_id DESC"; // OREDER BY id DESC is order result by descending $result=mysql_query($sql); ?> <table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td width="53%" align="center" bgcolor="#E6E6E6"><strong>Topic</strong></td> <td width="15%" align="center" bgcolor="#E6E6E6"><strong>Views</strong></td> <td width="13%" align="center" bgcolor="#E6E6E6"><strong>Replies</strong></td> <td width="13%" align="center" bgcolor="#E6E6E6"><strong>Date/Time</strong></td> </tr> <?php while($rows=mysql_fetch_array($result)){ // Start looping table row ?> <tr> <td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['question_id']; ?>"><? echo $rows['judul']; ?></a><BR></td> <td align="center" bgcolor="#FFFFFF"><? echo $rows['view']; ?></td> <td align="center" bgcolor="#FFFFFF"><? echo $rows['reply']; ?></td> <td align="center" bgcolor="#FFFFFF"><? echo $rows['datetime']; ?></td> </tr> <tr> <td colspan="5" align="right" bgcolor="#E6E6E6"> <?php } if ( isset($_SESSION['sudah_login']) AND $_SESSION['sudah_login'] == TRUE) { echo "<a href='create_topic.php'>Create New Topic </a>"; } else { echo "<a href='index.php'>[Home]</a>"; } ?> </td> </tr> </table> 2nd file is view_topic.php Note : "username" and "email" is taken from different table named "user" but i dont know how to join the tables <?php session_start(); include('../connect/configdb.php'); // get value of id that sent from address bar $id=$_GET['question_id']; $sql="SELECT * FROM forum_question WHERE question_id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); ?> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td><table width="100%" border="0" cellpadding="3" cellspacing="1" bordercolor="1" bgcolor="#FFFFFF"> <tr> <td bgcolor="#F8F7F1"><strong><? echo $rows['judul']; ?></strong></td> </tr> <tr> <td bgcolor="#F8F7F1"><? echo $rows['detail']; ?></td> </tr> <tr> <td bgcolor="#F8F7F1"><strong>By :</strong> <? echo $rows['username']; ?> <strong> Email : </strong><? echo $rows['email'];?></td> </tr> <tr> <td bgcolor="#F8F7F1"><strong>Date/time : </strong><? echo $rows['datetime']; ?></td> </tr> </table></td> </tr> </table> <BR> <?php $sql2="SELECT * FROM forum_answer WHERE question_id='$id'"; $result2=mysql_query($sql2); while($rows=mysql_fetch_array($result2)){ ?> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td><table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td bgcolor="#F8F7F1"><strong>ID</strong></td> <td bgcolor="#F8F7F1">:</td> <td bgcolor="#F8F7F1"><? echo $rows['answer_id']; ?></td> </tr> <tr> <td width="18%" bgcolor="#F8F7F1"><strong>Name</strong></td> <td width="5%" bgcolor="#F8F7F1">:</td> <td width="77%" bgcolor="#F8F7F1"><? echo $rows['nama']; ?></td> </tr> <tr> <td bgcolor="#F8F7F1"><strong>Email</strong></td> <td bgcolor="#F8F7F1">:</td> <td bgcolor="#F8F7F1"><? echo $rows['email']; ?></td> </tr> <tr> <td bgcolor="#F8F7F1"><strong>Answer</strong></td> <td bgcolor="#F8F7F1">:</td> <td bgcolor="#F8F7F1"><? echo $rows['detail']; ?></td> </tr> <tr> <td bgcolor="#F8F7F1"><strong>Date/Time</strong></td> <td bgcolor="#F8F7F1">:</td> <td bgcolor="#F8F7F1"><? echo $rows['datetime']; ?></td> </tr> </table></td> </tr> </table><br> <? } $sql3="SELECT view FROM forum_question WHERE question_id='$id'"; $result3=mysql_query($sql3); $rows=mysql_fetch_array($result3); $view=$rows['view']; // if have no counter value set counter = 1 if(empty($view)){ $view=1; $sql4="INSERT INTO forum_question (view) VALUES('$view') WHERE question_id='$id'"; $result4=mysql_query($sql4); } // count more value $addview=$view+1; $sql5="update forum_question set view='$addview' WHERE question_id='$id'"; $result5=mysql_query($sql5); mysql_close(); ?> <BR> <?php if ( isset($_SESSION['sudah_login']) AND $_SESSION['sudah_login'] == TRUE) { ?> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form name="form1" method="post" action="add_answer.php"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td valign="top"><strong>Add Topic </strong></td> <td valign="top">:</td> <td><textarea name="a_answer" cols="45" rows="3" id="a_answer"></textarea></td> </tr> <tr> <td> </td> <td><input name="id" type="hidden" value="<? echo $id; ?>"></td> <td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td> </tr> </table> </td> </form> </tr> </table> <? } ?> and the last file is "add_answer.php" <?php include('../connect/configdb.php'); // Get value of id that sent from hidden field $id=$_POST['question_id']; // Find highest answer number. $sql="SELECT MAX(answer_id) AS Maxa_id FROM forum_answer WHERE question_id='$id'"; $result=mysql_query($sql); $rows=mysql_fetch_array($result); // add + 1 to highest answer number and keep it in variable name "$Max_id". if there no answer yet set it = 1 if ($rows) { $Max_id = $rows['Maxa_id']+1; } else { $Max_id = 1; } // get values that sent from form $detail =$_POST['detail']; $datetime=date("d/m/y H:i:s"); // create date and time // Insert answer $sql2="INSERT INTO forum_answer(question_id, answer_id, detail, datetime) VALUES('$id', '$Max_id', '$detail', '$datetime')"; $result2=mysql_query($sql2); if($result2){ ?> <br> <br> <div align="center"> <? echo "Your answer has been added<BR>"; echo "<a href='view_topic.php?id=".$id."'>View your answer</a>"; // If added new answer, add value +1 in reply column $sql3="UPDATE forum_question SET reply='$Max_id' WHERE question_id='$id'"; $result3=mysql_query($sql3); } else { echo "ERROR"; } mysql_close(); ?> Anyways, thanks HeyRay2 for the respond, hopefuly i could solve this prob, to cyrixware i will share you the codes when im done with this Quote Link to comment Share on other sites More sharing options...
Anzeo Posted April 17, 2007 Share Posted April 17, 2007 use $_SESSION variables to store your username during a session so when logging in you'd need to put for example $_SESSION['Username'] = put here your sql result for the username Quote Link to comment Share on other sites More sharing options...
cyrixware Posted April 18, 2007 Share Posted April 18, 2007 thanks luvlemonade... cheers! Quote Link to comment Share on other sites More sharing options...
luvlemonade Posted April 18, 2007 Author Share Posted April 18, 2007 Anzeo, i need more explanation, where do i put the $_SESSION variables and would u plz give me the sql query... thankz Quote Link to comment Share on other sites More sharing options...
Anzeo Posted April 18, 2007 Share Posted April 18, 2007 well I assume you have a login page for your members? And you made it work right? Have you already used session or not? Otherwise I'll need to explain to you in more detail. Quote Link to comment Share on other sites More sharing options...
luvlemonade Posted April 19, 2007 Author Share Posted April 19, 2007 you're rite i have login page and it works and i have made the session like this: $_SESSION['username']=$rec['username']; $_SESSION['sudah_login']=TRUE; but i dunno where to put the session variables, and also the sql query to select the fields.. Quote Link to comment Share on other sites More sharing options...
Anzeo Posted April 19, 2007 Share Posted April 19, 2007 well you could use your $_SESSION['username'] now to query your table. It should look like: <?php $UserName = $_SESSION['username']; $qry = "SELECT Name,Email FROM USERS WHERE Name='$UserName'"; if($result = mysql_query($qry)) { if(mysql_num_rows($result)) { $Username = $result['Name']; $Email = $result['Email']; } else echo "No data available"; } else echo "Query failed" . mysqlerror(); ?> Quote Link to comment Share on other sites More sharing options...
luvlemonade Posted April 20, 2007 Author Share Posted April 20, 2007 oh i thought i would need to join the tables of user and forum_question, i will try ur way anyways , i will let u know when it works, thanks Anzeo Quote Link to comment Share on other sites More sharing options...
luvlemonade Posted April 20, 2007 Author Share Posted April 20, 2007 humm i have tried it, i didnt find any error, but name and email doesnt appear on the browser.. Quote Link to comment Share on other sites More sharing options...
Anzeo Posted April 24, 2007 Share Posted April 24, 2007 Have you echoed them out? Quote Link to comment Share on other sites More sharing options...
luvlemonade Posted May 4, 2007 Author Share Posted May 4, 2007 lol im still working on the forum, kinda complicated to me and yes i did echo em out, it didnt work. Anyways, can i join the table by adding the user_id from USER table into forum answer where username is taken from $_SESSION['username'] ?? if it can, how do i query it? one more question, how do i add SMF into our website? thanks Quote Link to comment Share on other sites More sharing options...
yzerman Posted May 4, 2007 Share Posted May 4, 2007 ok from what I understand, you have 2 tables, and you want to pull information from both of them. This is actually less complicated then it seems. Using MySql, the easiest way to accomplish this is using the period. Here is the first query, for users: SELECT username FROM users Thats it. Now, lets say you just want the username and password from the users table, and the posts that that user made in the forums table SELECT users.username, users.password, forums.topicid FROM users, forums WHERE forums.post_user = users.username AND users.username = 'yzerman' This will output the following: USERNAME PASSWORD TOPICID yzermanMD5HASHPWD12345 ok, so basically, when you are doing querys, there are 3 levels you can select, update, insert, delete, etc with Rows SELECT * FROM users Tables SELECT users.username, users.password, forums.topicid FROM users, forums WHERE forums.post_user = users.username AND users.username = 'yzerman' And finally databases. Say your users table is in a database called bio, and your forums were in a database called community To select from different databases, all you have to do is add another level to your query. SELECT bio.users.username, bio.users.password, community.forums.topicid FROM bio.users, community.forums WHERE community.forums.post_user = bio.users.username AND bio.users.username = 'yzerman' Just remember when your using querys like this, you always have to specify the table, and database if you select it in this fashion. Quote Link to comment Share on other sites More sharing options...
luvlemonade Posted May 11, 2007 Author Share Posted May 11, 2007 it worked! i forgot to add $_session['user_id'] in login script to be used in forum table lolz thanks guys Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.