Bojak Posted September 11, 2013 Share Posted September 11, 2013 <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <?php // Make a MySQL Connection mysql_connect("127.0.0.1", "root", "") or die(mysql_error()); mysql_select_db("users") or die(mysql_error()); $result = mysql_query("SELECT comments FROM users"); ?> </head> <body> <form name="input" action="" method="get"> <div id="commentsr"> <?php echo $result["comments"]; ?> </div> <textarea id="myMessage" name="comments" cols="30" rows="10" style="width: 400px; height: 83px;"></textarea> <input type="submit" value="Submit"> </form> </body> </html> i am building a comment system that refuses to put the comment in the db and echo the comment to a page. i tried putting the comment in a div. that could also be a wrong way of doing it. Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/ Share on other sites More sharing options...
Muddy_Funster Posted September 11, 2013 Share Posted September 11, 2013 mysql_query() returns a resource, not a dataset. You need to use mysql_fetch_assoc() to get the dataset. as for putting the comment into the database - there is nothing there that would perform an insert query to add the information, you're missing at least two thirds of the code you need to have to do what you want. Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449056 Share on other sites More sharing options...
Bojak Posted September 11, 2013 Author Share Posted September 11, 2013 would it at least echo from the db like that? Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449057 Share on other sites More sharing options...
Muddy_Funster Posted September 11, 2013 Share Posted September 11, 2013 almost, I have re-written your code. It uses a few functions you may not have come across - if you don't know them please learn them before using them in your own code - have a look at this, it should work: <?php // Make a MySQL Connection mysql_connect("127.0.0.1", "root", "") or die(mysql_error()); mysql_select_db("users") or die(mysql_error()); $result = mysql_query("SELECT comments FROM users"); while($row = mysql_fetch_assoc($result)){ if(!isset($dataString)){ $dataString = $row['comments']; } else{ $dataString .= "<br><br>{$row['comments']}"; } } $page = <<<PAGE <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <form name="input" action="" method="get"> <div id="commentsr"> $dataString </div> <textarea id="myMessage" name="comments" cols="30" rows="10" style="width: 400px; height: 83px;"></textarea> <input type="submit" value="Submit"> </form> </body> </html> PAGE; echo $page; ?> Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449059 Share on other sites More sharing options...
Bojak Posted September 11, 2013 Author Share Posted September 11, 2013 <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <?php // Make a MySQL Connection mysql_connect("127.0.0.1", "root", "") or die(mysql_error()); mysql_select_db("user") or die(mysql_error()); // Get all the data from the "example" table $result = mysql_query("SELECT comments FROM comments") or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Name</th> <th>Age</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['comments']; echo "</td><td>"; } echo "</table>"; ?> </head> <body> <form name="input" action="" method="get"> <textarea id="myMessage" name="comments" cols="30" rows="10" style="width: 400px; height: 83px;"></textarea> <input type="submit" value="Submit"> </form> </body> </html> Table 'users.comments' doesn't exist, i dont know why its saying this. thats not even my table name. my table name is users. Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449061 Share on other sites More sharing options...
Bojak Posted September 11, 2013 Author Share Posted September 11, 2013 Notice: Undefined variable: dataString in C:\wamp\www\comments.php on line 31 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\comments.php on line 6 Muddy_Funster i was given the above errors when trying your code. Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449063 Share on other sites More sharing options...
mac_gyver Posted September 11, 2013 Share Posted September 11, 2013 Table 'users.comments' doesn't exist, i dont know why its saying this. thats not even my table name. my table name is users. that's because the query in your code in reply #5 doesn't contain the correct table name. Notice: Undefined variable: dataString in C:\wamp\www\comments.php on line 31 Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\wamp\www\comments.php on line 6 Muddy_Funster i was given the above errors when trying your code. the or die(mysql_error()) logic that you do have in the code in reply #5 on the end of the mysql_query() statement, needs to be in your current code to get your code to tell you why the query is failing. Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449088 Share on other sites More sharing options...
Bojak Posted September 11, 2013 Author Share Posted September 11, 2013 <?php // Make a MySQL Connection mysql_connect("127.0.0.1", "root", "") or die(mysql_error()); mysql_select_db("users") or die(mysql_error()); $result = mysql_query("SELECT comments FROM users"); while($row = mysql_fetch_assoc($result)){ if(!isset($dataString)){ $dataString = $row['comments']; } else{ $dataString .= "<br><br>{$row['comments']}"; } } $page = <<<PAGE <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <form name="input" action="" method="get"> <div id="commentsr"> $dataString </div> <textarea id="myMessage" name="comments" cols="30" rows="10" style="width: 400px; height: 83px;"></textarea> <input type="submit" value="Submit"> </form> </body> </html> PAGE; echo $page; ?> mac_gyver where would I add it and what exactly would I add? Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449134 Share on other sites More sharing options...
mac_gyver Posted September 11, 2013 Share Posted September 11, 2013 it was already mentioned where - the or die(mysql_error()) logic that you do have in the code in reply #5 on the end of the mysql_query() statement, needs to be in your current code to get your code to tell you why the query is failing. Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449137 Share on other sites More sharing options...
Bojak Posted September 11, 2013 Author Share Posted September 11, 2013 (edited) <?php // Make a MySQL Connection mysql_connect("127.0.0.1", "root", "") or die(mysql_error()); mysql_select_db("users") or die(mysql_error()); $result = mysql_query("SELECT comments FROM users") or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ if(!isset($dataString)){ $dataString = $row['comments'] or die(mysql_error()); } else{ $dataString .= "<br><br>{$row['commens']}" or die(mysql_error()); } } // http://forums.phpfreaks.com/topic/282059-comment-system/ $page = <<<PAGE <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <form name="input" action="" method="get"> <div id="commentsr"> $dataString </div> <textarea id="myMessage" name="comments" cols="30" rows="10" style="width: 400px; height: 83px;"></textarea> <input type="submit" value="Submit"> </form> </body> </html> PAGE; echo $page; ?> I changed lines 8, 11, 5 but im getting a new error. ( ! ) Parse error: syntax error, unexpected ';' in C:\wamp\www\comments.php on line 5 Edited September 11, 2013 by Bojak Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449139 Share on other sites More sharing options...
Bojak Posted September 11, 2013 Author Share Posted September 11, 2013 I also don't have action in the form. does that matter? Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449145 Share on other sites More sharing options...
Bojak Posted September 12, 2013 Author Share Posted September 12, 2013 Notice: Undefined variable: dataString in C:\wamp\www\comments.php on line 31 <?php // Make a MySQL Connection mysql_connect("127.0.0.1", "root", "") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); $result = mysql_query("SELECT comments FROM users") or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ if(!isset($dataString)){ $dataString = $row['comments']; } else{ $dataString .= "<br><br>{$row['commens']}"; } } $page = <<<PAGE <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <form name="input" action="" method="get"> <div id="commentsr"> $dataString </div> <textarea id="myMessage" name="comments" cols="30" rows="10" style="width: 400px; height: 83px;"></textarea> <input type="submit" value="Submit"> </form> </body> </html> PAGE; echo $page; ?> Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449171 Share on other sites More sharing options...
Bojak Posted September 12, 2013 Author Share Posted September 12, 2013 oops sorry <?php // Make a MySQL Connection mysql_connect("127.0.0.1", "root", "") or die(mysql_error()); mysql_select_db("test") or die(mysql_error()); $result = mysql_query("SELECT comments FROM users") or die(mysql_error()); while($row = mysql_fetch_assoc($result)){ if(!isset($dataString)){ $dataString = $row['comments']; } else{ $dataString .= "<br><br>{$row['commens']}"; } } // http://forums.phpfreaks.com/topic/282059-comment-system/ $page = <<<PAGE <!DOCTYPE html> <html> <head> <title></title> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head> <body> <form name="input" action="" method="get"> <div id="commentsr"> $dataString </div> <textarea id="myMessage" name="comments" cols="30" rows="10" style="width: 400px; height: 83px;"></textarea> <input type="submit" value="Submit"> </form> </body> </html> PAGE; echo $page; ?> Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449172 Share on other sites More sharing options...
mac_gyver Posted September 12, 2013 Share Posted September 12, 2013 if the only php error is the Notice: Undefined variable: dataString ..., that means you found and fixed whatever was causing the query to fail with an error, but now there are no rows in your database table. Quote Link to comment https://forums.phpfreaks.com/topic/282059-comment-system/#findComment-1449203 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.