Jump to content

need help [simple forum]


luvlemonade

Recommended Posts

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 :-\

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

  • 2 weeks later...

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

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.