kleb Posted January 18, 2012 Share Posted January 18, 2012 please this code is not working,i want when users comment on a page it should refresh this is my code: <h3>Comments:</h3> <table> <tr class="row-a"> <td class="first"></td> <td><?php include"header.php"; $sql="SELECT post_content,post_by FROM post ORDER BY topicsID"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"<strong>{$row['post_by']}</strong>: {$row['post_content']}"."</br>"; } if(mysql_affected_rows()>0) { header("location:".$_SERVER['PHP_SELF']); } ?></td> </tr> </table> <h3>Post your comments here</h3> <form action='reply.php'method='post'> <textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea> <br /> Name:<input type="text"name="name"/> <input class="button" type="submit"name="submit"value="submit" /> </p> </form> <br /> [code] Quote Link to comment https://forums.phpfreaks.com/topic/255271-auto-refresh-problem/ Share on other sites More sharing options...
joel24 Posted January 18, 2012 Share Posted January 18, 2012 You're not updating or inserting anything for mysql_affected_rows() to return anything Quote Link to comment https://forums.phpfreaks.com/topic/255271-auto-refresh-problem/#findComment-1308807 Share on other sites More sharing options...
kleb Posted January 18, 2012 Author Share Posted January 18, 2012 ok.but how do it? Quote Link to comment https://forums.phpfreaks.com/topic/255271-auto-refresh-problem/#findComment-1308827 Share on other sites More sharing options...
solon Posted January 18, 2012 Share Posted January 18, 2012 Try the following (change the query to match your table and values <h3>Comments:</h3> <table> <tr class="row-a"> <td class="first"></td> <td> <?php include"header.php"; if($_POST['submit']) { $post = $_POST["comment"]; $by = $_POST["name"]; $add = "INSERT INTO `post` values(`post_content`,`post_by`,) ('$post','$by')"; $add_values = mysql_query($add) or die(mysql_error()); } $sql="SELECT post_content,post_by FROM post ORDER BY topicsID"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"<strong>{$row['post_by']}</strong>: {$row['post_content']}"."</br>"; } ?> </td> </tr> </table> <h3>Post your comments here</h3> <form action='<?php $_SERVER['PHP_SELF']; ?>' method='post'> <textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea> <br /> Name:<input type="text"name="name"/> <input class="button" type="submit"name="submit"value="submit" /> </p> </form> I hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/255271-auto-refresh-problem/#findComment-1308906 Share on other sites More sharing options...
kleb Posted January 19, 2012 Author Share Posted January 19, 2012 thanks so much SOLON but it gave me error concerning my id(it says index id undefined)i retrieved the id from the page that contains the topic although it worked fine before but since i changed the <form action> to <form action='$_SERVER['PHP_SELF']> it gave the error.please what can i do? thanks Quote Link to comment https://forums.phpfreaks.com/topic/255271-auto-refresh-problem/#findComment-1309190 Share on other sites More sharing options...
solon Posted January 19, 2012 Share Posted January 19, 2012 Try it now (there was a syntax error in the query): <h3>Comments:</h3> <table> <tr class="row-a"> <td class="first"></td> <td> <?php include"header.php"; if($_POST['submit']) { $post = $_POST["comment"]; $by = $_POST["name"]; $add = "INSERT INTO `post` values(`post_content`,`post_by`) ('$post','$by')"; $add_values = mysql_query($add) or die(mysql_error()); } $sql="SELECT post_content,post_by FROM post ORDER BY topicsID"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"<strong>{$row['post_by']}</strong>: {$row['post_content']}"."</br>"; } ?> </td> </tr> </table> <h3>Post your comments here</h3> <form action='<?php $_SERVER['PHP_SELF']; ?>' method='post'> <textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea> <br /> Name:<input type="text"name="name"/> <input class="button" type="submit"name="submit"value="submit" /> </p> </form> It should work now Quote Link to comment https://forums.phpfreaks.com/topic/255271-auto-refresh-problem/#findComment-1309191 Share on other sites More sharing options...
kleb Posted January 19, 2012 Author Share Posted January 19, 2012 You dont get me solon..let me post the full code to you: <span class="date"><?php include"header.php"; $topicid=$_GET['id']; $sql="SELECT*FROM topics WHERE topicsID=$topicid"; $result=mysql_query($sql) or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"{$row['topics_date']}"; } ?></span> </p> </div> <h3>Comments:</h3> <table> <tr class="row-a"> <td class="first"></td> <td> <?php include"header.php"; if(isset($_POST['submit'])) { $comment=mysql_real_escape_string(trim($_POST['comment'])); $name=mysql_real_escape_string(trim($_POST['name'])); if($comment!=='' && $name!=='') { $ins="INSERT INTO post(post_content,post_by)VALUES('$comment','$name')"; mysql_query($ins) or die(mysql_error()); $sql="SELECT post_content,post_by FROM post ORDER BY topicsID"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"<strong>{$row['post_by']}</strong>: {$row['post_content']}"."</br>"; } } } ?></td> </tr> </table> <h3>Post your comments here</h3> <form action='<?php $_SERVER['PHP_SELF'];?>''method='post'> <textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea> <br /> Name:<input type="text"name="name"/> <input class="button" type="submit"name="submit"value="submit" /> </p> </form> <br /> Quote Link to comment https://forums.phpfreaks.com/topic/255271-auto-refresh-problem/#findComment-1309216 Share on other sites More sharing options...
solon Posted January 19, 2012 Share Posted January 19, 2012 The error "Undefined index id" is referring to the $_GET['id'] variable. since you don't sent anything using the GET method it comes up us undefined. You can edit that and line and use the following: <span class="date"><?php include"header.php"; if(isset($_GET['id']) //CHECK IF GET ID IS SET ELSE GET IT FROM HIDDEN FIELD IN THE FORM { $topicid=$_GET['id']; } else { $topicid=$_POST['id']; } $sql="SELECT*FROM topics WHERE topicsID=$topicid"; $result=mysql_query($sql) or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"{$row['topics_date']}"; } ?></span> </p> </div> <h3>Comments:</h3> <table> <tr class="row-a"> <td class="first"></td> <td> <?php include"header.php"; if(isset($_POST['submit'])) { $comment=mysql_real_escape_string(trim($_POST['comment'])); $name=mysql_real_escape_string(trim($_POST['name'])); if($comment!=='' && $name!=='') { $ins="INSERT INTO post(post_content,post_by)VALUES('$comment','$name')"; mysql_query($ins) or die(mysql_error()); $sql="SELECT post_content,post_by FROM post ORDER BY topicsID"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"<strong>{$row['post_by']}</strong>: {$row['post_content']}"."</br>"; } } } ?></td> </tr> </table> <h3>Post your comments here</h3> <form action='<?php $_SERVER['PHP_SELF'];?>' method='post'> <textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea> <br /> Name:<input type="text"name="name"/> <input class="button" type="submit"name="submit"value="submit" /> <input type="hidden" name="id" value="<?php echo $topicid; ?>" /> //THIS LINE IS A HIDDEN FIELD SAVING THE TOPIC ID FOR USE AFTER THE FORM IS SUBMITED </p> </form> <br /> Quote Link to comment https://forums.phpfreaks.com/topic/255271-auto-refresh-problem/#findComment-1309220 Share on other sites More sharing options...
kleb Posted January 19, 2012 Author Share Posted January 19, 2012 Thanks it works fine but i discover now that every comment in my POST table get echoed out so i introduced a WHERE clause in the SELECT query but it will only echo out only one comment even though i comment again and again.this is the new code: <p>Posted by <a href=""><?php include"header.php"; if(isset($_GET['id'])) { $topicid=$_GET['id']; } else { $topicid=$_POST['id']; } $sql="SELECT*FROM topics WHERE topicsID=$topicid"; $result=mysql_query($sql) or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"{$row['topics_by']}</a></p>"; echo"<p> {$row['topics_subject']}"; echo" <p align='left'><img src='speakout_1.png'/>"; echo"{$row['topics_content']}"; echo"<p class='post-footer align-right'>"; echo"<a href='' class='comments'>"; session_start(); if(isset($_SESSION['views'])) $_SESSION['views']=$_SESSION['views']+1; else $_SESSION['views']=1; echo"Comments:".$_SESSION['views']; echo"</a>"; echo"<span class='date'>"; echo"{$row['topics_date']}"; } ?></span> </p> </div> <h3>Comments:</h3> <table> <tr class="row-a"> <td class="first"></td> <td> <?php include"header.php"; $sql="SELECT post_content,post_by FROM post WHERE postID='$topicid'"; $result=mysql_query($sql)or die(mysql_error()); while($row=mysql_fetch_array($result)) { echo"<strong>{$row['post_by']}</strong>: {$row['post_content']}"."</br>"; } if(isset($_POST['submit'])) { $comment=mysql_real_escape_string(trim($_POST['comment'])); $name=mysql_real_escape_string(trim($_POST['name'])); if($comment=='' && $name=='') { echo"You can not leave any field blank"; } else{ $ins="INSERT INTO post(post_content,post_by)VALUES('$comment','$name')"; mysql_query($ins) or die(mysql_error()); } } ?></td> </tr> </table> <h3>Post your comments here</h3> <form action='<?php $_SERVER['PHP_SELF']; ?>'method='post'> <textarea name="comment" id="content" style="width:400px;height:50px;background-color:#D0F18F;color:#000000;font:15px/20px cursive;scrollbar-base-color:#638E0D;"></textarea> <br /> Name:<input type="text"name="name"/> <input type="hidden"name="id"value="<?php echo $topicid;?>"/> <input class="button" type="submit"name="submit"value="submit" /> </p> Quote Link to comment https://forums.phpfreaks.com/topic/255271-auto-refresh-problem/#findComment-1309281 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.