01-24-2008, 01:26 AM
|
#12 (permalink)
|
|
The Contributor
Join Date: Jan 2008
Posts: 87
Thanks: 49
|
It works!
Code:
<?php
// Database information
/* Setting username and password */
$username="";
$password="";
$database="guestbook";
// MySQL Connections
mysql_connect($localhost, $username, $password) or die ('<strong>MySQL Error:</strong> '.mysql_error());
mysql_select_db($database) or die ('<strong>MySQL Error:</strong> '.mysql_error());
// Setting variables
$yourname = addslashes ($_POST['yourname']);
$email = urlencode(addslashes($_POST['email']));
$websiteurl = urlencode(addslashes($_POST['website']));
$comment = htmlentities(addslashes(strip_tags($_POST['comment'])));
// Creating the query to store the data entered in the database
$query = 'INSERT INTO entries SET name = "'.mysql_real_escape_string($yourname).'",
email = "'.mysql_real_escape_string($email).'",
website = "'.mysql_real_escape_string($website).'",
comment = "'.mysql_real_escape_string($comment).'"';
mysql_query($query);
// Display all of the saved records in the database
$guessbookentries = 'SELECT * FROM entries';
$result = mysql_query($guessbookentries);
// Return all rows from the above query
$num=mysql_numrows($result);
// Close connection
mysql_close();
?>
<html>
<head>
<title>Cookies</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" />
<script language="JavaScript" type="text/JavaScript">
<!--This will hide our code from old browsers
//Date
var mydate = new Date()
var year = mydate.getYear()
if (year < 1000)
year+=1900
var day = mydate.getDay()
var month = mydate.getMonth()+1
if (month<10)
month="0"+month
var daym = mydate.getDate()
if (daym<10)
daym = "0"+daym
// Time
</script>
<noscript>Your browser has disabled Javascript</noscript>
</head>
<body>
<h1 align="center"><a href="index.php">Sign Guest Book</a></h1>
<?
// Loop
$i=0;
while ($i < $num) {
$guestname = mysql_result($result, $i, "name");
$guestemail = mysql_result($result, $i, "email");
$guestwebsite = mysql_result($result, $i, "website");
$guestcomment = mysql_result($result, $i, "comment");
?>
<div align="center">
<table width="500" border="1" bordercolor="#0066FF" id="guestbooktable">
<tr>
<td width="58" rowspan="2"> </td>
<td width="426">
<script language="javascript" type="text/javascript">
// Date
document.write("<small>"+month+"/"+daym+"/"+year+"</small>")
</script> </td>
</tr>
<tr>
<td height="61"> <? echo $guestname; ?> <br /> <br /><? echo $guestcomment; ?></td>
</tr>
</table>
</div>
<?
$i++;
}
?>
</table>
</div>
</body>
</html>
The only problem I have is: When I enter a new entry into the guest book, it doesn't go to the top of the list, it goes to the second top for some reason.
I was also wondering if someone could explain this:
Code:
<?
// Loop
$i=0;
while ($i < $num) {
$guestname = mysql_result($result, $i, "name");
$guestemail = mysql_result($result, $i, "email");
$guestwebsite = mysql_result($result, $i, "website");
$guestcomment = mysql_result($result, $i, "comment");
?>
That's not making 100% sense to me at the moment, I pulled it from something I was doing a few days ago and it worked.
i=0 so while (0 is less than $num (the number of rows in the query) then display the next set of variables. I don't understand how "i" can ever become less than "$num".
|
|
|
|