Jump to content

The topic doesn't show up in the forum board


leeex

Recommended Posts

I'm using a simple script to make a forum.

When you submit a topic, it successfully shows up in the database, but when you try to view the topic as a general user, the table is blank, but another row is made for every entry input.

 

create_topic.php

<?php include("layout.php"); ?>

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">

<tr>

<form id="form1" name="form1" method="post" action="add_topic.php">

<td>

<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">

<tr>

<td colspan="3" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>

</tr>

<tr>

<td width="14%"><strong>Topic</strong></td>

<td width="2%">:</td>

<td width="84%"><input name="topic" type="text" id="topic" size="50" /></td>

</tr>

<tr>

<td valign="top"><strong>Detail</strong></td>

<td valign="top">:</td>

<td><textarea name="detail" cols="50" rows="3" id="detail"></textarea></td>

</tr>

<tr>

<td><strong>Name</strong></td>

<td>:</td>

<td><input name="name" type="text" id="name" size="50" /></td>

</tr>

<tr>

<td><strong>Email</strong></td>

<td>:</td>

<td><input name="email" type="text" id="email" size="50" /></td>

</tr>

<tr>

<td> </td>

<td> </td>

<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>

</tr>

</table>

</td>

</form>

</tr>

</table>

<?php include("layout2.php"); ?>

 

 

view_topic.php

<?php include("layout.php"); ?>

<?php

$host="localhost"; // Host name

$username="root"; // Mysql username

$password="junior"; // Mysql password

$db_name="test"; // Database name

$tbl_name="forum_question"; // Table name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

// get value of id that sent from address bar

$id=$_GET['id'];

 

$sql="SELECT * FROM $tbl_name WHERE 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['topic']; ?></strong></td>

</tr>

 

<tr>

<td bgcolor="#F8F7F1"><? echo $rows['detail']; ?></td>

</tr>

 

<tr>

<td bgcolor="#F8F7F1"><strong>By :</strong> <? echo $rows['name']; ?> <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

$tbl_name2="forum_answer"; // Switch to table "forum_answer"

 

$sql2="SELECT * FROM $tbl_name2 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['a_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['a_name']; ?></td>

</tr>

<tr>

<td bgcolor="#F8F7F1"><strong>Email</strong></td>

<td bgcolor="#F8F7F1">:</td>

<td bgcolor="#F8F7F1"><? echo $rows['a_email']; ?></td>

</tr>

<tr>

<td bgcolor="#F8F7F1"><strong>Answer</strong></td>

<td bgcolor="#F8F7F1">:</td>

<td bgcolor="#F8F7F1"><? echo $rows['a_answer']; ?></td>

</tr>

<tr>

<td bgcolor="#F8F7F1"><strong>Date/Time</strong></td>

<td bgcolor="#F8F7F1">:</td>

<td bgcolor="#F8F7F1"><? echo $rows['a_datetime']; ?></td>

</tr>

</table></td>

</tr>

</table><br>

 

<?

}

 

$sql3="SELECT view FROM $tbl_name WHERE 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 $tbl_name(view) VALUES('$view') WHERE id='$id'";

$result4=mysql_query($sql4);

}

 

// count more value

$addview=$view+1;

$sql5="update $tbl_name set view='$addview' WHERE id='$id'";

$result5=mysql_query($sql5);

 

mysql_close();

?>

<BR>

<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 width="18%"><strong>Name</strong></td>

<td width="3%">:</td>

<td width="79%"><input name="a_name" type="text" id="a_name" size="45"></td>

</tr>

<tr>

<td><strong>Email</strong></td>

<td>:</td>

<td><input name="a_email" type="text" id="a_email" size="45"></td>

</tr>

<tr>

<td valign="top"><strong>Answer</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>

<?php include("layout2.php"); ?>

 

add_topic.php

<?php include("layout.php"); ?>

<?php

$host="localhost"; // Host name

$username="root"; // Mysql username

$password="junior"; // Mysql password

$db_name="test"; // Database name

$tbl_name="forum_question"; // Table name

 

// Connect to server and select database.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

// get data that sent from form

$topic=$_POST['topic'];

$detail=$_POST['detail'];

$name=$_POST['name'];

$email=$_POST['email'];

 

$datetime=date("d/m/y h:i:s"); //create date time

 

$sql="INSERT INTO $tbl_name(topic, detail, name, email, datetime)VALUES('$topic', '$detail', '$name', '$email', '$datetime')";

$result=mysql_query($sql);

 

if($result){

echo "Successful<BR>";

echo "<a href=main_forum.php>View your topic</a>";

}

else {

echo "ERROR";

}

mysql_close();

?>

<?php include("layout2.php"); ?>

 

main_forum.php

<?php include("layout.php"); ?>

<?php

$host="localhost"; // Host name

$username="root"; // Mysql username

$password="junior"; // Mysql password

$db_name="test"; // Database name

$tbl_name="forum_question"; // Table name

 

// Connect to server and select databse.

mysql_connect("$host", "$username", "$password")or die("cannot connect");

mysql_select_db("$db_name")or die("cannot select DB");

 

$sql="SELECT * FROM $tbl_name ORDER BY 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="6%" align="center" bgcolor="#E6E6E6"><strong>#</strong></td>

<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"><? echo $rows['id']; ?></td>

<td bgcolor="#FFFFFF"><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></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>

 

<?php

// Exit looping and close connection

}

mysql_close();

?>

<tr>

<td colspan="5" align="right" bgcolor="#E6E6E6"><a href="create_topic.php"><strong>Create New Topic</strong> </a></td>

</tr>

</table>

<?php include("layout2.php"); ?>

 

 

Even when I replaced forum_question with forum_answer for the table name under main_forum.php, i got:

WARNING: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\XAMPP\HTDOCS\MAIN_FORUM.PHP on line 27

 

So I just switched it back, and it's working the same again.

 

Here's what my tables look like:

 

forum_answer

nHbef.png

forum_question

wNNws.png

 

screenshot of main_forum.php

brcpp.png

 

Thanks in advance for any help!

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.