View Full Version : PHP MySQL guestbook
joso
August 28th, 2007, 05:53 PM
hi,
i've just followed a tutorial for a mysql guestbook as i was told my other one was far too outdated, thanks to binime for getting it working though. Anyhow, after completing the tut, and getting it looking just how i'd like, i've hit a few snags. Firstly adding 'checkfield' to stop the form being submitted if any of the the text fields aren't completed. it seems to show the error message even when the form is filled out in full :
http://www.unearth-online.net/home.php?p=guestbook
here's the code :
<?
include "gb-database.php";
$name = $_POST["name"];
$email = $_POST["email"];
$rating = $_POST["rating"];
$message = $_POST["message"];
$date = date("F j, Y");
$error = 0;
function checkfield($input)
{
if(!preg_match ("/^[A-z]+[0-9]+[A-z]+?.@!$%£&*()/",$input) && $input != "")
return TRUE;
else
return FALSE;
}
if (!checkfield($name));
(!checkfield($email));
(!checkfield($message));
(!checkfield($rating));
{
echo "One, or more of the fields was not filled out correctly...<br /><br />";
echo "<a href=\"home.php?p=guestbook\">Click here</a> to go back.";
$error = 1;
}
if($error == 0){
mysql_select_db($db, $connect);
mysql_query("INSERT INTO comments (name, email, rating, message, date)
VALUES ('$name', '$email', '$rating', '$message', '$date')");
echo "Thanks for using this guestbook.<br /><br /> <a href=\"home.php?p=guestbook\">Click here</a> to go back.";
}
mysql_close($connect);
?>
I'm also looking to have a new page start after 10 entries but im not sure where to start. if anyone could help me out that'd be great, thanks
:afro:
simplistik
August 28th, 2007, 06:35 PM
just curious... and I haven't really disected the code but shouldn't
if(!preg_match ...
be
if(preg_match ...
that's the first thing that pops out to me, cause to me it looks like you're saying
if $field doesn't match preg string, and isn't empty = true, so it looks to me like you're saying... HEY you can't be any of these values in my preg string which looks like that eliminates all alpha numeric values lol... and then you go and tell it that i can't be empty, but if it can't be alpha numeric how can you possibly type something in the field. I could be wrong though... as I said... that's what popped into my head right away when I looked at the code.
joso
August 28th, 2007, 08:13 PM
yea I tried that, no improvement though, like i say...i have no clue, lol
simplistik
August 29th, 2007, 12:24 AM
Yea, I think it's cause you're pregmatch is wrong try doing:
if ( preg_match ('|[0-9,A-Z,a-z,?.@!$%£&*()]|',$input) && $input != '' )
I'm also pretty sure you're if statement that contains all those checkfield functions isn't gonna work. But if it does, seems pretty inefficient to me. I personally would drop the errors into an array, makes it easier to work w/ and it's prettier to look at IMO.
Refined
August 29th, 2007, 04:29 AM
Hi joso,
simplistik is right, there is no way that conditional statement will work, you can't put command termination characters - ; - in the middle of a command!
if (!checkfield($name) || !checkfield($email) || !checkfield($message) || !checkfield($rating))
{
echo "One, or more of the fields was not filled out correctly...<br /><br />";
echo "<a href=\"home.php?p=guestbook\">Click here</a> to go back.";
$error = 1;
}
That should work. Try to turn on error reporting in your PHP configuration aswell to see if there are any other errors in the script.
Regards.
joso
August 29th, 2007, 05:02 AM
thanks Refined, it seems to be running fine with that code. i've also added the new code for 'preg match', thanks for explaining what the problem was, i'm still learning so it all helps a lot :)
do you have any tips or suggestions on a really simple way of displaying the number of posts and limiting the number of entries to 10 per page. if you take a look at the link again i've added where the text links to shift through the pages would be and a 'total entries' bit of text at the bottom, i used to know how to do it with flat file php stuff and .txt files but im not very good with mysql yet, i've added the files in a zip folder if anyone wants to look....
http://www.unearth-online.net/guestbook.zip
cheers!
Refined
August 30th, 2007, 04:42 AM
Hi joso,
Pagination is always annoying when you're still learning but after the first few tries the logic becomes common sense. It's basically adding a LIMIT 10 to the query of the page with an OFFSET for the rows to grab for the page you're on which gets set by links.
Unfortunately I don't have time to write a tutorial (at work) but try this one, seems fairly straight forward to follow.
http://www.tonymarston.net/php-mysql/pagination.html
If you want any more detailed explanations of any of his steps let me know.
Oh and a simple way to get the number of entries is to simply do this:
$resultCount = mysql_num_rows($result);
Where $result is your result set from the query.
Hope it helps.
joso
August 30th, 2007, 10:58 AM
thanks for that refined. i looked through the tutorial and set it up but it seemed to be hitting errors every few lines (probably my fault), so i had a look around for some other tutorials,i wasn't able to do that beforehand as i wasn't aware of the correct term 'pagination'.
anyhow, i came across one and managed to get it working to a certain extent.
http://www.unearth-online.net/home.php?p=guestbook
as you can see, the 'page 1 of 4' part seems to be correct as i've set it to display 5 entries per page but i'm obviously missing something out as its still just showing all the entries on one page. these are the scripts so far as i have it.
this one for the pages
include "gb-database.php";
mysql_select_db($db, $connect);
if (!(isset($pagenum)))
{
$pagenum = 1;
}
$data = mysql_query("SELECT * FROM comments") or die(mysql_error());
$rows = mysql_num_rows($data);
$page_rows = 5;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$max = 'limit ' .($pagenum - 1) * $page_rows .',' .$page_rows;
$data_p = mysql_query("SELECT * FROM comments $max") or die(mysql_error());
while($info = mysql_fetch_array( $data_p ))
{
Print $info['comments'];
echo "<br>";
}
echo "<p>";
echo " --Page $pagenum of $last-- <p>";
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=1'> <<-First</a> ";
echo " ";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=$previous'> <-Previous</a> ";
}
echo " ---- ";
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=$next'>Next -></a> ";
echo " ";
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=$last'>Last ->></a> ";
}
and this one prints the entry
include "gb-database.php";
mysql_select_db($db, $connect);
$result = mysql_query("SELECT * FROM comments order by id desc ");
while($row = mysql_fetch_array($result))
{
echo "<table width=\"400\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td align=\"left\" valign=\"top\">";
echo "<img src=\"images/layout/gb-border.jpg\" width=\"400\" height=\"5\" /></td></tr>";
echo "<tr><td align=\"left\" valign=\"top\"><table width=\"400\" border=\"0\" cellspacing=\"10\" cellpadding=\"0\"><tr>";
echo "<td colspan=\"2\" align=\"left\" valign=\"top\">" . $row['message'] . "</td>";
echo "</tr><tr>";
echo "<td width=\"110\" align=\"left\" valign=\"top\">Site Rating : " . $row['rating'] . "/10 </td>";
echo "<td width=\"260\" align=\"right\" valign=\"top\">Posted on " . $row['date'] . " by <a href=\"mailto:" . $row['email'] . "\">" . $row['name'] . "</a></td>";
echo "</tr></table></td></tr></table>";
}
?>
if you could break it down for me a bit and tell me what i'm doing wrong that would be great, cheers :puzzled:
Refined
August 31st, 2007, 06:34 AM
Hi joso,
Why have you got two pages? It looks like you have one for the pagination results and one for all of them?
As a quick answer, try this for line 27:
$max = 'limit '.$page_rows.','.($pagenum-1)*$page_rows;
I think you had the LIMIT parameters the wrong way around, the syntax is:
LIMIT limit,offset;
Hope it helps.
joso
August 31st, 2007, 08:52 AM
nah, its still not playing ball. I thought the two separate scripts might not be helping so i tidied it up abit but still nothing...
include "gb-database.php";
mysql_select_db($db, $connect);
if (!(isset($pagenum)))
{
$pagenum = 1;
}
$data = mysql_query("SELECT * FROM comments order by id desc ") or die(mysql_error());
$rows = mysql_num_rows($data);
$page_rows = 5;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$max = 'limit '.$page_rows.','.($pagenum-1) * $page_rows;
$data_p = mysql_query("SELECT * FROM comments $max") or die(mysql_error());
while($info = mysql_fetch_array( $data_p ))
{
Print $info['comments'];
}
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=1'><strong><<<</strong> </a> |";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=$previous'> <<<</a> ";
}
echo "| Page $pagenum of $last |";
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=$next'>>>></a> | ";
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=$last'><strong>>>></strong></a>";
}
echo "<br /><br />";
while($row = mysql_fetch_array($data))
{
echo "<table width=\"400\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td align=\"left\" valign=\"top\">";
echo "<img src=\"images/layout/gb-border.jpg\" width=\"400\" height=\"5\" /></td></tr>";
echo "<tr><td align=\"left\" valign=\"top\"><table width=\"400\" border=\"0\" cellspacing=\"10\" cellpadding=\"0\"><tr>";
echo "<td colspan=\"2\" align=\"left\" valign=\"top\">" . $row['message'] . "</td>";
echo "</tr><tr>";
echo "<td width=\"110\" align=\"left\" valign=\"top\">Site Rating : " . $row['rating'] . "/10 </td>";
echo "<td width=\"260\" align=\"right\" valign=\"top\">Posted on " . $row['date'] . " by <a href=\"mailto:" . $row['email'] . "\">" . $row['name'] . "</a></td>";
echo "</tr></table></td></tr></table>";
}
Refined
August 31st, 2007, 09:38 AM
Hi joso,
Change line 60 to:
while($row = mysql_fetch_array($data_p))
You have two result sets; (1) the total number of results (used for counting and setting pagination logic) and (2) the right subset of data for the requested page.
In your previous while loop you were looping over the first result set and therefore not looping through your desired subset of results but the complete results.
I wasn't sure why you had it in two files so I didn't even look at the second file!
You're also not seeing if 'pagenum' has been requested in the $_GET variables. Change lines 6 through 9 to:
if (!isset($_GET['pagenum']))
{
$pagenum = 1;
}
else
{
$pagenum = $_GET['pagenum'];
}
Hope it helps.
joso
August 31st, 2007, 09:52 AM
sorry about this man. lol
there's a definate improvement as its letting me scroll through the pages, but now no entries are showing :puzzled:
Refined
August 31st, 2007, 10:52 AM
Hi joso,
Sounds strange, can you post the exact code you're using now? Also, what are the field names you are trying to show?
Regards.
joso
August 31st, 2007, 11:07 AM
this is the script displaying the guestbook entries:
include "gb-database.php";
mysql_select_db($db, $connect);
if (!isset($_GET['pagenum']))
{
$pagenum = 1;
}
else
{
$pagenum = $_GET['pagenum'];
}
$data = mysql_query("SELECT * FROM comments order by id desc ") or die(mysql_error());
$rows = mysql_num_rows($data);
$page_rows = 5;
$last = ceil($rows/$page_rows);
if ($pagenum < 1)
{
$pagenum = 1;
}
elseif ($pagenum > $last)
{
$pagenum = $last;
}
$max = 'limit '.$page_rows.','.($pagenum-1) * $page_rows;
$data_p = mysql_query("SELECT * FROM comments $max") or die(mysql_error());
while($info = mysql_fetch_array( $data_p ))
{
Print $info['comments'];
}
if ($pagenum == 1)
{
}
else
{
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=1'><strong><<<</strong> </a> |";
$previous = $pagenum-1;
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=$previous'> <<<</a> ";
}
echo "| Page $pagenum of $last |";
if ($pagenum == $last)
{
}
else {
$next = $pagenum+1;
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=$next'>>>></a> | ";
echo " <a href='{$_SERVER['PHP_SELF']}?p=guestbook&pagenum=$last'><strong>>>></strong></a>";
}
echo "<br /><br />";
while($row = mysql_fetch_array($data_p))
{
echo "<table width=\"400\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td align=\"left\" valign=\"top\">";
echo "<img src=\"images/layout/gb-border.jpg\" width=\"400\" height=\"5\" /></td></tr>";
echo "<tr><td align=\"left\" valign=\"top\"><table width=\"400\" border=\"0\" cellspacing=\"10\" cellpadding=\"0\"><tr>";
echo "<td colspan=\"2\" align=\"left\" valign=\"top\">" . $row['message'] . "</td>";
echo "</tr><tr>";
echo "<td width=\"110\" align=\"left\" valign=\"top\">Site Rating : " . $row['rating'] . "/10 </td>";
echo "<td width=\"260\" align=\"right\" valign=\"top\">Posted on " . $row['date'] . " by <a href=\"mailto:" . $row['email'] . "\">" . $row['name'] . "</a></td>";
echo "</tr></table></td></tr></table>";
}
im trying to display the 'name', 'rating' and 'message' field, with a mailto: link on 'name' which uses the 'email' field.
don't know if you need it but heres the script used to add the entries
include "gb-database.php";
$name = $_POST["name"];
$email = $_POST["email"];
$rating = $_POST["rating"];
$message = $_POST["message"];
$date = date("F j, Y");
$error = 0;
function checkfield($input)
{
if ( preg_match ('|[0-9,A-Z,a-z,?.@!$%£&*()]|',$input) && $input != '' )
return TRUE;
else
return FALSE;
}
if (!checkfield($name) || !checkfield($email) || !checkfield($message) || !checkfield($rating))
{
echo "One, or more of the fields was not filled out correctly...<br /><br />";
echo "<a href=\"home.php?p=guestbook\">Click here</a> to go back.";
$error = 1;
}
if($error == 0){
mysql_select_db($db, $connect);
mysql_query("INSERT INTO comments (name, email, rating, message, date)
VALUES ('$name', '$email', '$rating', '$message', '$date')");
echo "Thanks for using this guestbook.<br /><br /> <a href=\"home.php?p=guestbook\">Click here</a> to go back.";
}
mysql_close($connect);
Refined
August 31st, 2007, 12:23 PM
Hi joso,
I missed this before. You've got two while loops looping over the same result set; this can't be done.
The PHP manual for mysql_fetch_array() states:
Returns an array that corresponds to the fetched row and moves the internal data pointer ahead.
This means that once you use mysql_fetch_array() to loop till the end of a result identifier the result identifer is left at the last result row. Put this before your second loop:
mysql_data_seek($data_p, 0);
That should reset the internal pointer on the result set back to the first row. It's a common pitfall, sorry I didn't see it sooner.
Hope it helps.
joso
August 31st, 2007, 12:52 PM
hi,
it seems to be giving me an error message
Warning: mysql_data_seek(): supplied argument is not a valid MySQL result resource in /home/uneasim2/public_html/guestbook.php on line 49
have i put that code in the wrong place or something? its just above the second mysql_fetch_array
echo "<br /><br />";
mysql_data_seek($data_p, 0);
while ($row = mysql_fetch_array($data_p))
{
echo "<table width=\"400\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\"><tr><td align=\"left\" valign=\"top\">";
echo "<img src=\"images/layout/gb-border.jpg\" width=\"400\" height=\"5\" /></td></tr>";
echo "<tr><td align=\"left\" valign=\"top\"><table width=\"400\" border=\"0\" cellspacing=\"10\" cellpadding=\"0\"><tr>";
echo "<td colspan=\"2\" align=\"left\" valign=\"top\">" . $row['message'] . "</td>";
echo "</tr><tr>";
echo "<td width=\"110\" align=\"left\" valign=\"top\">Site Rating : " . $row['rating'] . "/10 </td>";
echo "<td width=\"260\" align=\"right\" valign=\"top\">Posted on " . $row['date'] . " by <a href=\"mailto:" . $row['email'] . "\">" . $row['name'] . "</a></td>";
echo "</tr></table></td></tr></table>";
}
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.