trecool999 Posted January 29, 2007 Share Posted January 29, 2007 Ok, when a user posts a topic on my forum/shoutbox hybrid thingy, it works fine, but then when someone posts a reply to it, it posts well but only displays id: and just gives you the number of the reply on the page. Please help. Example: [url=http://trecool999.8888mb.com/view_topic.php?id=1]http://trecool999.8888mb.com/view_topic.php?id=1[/url].Codes:view_topic.php:[code]<?php$host="localhost"; // Host name$username="trecool999_user"; // Mysql username$password="*password*"; // Mysql password$db_name="trecool999_user"; // 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 = 1if(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>[/code]add_answer.php:[code]<?php$host="localhost"; // Host name$username="trecool999_user"; // Mysql username$password="*password*"; // Mysql password$db_name="trecool999_user"; // Database name$tbl_name="forum_answer"; // 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 hidden field$id=$_POST['id'];// Find highest answer number.$sql="SELECT MAX(a_id) AS Maxa_id FROM $tbl_name 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 = 1if ($rows) {$Max_id = $rows['Maxa_id']+1;}else {$Max_id = 1;}// get values that sent from form$a_name=$POST['a_name'];$a_email['a_email'];$a_answer['a_answer'];$datetime=date("d/m/y H:i:s"); // create date and time// Insert answer$sql2="INSERT INTO $tbl_name(question_id, a_id, a_name, a_email, a_answer, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_email', '$a_answer', '$datetime')";$result2=mysql_query($sql2);if($result2){echo "Successful<BR>";echo "<a href='view_topic.php?id=".$id."'>View your answer</a>";// If added new answer, add value +1 in reply column$tbl_name2="forum_question";$sql3="UPDATE $tbl_name2 SET reply='$Max_id' WHERE id='$id'";$result3=mysql_query($sql3);}else {echo "ERROR";}mysql_close();?>[/code]add_topic.php:[code]<?php$host="localhost"; // Host name$username="trecool999_user"; // Mysql username$password="*password*"; // Mysql password$db_name="trecool999_user"; // 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();?>[/code]create_topic.php:[code]<table width="400" 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>[/code]main_forum:[code]<?php$host="localhost"; // Host name$username="trecool999_user"; // Mysql username$password="*password*"; // Mysql password$db_name="trecool999_user"; // 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);?><style type="text/css"><!--#Layer1 { position:absolute; left:0; top:0; width:480; height:272; z-index:1;}body { background-image: url(Images/Background-X.png); background-repeat: repeat-x; background-color: #0099FF;}--></style><div id="Layer1"> <table width="480" height="186" border="0" align="left" cellpadding="0" cellspacing="0"> <tr> <td width="6%" height="90" align="center"> </td> <td width="42%" align="center"> </td> <td width="23%" align="center"> </td> <td width="13%" align="center"> </td> <td width="16%" align="center"> </td> </tr> <tr> <td width="6%" height="19" align="center"><strong>#</strong></td> <td align="center"><strong>Topic</strong></td> <td width="23%" align="center"><strong>Views</strong></td> <td width="13%" align="center"><strong>Replies</strong></td> <td width="16%" align="center"><strong>Date/Time</strong></td> </tr> <?phpwhile($rows=mysql_fetch_array($result)){ // Start looping table row?> <tr> <td height="24"><? echo $rows['id']; ?></td> <td><a href="view_topic.php?id=<? echo $rows['id']; ?>"><? echo $rows['topic']; ?></a><br /></td> <td align="center"><? echo $rows['view']; ?></td> <td align="center"><? echo $rows['reply']; ?></td> <td align="center"><? echo $rows['datetime']; ?></td> </tr> <?php// Exit looping and close connection}mysql_close();?> <tr> <td height="19" colspan="5" align="right"><a href="create_topic.php"><strong>Create New Topic</strong> </a></td> </tr> <tr> <td height="32" colspan="5" align="right"><img src="Images/Background-X2.png" width="480" height="32" /></td> </tr> </table></div>[/code] Link to comment https://forums.phpfreaks.com/topic/36137-solved-why-does-this-happen/ Share on other sites More sharing options...
tippy_102 Posted January 29, 2007 Share Posted January 29, 2007 In add_answer.php you have$sql="INSERT INTO $tbl_name(topic, detail, ...You do not need the $ in front of the tbl_name Link to comment https://forums.phpfreaks.com/topic/36137-solved-why-does-this-happen/#findComment-171602 Share on other sites More sharing options...
trecool999 Posted January 29, 2007 Author Share Posted January 29, 2007 That just gives me an error after replying. Link to comment https://forums.phpfreaks.com/topic/36137-solved-why-does-this-happen/#findComment-171664 Share on other sites More sharing options...
chronister Posted January 29, 2007 Share Posted January 29, 2007 $tbl_name is a variable so the $ is needed.Go through the steps and make sure that all your variables are being filled properly. Echo all your queries and make sure they are getting the right information. Make sure that your forms are posting properly.In add_answer.php what are these lines doing??[code]// get values that sent from form$a_name=$POST['a_name']; /* This should be $_POST['a_name'] */$a_email['a_email']; /* What is this??, do you have an array called $a_email */$a_answer['a_answer']; /* this also is doing nothing again, do you have an array called $a_answer?? */[/code]In your query you are trying to use $a_answer and $a_email but I am not seeing where you set those, It looks like a typo on the $a_email and $a_answer variables they should be[code]$a_name=$_POST['a_name'];$a_email=$_POST['a_email']; $a_answer=$_POST['a_answer'];[/code] I would start there. Link to comment https://forums.phpfreaks.com/topic/36137-solved-why-does-this-happen/#findComment-171675 Share on other sites More sharing options...
trecool999 Posted January 29, 2007 Author Share Posted January 29, 2007 [quote author=chronister link=topic=124490.msg515938#msg515938 date=1170058312]$tbl_name is a variable so the $ is needed.Go through the steps and make sure that all your variables are being filled properly. Echo all your queries and make sure they are getting the right information. Make sure that your forms are posting properly.[b]In add_answer.php what are these lines doing??[/b]It collects the data from the form, turns it into a variable and then the variable is echoed (or meant to) in the table.[code]// get values that sent from form$a_name=$POST['a_name']; /* This should be $_POST['a_name'] */$a_email['a_email']; /* What is this??, do you have an array called $a_email */$a_answer['a_answer']; /* this also is doing nothing again, do you have an array called $a_answer?? */[/code]In your query you are trying to use $a_answer and $a_email but I am not seeing where you set those, It looks like a typo on the $a_email and $a_answer variables they should be[code]$a_name=$_POST['a_name'];$a_email=$_POST['a_email']; $a_answer=$_POST['a_answer'];[/code] I would start there.[/quote] Link to comment https://forums.phpfreaks.com/topic/36137-solved-why-does-this-happen/#findComment-171908 Share on other sites More sharing options...
trecool999 Posted January 29, 2007 Author Share Posted January 29, 2007 Hey, it works! Thanks so much! :D :-*Finally!You don't know how much I owe you for that... :P Link to comment https://forums.phpfreaks.com/topic/36137-solved-why-does-this-happen/#findComment-171910 Share on other sites More sharing options...
ShogunWarrior Posted January 29, 2007 Share Posted January 29, 2007 You should really protect your SQL queries from injection using mysql_escape_string.After viewing the code you provided I could make your code return different fields or insert different data by inputting SQL. Link to comment https://forums.phpfreaks.com/topic/36137-solved-why-does-this-happen/#findComment-171921 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.