Jump to content

AdamCCFC

Members
  • Posts

    41
  • Joined

  • Last visited

    Never

Everything posted by AdamCCFC

  1. <?php /* user config variables */ $max_items = 5; /* max number of news items to show */ /* make database connection */ $db = mysql_connect ('localhost','root',''); mysql_select_db ('mydreamz',$db); function displayDreams($all = 0) { /* bring in two variables * $db is our database connection * $max_items is the maximum number * of news items we want to display */ global $db, $max_items; /* query for news items */ if ($all == 0) { /* this query is for up to $max_items */ $query = "SELECT dream_id,name,dream,tags FROM dream ORDER BY date_entered DESC LIMIT $max_items"; } else { /* this query will get all news */ $query = "SELECT dream_id,name,dream,tags FROM dream ORDER BY date_entered DESC"; } $result = mysql_query ($query); while ($row = mysql_fetch_assoc ($result)) { /* display news in a simple table */ echo "<TABLE border'1' width='300'"; /* place table row data in * easier to use variables. * Here we also make sure no * HTML tags, other than the * ones we want are displayed */ $date = $row['date_entered']; $name = htmlentities ($row['name']); $dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>')); $tags = $row['tags']; /* display the data */ echo "<TR><TD>$name<TD><TR>"; echo "<TR><TD>$dream<TD><TR>"; echo "<TR><TD>$tags<TD><TR>"; /* get number of comments */ $comment_query = "SELECT count(*) FROM dreams_comments WHERE dream_id={$row['dream_id']}"; $comment_result = mysql_query ($comment_query); $comment_row = mysql_fetch_row($comment_result); /* display number of comments with link */ echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}\"?action=show&id=$id\"{$row['dream_id']}\">Comments ($comment_row[0])</a></TD></TR>"; /* finish up table*/ echo "</TABLE>"; echo "</br>"; } /* if we aren't displaying all dream, * then give a link to do so */ if ($all == 0) { echo "<a href=\"{$_SERVER['PHP_SELF']}\"?action=all'\">'View all dreams'</a>"; } } function displayOneItem($id) { global $db; echo "<br>id=$id<br>"; /* query for item */ $query = "SELECT * FROM `dream` WHERE `dream_id` = '$id'"; $result = mysql_query ($query); /* if we get no results back, error out */ if (mysql_num_rows ($result) == 0) { echo "Bad Dream ID\n"; return; } $row = mysql_fetch_assoc($result); echo "<table border='1' width='300'>"; /* easier to read variables and * striping out tags */ $name = htmlentities ($row['name']); $dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>')); /* display the items */ echo "<TR><TD>$name</TD></TR>"; echo "<TR><TD>$dream</TD></TR>"; echo "<TR><TD>$tags</TD></TR>"; echo "</TABLE>"; echo "</br>"; /* now show the comments */ displayComments($id); } function displayComments($id) { /* bring db connection variable into scope */ global $db; echo "<br>id=$id<br>"; /* query for comments */ $query = "SELECT * FROM dreams_comments WHERE dream_id=$id"; $result = mysql_query ($query); echo "Comments:<BR><HR width='300'>"; /* display the all the comments */ while ($row = mysql_fetch_assoc ($result)) { echo "<TABLE border='1' width='300'"; $name = htmlentities ($row['name']); echo "<TR><TD>$name</TD></TR>"; $comment = strip_tags ($row['comment'], '<a><b><i><u>'); $comment = nl2br ($comment); echo "<TR><TD>$comment</TD></TR>"; echo "</TABLE<"; echo "</br>"; } /* add a form where users can enter new comments */ echo "<form id='addcomment' action=\"{$_SERVER['PHP_SELF']} action='addcomment&id=$id' method='POST'>"; echo "Name: <input type'text' width'30' name='name'></br>"; echo "Comment <textarea cols'40' rows='5' name='comment'></textarea></br>"; echo "input type'submit' name='submit' value='Add Comment'"; echo "</form>"; } function addComment($id) { global $db; echo "<br>id=$id<br>"; /* insert the comment */ $query = "INSERT INTO dreams_comments VALUES('',$id,'{$_POST['name']}','{$_POST['comment']}')"; mysql_query($query); echo "Comment entered. Thanks!</br>"; echo "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\">Back</a>"; } /* this is where the script decides what do do */ print_r($_GET) switch($_GET['action']) { case 'show': displayOneItem($_GET['id']); break; case 'all': displayDreams(1); break; case 'addcomment': addComment($_GET['id']); break; default: displayDreams(); } ?>
  2. Ok so when I do that I get an error message saying - Parse error: syntax error, unexpected T_SWITCH in C:\xampp\htdocs\mydreamz\comments.php on line 143 Line 143 then is switch($_GET['action']) {
  3. Yer sure, here it is - <?php /* user config variables */ $max_items = 5; /* max number of news items to show */ /* make database connection */ $db = mysql_connect ('localhost','root',''); mysql_select_db ('mydreamz',$db); function displayDreams($all = 0) { /* bring in two variables * $db is our database connection * $max_items is the maximum number * of news items we want to display */ global $db, $max_items; /* query for news items */ if ($all == 0) { /* this query is for up to $max_items */ $query = "SELECT dream_id,name,dream,tags FROM dream ORDER BY date_entered DESC LIMIT $max_items"; } else { /* this query will get all news */ $query = "SELECT dream_id,name,dream,tags FROM dream ORDER BY date_entered DESC"; } $result = mysql_query ($query); while ($row = mysql_fetch_assoc ($result)) { /* display news in a simple table */ echo "<TABLE border'1' width='300'"; /* place table row data in * easier to use variables. * Here we also make sure no * HTML tags, other than the * ones we want are displayed */ $date = $row['date_entered']; $name = htmlentities ($row['name']); $dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>')); $tags = $row['tags']; /* display the data */ echo "<TR><TD>$name<TD><TR>"; echo "<TR><TD>$dream<TD><TR>"; echo "<TR><TD>$tags<TD><TR>"; /* get number of comments */ $comment_query = "SELECT count(*) FROM dreams_comments WHERE dream_id={$row['dream_id']}"; $comment_result = mysql_query ($comment_query); $comment_row = mysql_fetch_row($comment_result); /* display number of comments with link */ echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}\"?action=show&id=$id\"{$row['dream_id']}\">Comments ($comment_row[0])</a></TD></TR>"; /* finish up table*/ echo "</TABLE>"; echo "</br>"; } /* if we aren't displaying all dream, * then give a link to do so */ if ($all == 0) { echo "<a href=\"{$_SERVER['PHP_SELF']}\"?action=all'\">'View all dreams'</a>"; } } function displayOneItem($id) { global $db; /* query for item */ $query = "SELECT * FROM `dream` WHERE `dream_id` = '$id'"; $result = mysql_query ($query); /* if we get no results back, error out */ if (mysql_num_rows ($result) == 0) { echo "Bad Dream ID\n"; return; } $row = mysql_fetch_assoc($result); echo "<table border='1' width='300'>"; /* easier to read variables and * striping out tags */ $name = htmlentities ($row['name']); $dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>')); /* display the items */ echo "<TR><TD>$name</TD></TR>"; echo "<TR><TD>$dream</TD></TR>"; echo "<TR><TD>$tags</TD></TR>"; echo "</TABLE>"; echo "</br>"; /* now show the comments */ displayComments($id); } function displayComments($id) { /* bring db connection variable into scope */ global $db; /* query for comments */ $query = "SELECT * FROM dreams_comments WHERE dream_id=$id"; $result = mysql_query ($query); echo "Comments:<BR><HR width='300'>"; /* display the all the comments */ while ($row = mysql_fetch_assoc ($result)) { echo "<TABLE border='1' width='300'"; $name = htmlentities ($row['name']); echo "<TR><TD>$name</TD></TR>"; $comment = strip_tags ($row['comment'], '<a><b><i><u>'); $comment = nl2br ($comment); echo "<TR><TD>$comment</TD></TR>"; echo "</TABLE<"; echo "</br>"; } /* add a form where users can enter new comments */ echo "<form id='addcomment' action=\"{$_SERVER['PHP_SELF']} action='addcomment&id=$id' method='POST'>"; echo "Name: <input type'text' width'30' name='name'></br>"; echo "Comment <textarea cols'40' rows='5' name='comment'></textarea></br>"; echo "input type'submit' name='submit' value='Add Comment'"; echo "</form>"; } function addComment($id) { global $db; /* insert the comment */ $query = "INSERT INTO dreams_comments VALUES('',$id,'{$_POST['name']}','{$_POST['comment']}')"; mysql_query($query); echo "Comment entered. Thanks!</br>"; echo "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\">Back</a>"; } /* this is where the script decides what do do */ switch($_GET['action']) { case 'show': displayOneItem($_GET['id']); break; case 'all': displayDreams(1); break; case 'addcomment': addComment($_GET['id']); break; default: displayDreams(); } ?> Sorry, old code first time lol!
  4. Nope didn't change anything :-\ I need it so when the link is clicked on the URL changes to pull the Dream and the comments from the database. But when I'm clicking on the links it just doesn't seem to be doing anything Just want to say thanks guys for helping too, it's greatly appreciated!!
  5. I changed the & to & through all the code and still doesn't work?
  6. Ohh wait there, it's displaying it now! No idea what I did/didn't do It displays the number of comments now, but when I click the link it doesn't take me to a page, showing the dream and comment?
  7. It gives me this error message Error SQL query: Documentation $query = "SELECT * FROM dream WHERE dream_id=$id"; MySQL said: Documentation #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '$query = "SELECT * FROM dream WHERE dream_id=$id"' at line 1
  8. Can anybody tell me why I am getting this warning? - Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\mydreamz\comments.php on line 71 Bad Dream ID The code for line 71 is - if (mysql_num_rows ($result) == 0) { The whole code is here - <?php /* user config variables */ $max_items = 5; /* max number of news items to show */ /* make database connection */ $db = mysql_connect ('localhost','root',''); mysql_select_db ('mydreamz',$db); function displayDreams($all = 0) { /* bring in two variables * $db is our database connection * $max_items is the maximum number * of news items we want to display */ global $db, $max_items; /* query for news items */ if ($all == 0) { /* this query is for up to $max_items */ $query = "SELECT dream_id,name,dream,tags FROM dream ORDER BY date_entered DESC LIMIT $max_items"; } else { /* this query will get all news */ $query = "SELECT dream_id,name,dream,tags FROM dream ORDER BY date_entered DESC"; } $result = mysql_query ($query); while ($row = mysql_fetch_assoc ($result)) { /* display news in a simple table */ echo "<TABLE border'1' width='300'"; /* place table row data in * easier to use variables. * Here we also make sure no * HTML tags, other than the * ones we want are displayed */ $date = $row['date_entered']; $name = htmlentities ($row['name']); $dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>')); $tags = $row['tags']; /* display the data */ echo "<TR><TD>$name<TD><TR>"; echo "<TR><TD>$dream<TD><TR>"; echo "<TR><TD>$tags<TD><TR>"; /* get number of comments */ $comment_query = "SELECT count(*) FROM dreams_comments WHERE dream_id={$row['dream_id']}"; $comment_result = mysql_query ($comment_query); $comment_row = mysql_fetch_row($comment_result); /* display number of comments with link */ echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}\"?action=show&id=\"{$row['dream_id']}\">'Comments'\"($comment_row[0])\"</a></TD></TR>"; /* finish up table*/ echo "</TABLE>"; echo "</br>"; } /* if we aren't displaying all dream, * then give a link to do so */ if ($all == 0) { echo "<a href=\"{$_SERVER['PHP_SELF']}\"?action=all'\">'View all dreams'</a>"; } } function displayOneItem($id) { global $db; /* query for item */ $query = "SELECT * FROM dream WHERE dream_id=$id"; $result = mysql_query ($query); /* if we get no results back, error out */ if (mysql_num_rows ($result) == 0) { echo "Bad Dream ID\n"; return; } $row = mysql_fetch_assoc($result); echo "<table border='1' width='300'>"; /* easier to read variables and * striping out tags */ $name = htmlentities ($row['name']); $dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>')); /* display the items */ echo "<TR><TD>$name</TD></TR>"; echo "<TR><TD>$dream</TD></TR>"; echo "<TR><TD>$tags</TD></TR>"; echo "</TABLE>"; echo "</br>"; /* now show the comments */ displayComments($id); } function displayComments($id) { /* bring db connection variable into scope */ global $db; /* query for comments */ $query = "SELECT * FROM dreams_comments WHERE dream_id=$id"; $result = mysql_query ($query); echo "Comments:<BR><HR width='300'>"; /* display the all the comments */ while ($row = mysql_fetch_assoc ($result)) { echo "<TABLE border='1' width='300'"; $name = htmlentities ($row['name']); echo "<TR><TD>$name</TD></TR>"; $comment = strip_tags ($row['comment'], '<a><b><i><u>'); $comment = nl2br ($comment); echo "<TR><TD>$comment</TD></TR>"; echo "</TABLE<"; echo "</br>"; } /* add a form where users can enter new comments */ echo "<form id='addcomment' action=\"{$_SERVER['PHP_SELF']} action='addcomment&id=$id' method='POST'>"; echo "Name: <input type'text' width'30' name='name'></br>"; echo "Comment <textarea cols'40' rows='5' name='comment'></textarea></br>"; echo "input type'submit' name='submit' value='Add Comment'"; echo "</form>"; } function addComment($id) { global $db; /* insert the comment */ $query = "INSERT INTO dreams_comments VALUES('',$id,'{$_POST['name']}','{$_POST['comment']}')"; mysql_query($query); echo "Comment entered. Thanks!</br>"; echo "<a href=\"{$_SERVER['PHP_SELF']}?action=show&id=$id\">Back</a>"; } /* this is where the script decides what do do */ switch($_GET['action']) { case 'show': displayOneItem($_GET['dream_id']); break; case 'all': displayDreams(1); break; case 'addcomment': addComment($_GET['dream_id']); break; default: displayDreams(); } ?> Thanks in advanced for any help, Adam
  9. Hey, I'm receiving a parse error on line 50! Here's the error message - Parse error: syntax error, unexpected '{', expecting ',' or ';' in C:\xampp\htdocs\mydreamz\comments.php on line 50 Here's line 50 echo "<TR><TD><a href="{$_SERVER['PHP_SELF']}"?action=show&id="{$row['dream_id']}">'Comments'"($comment_row[0])"</TD></TR>"; Can anyone show me where I'm going wrong? Thanks in advanced, Adam
  10. Yerr I was just about to edit it and say the warning is given on line 27 I shall try editing the query then, thanks!
  11. Hey guys, I'm making a website for my University project and have hit a brick wall. I want to be able to add comments to dreams that have been submitted to my database. I followed this tutorial here http://www.codewalkers.com/c/a/Database-Articles/PHPMySQL-News-with-Comments/1/ but as you can see from the title I am getting an error message. I'll show you the code I have <?php /* user config variables */ $max_items = 5; /* max number of news items to show */ /* make database connection */ $db = mysql_connect ('localhost','root',''); mysql_select_db ('dream',$db); function displayNews($all = 0) { /* bring in two variables * $db is our database connection * $max_items is the maximum number * of news items we want to display */ global $db, $max_items; /* query for news items */ if ($all == 0) { /* this query is for up to $max_items */ $query = "SELECT dream_id,name,dream,tags" . "FROM dream ORDER BY date_entered DESC LIMIT $max_items"; } else { /* this query will get all news */ $query = "SELECT dream_id,name,dream,tags" . "FROM dream ORDER BY date_entered DESC"; } $result = mysql_query ($query); while ($row = mysql_fetch_assoc ($result)) { /* display news in a simple table */ echo "<TABLE border=\"1\" width=\"300\">\n"; /* place table row data in * easier to use variables. * Here we also make sure no * HTML tags, other than the * ones we want are displayed */ $date = $row['date_entered']; $name = htmlentities ($row['name']); $dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>')); /* display the data */ echo "<TR><TD><b>$name</b> posted on $date</TD></TR>\n"; echo "<TR><TD>$dream</TD></TR>\n"; /* get number of comments */ $comment_query = "SELECT count(*) FROM dreams_comments " . "WHERE dream_id={$row['id']}"; $comment_result = mysql_query ($comment_query); $comment_row = mysql_fetch_row($comment_result); /* display number of comments with link */ echo "<TR><TD><a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id={$row['id']}\">Comments</a>" . "($comment_row[0]}</TD></TR>\n"; /* finish up table*/ echo "</TABLE>\n"; echo "<BR>\n"; } /* if we aren't displaying all dream, * then give a link to do so */ if ($all == 0) { echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=all\">View all dream</a>\n"; } } function displayOneItem($id) { global $db; /* query for item */ $query = "SELECT * FROM dream WHERE id=$id"; $result = mysql_query ($query); /* if we get no results back, error out */ if (mysql_num_rows ($result) == 0) { echo "Bad dream id\n"; return; } $row = mysql_fetch_assoc($result); echo "<TABLE border=\"1\" width=\"300\">\n"; /* easier to read variables and * striping out tags */ $name = htmlentities ($row['name']); $dream = nl2br (strip_tags ($row['dream'], '<a><b><i><u>')); /* display the items */ echo "<TR><TD><b>$name</b></TD></TR>\n"; echo "<TR><TD>$dream</TD></TR>\n"; echo "</TABLE>\n"; echo "<BR>\n"; /* now show the comments */ displayComments($id); } function displayComments($id) { /* bring db connection variable into scope */ global $db; /* query for comments */ $query = "SELECT * FROM dreams_comments WHERE dreams_id=$id"; $result = mysql_query ($query); echo "Comments:<BR><HR width=\"300\">\n"; /* display the all the comments */ while ($row = mysql_fetch_assoc ($result)) { echo "<TABLE border=\"1\" width=\"300\">\n"; $name = htmlentities ($row['name']); echo "<TR><TD><b>by: $name</b></TD></TR>\n"; $comment = strip_tags ($row['comment'], '<a><b><i><u>'); $comment = nl2br ($comment); echo "<TR><TD>$comment</TD></TR>\n"; echo "</TABLE>\n"; echo "<BR>\n"; } /* add a form where users can enter new comments */ echo "<HR width=\"300\">"; echo "<FORM action=\"{$_SERVER['PHP_SELF']}" . "?action=addcomment&id=$id\" method=POST>\n"; echo "Name: <input type=\"text\" " . "width=\"30\" name=\"name\"><BR>\n"; echo "<TEXTAREA cols=\"40\" rows=\"5\" " . "name=\"comment\"></TEXTAREA><BR>\n"; echo "<input type=\"submit\" name=\"submit\" " . "value=\"Add Comment\"\n"; echo "</FORM>\n"; } function addComment($id) { global $db; /* insert the comment */ $query = "INSERT INTO dreams_comments " . "VALUES('',$id,'{$_POST['name']}'," . "'{$_POST['comment']}')"; mysql_query($query); echo "Comment entered. Thanks!<BR>\n"; echo "<a href=\"{$_SERVER['PHP_SELF']}" . "?action=show&id=$id\">Back</a>\n"; } /* this is where the script decides what do do */ echo "<CENTER>\n"; switch($_GET['action']) { case 'show': displayOneItem($_GET['id']); break; case 'all': displayNews(1); break; case 'addcomment': addComment($_GET['id']); break; default: displayNews(); } echo "</CENTER>\n"; ?> Can anybody see why I am getting that warning?? Any help would be greatly appreciated. Thanks in advanced! Adam
×
×
  • 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.