Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

PHP Guestbook


  • Please log in to reply
13 replies to this topic

#1 hoku_2000 _99

hoku_2000 _99

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 11 October 2011 - 07:46 PM

Creating a simple PHP guestbook

I am trying to re-create the guestbook example in the link, but just with Guest (being the ID), name, comment, and posted. This is my first time making a guestbook and its going to be hosted. I had someone suggest to me to make a database and then create the guestbook, but like I've said this is my first time making a guestbook with a database.
  • 0

#2 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 12 October 2011 - 05:31 AM

What's the question? I mean, it looks like a good project to work through, but have you started working on it? Are you stuck on anything?
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#3 hoku_2000 _99

hoku_2000 _99

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 12 October 2011 - 07:12 PM

I am using a free host (000webhost.com) and its my first time hosting an actual project just because I am trying to learn some new things. No, I havent started working on it yet. I am going to re-create the one in the link first and test it and run on my host to see if it works, then I am going to make changes to it.

My question is that, on my free host, they have MySQL and phpMyAdmin. Do I have to use one or both (MySQL and phpMyAdmin) to make a database and a table? And how do I link my database and table to the php? I noticed in the example a few things like username and password.
  • 0

#4 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 October 2011 - 04:45 AM

MySQL is the database engine itself. phpMyAdmin allows you to define what databases/tables/users your database will have for your PHP application to use.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#5 hoku_2000 _99

hoku_2000 _99

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 13 October 2011 - 04:47 PM

This is my output of what got when I did MySQL and phpMyAdmin? So far so good?

$mysql_host = "";
$mysql_database = "";
$mysql_user = "";
$mysql_password = "mypassword";


CREATE TABLE `a5143936_guest`.`guestbook` (
`id` INT( 4 ) NOT NULL AUTO_INCREMENT ,
`name` VARCHAR( 65 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL ,
`email` VARCHAR( 65 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL ,
`comment` VARCHAR( 200 ) CHARACTER SET utf8 COLLATE utf8_bin NOT NULL ,
`datetime` DATETIME NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_bin 




Edited by hoku_2000 _99, 13 October 2011 - 06:30 PM.

  • 0

#6 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 13 October 2011 - 06:28 PM

That doesn't make sense to me. Not sure if it's my poor memory of phpMyAdmin, but where did that come from? Also, you should NOT be posting login credentials for your database on a forum.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#7 hoku_2000 _99

hoku_2000 _99

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 13 October 2011 - 06:33 PM

Got it. I just edited my last post. Where I got the code from was what I built using MySQL and phpMyAdmin. I know it looked weird at first to me, but like I said its been awhile since I've done any SQL, not too sure if things have changed or I built it wrong or if I added extra things to it that I dont need.
  • 0

#8 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 14 October 2011 - 10:03 AM

You've posted two separate things. The first part is how you will connect to the database. The second part creates a table in the database. One goes in your PHP function, the other gets run once in phpMyAdmin.
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#9 hoku_2000 _99

hoku_2000 _99

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 14 October 2011 - 11:36 AM

So the second part stays in the phpMyAdmin? Here is what I got:

guestbook.php

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><strong>Test Sign Guestbook </strong></td> </tr> </table> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form id="form1" name="form1" method="post" action="addguestbook.php"> <td> <table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td width="117">Name</td> <td width="14">:</td> <td width="357"><input name="name" type="text" id="name" size="40" /></td> </tr> <tr> <td>Email</td> <td>:</td> <td><input name="email" type="text" id="email" size="40" /></td> </tr> <tr> <td valign="top">Comment</td> <td valign="top">:</td> <td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td> </tr> <tr> <td> </td> <td> </td> <td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td> </tr> </table> </td> </form> </tr> </table> <table width="400" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td> </tr> </table>


addguestbook.php

<?php $mysql_host = ""; $mysql_database = ""; $mysql_user = ""; $mysql_password = "mypassword";  $db_name="guest"; // Database name $tbl_name="guestbook"; // Table name  // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server "); mysql_select_db("$db_name")or die("cannot select DB");  $datetime=date("y-m-d h:i:s"); //date time  $sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')"; $result=mysql_query($sql);  //check if query successful if($result){ echo "Successful"; echo "<BR>"; echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page }  else { echo "ERROR"; }  mysql_close(); ?>


viewguestbook.php

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0"> <tr> <td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td> </tr> </table> <br>  <?php  $mysql_host = "mysql17.000webhost.com"; $mysql_database = "a5143936_guest"; $mysql_user = "a5143936_starr"; $mysql_password = "mypassword";  $db_name="guest"; // Database name $tbl_name="guestbook"; // Table name  // Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server "); mysql_select_db("$db_name")or die("cannot select DB");  $sql="SELECT * FROM $tbl_name"; $result=mysql_query($sql);  while($rows=mysql_fetch_array($result)){ ?> <table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td>ID</td> <td>:</td> <td><? echo $rows['id']; ?></td> </tr> <tr> <td width="117">Name</td> <td width="14">:</td> <td width="357"><? echo $rows['name']; ?></td> </tr> <tr> <td>Email</td> <td>:</td> <td><? echo $rows['email']; ?></td> </tr> <tr> <td valign="top">Comment</td> <td valign="top">:</td> <td><? echo $rows['comment']; ?></td> </tr> <tr> <td valign="top">Date/Time </td> <td valign="top">:</td> <td><? echo $rows['datetime']; ?></td> </tr> </table></td> </tr> </table> <BR> <? } mysql_close(); //close database ?>


But I think now I may have a problem with this part:



// Connect to server and select database. mysql_connect("$host", "$username", "$password")or die("cannot connect server "); mysql_select_db("$db_name")or die("cannot select DB");

I think I might have to change $host to $mysql_host? Along with username, and password? in that portion of code.
  • 0

#10 hoku_2000 _99

hoku_2000 _99

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 14 October 2011 - 03:35 PM

I just tested and I get Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'a5143936'@'localhost' (using password: NO) in /home/a5143936/public_html/addguestbook.php on line 11

I also get the error message: cannot connect to server

---------- Post added at 06:13 PM ---------- Previous post was at 05:58 PM ----------

For the error I just posted, I figured out I had an unwanted space, but now I get the error: cannot select DB.

---------- Post added at 06:35 PM ---------- Previous post was at 06:13 PM ----------

I fixed that error. Everything is like I want, but now when I run it by actually putting my name, email address, and comment, those fields remain blank. Here is my updated code:


<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>Test Sign Guestbook </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addguestbook.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name="name" type="text" id="name" size="40" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="40" /></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
</tr>
</table>

<?php
$mysql_host = "";
$mysql_database = "";
$mysql_user = "";
$mysql_password = "mypassword";

$db_name="guest"; // Database name
$tbl_name="guestbook"; // Table name

// Connect to server and select database.
mysql_connect("$mysql_host", "$mysql_user", "$mysql_password")or die("cannot connect server ");
mysql_select_db("$mysql_database")or die("cannot select DB");

$datetime=date("y-m-d h:i:s"); //date time

$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);

//check if query successful
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
}

else {
echo "ERROR";
}

mysql_close();
?>


<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td>
</tr>
</table>
<br>

<?php

$mysql_host = "";
$mysql_database = "";
$mysql_user = "";
$mysql_password = "mypassword";

$db_name="guest"; // Database name
$tbl_name="guestbook"; // Table name

// Connect to server and select database.
mysql_connect("$mysql_host", "$mysql_user", "$mysql_password")or die("cannot connect server ");
mysql_select_db("$mysql_database")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><? echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><? echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?
}
mysql_close(); //close database
?>

  • 0

#11 John

John

    CC Mentor

  • Moderator
  • 4450 posts
  • Location:New York, NY

Posted 14 October 2011 - 04:47 PM

You should be using the $_POST superglobal - and be sure to sanitize it before you insert it into the database.
  • 0

#12 hoku_2000 _99

hoku_2000 _99

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 64 posts

Posted 14 October 2011 - 06:09 PM

I got it up and running, but just in case to make sure that I did everything right heres my code:

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>Test Sign Guestbook </strong></td>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form1" name="form1" method="post" action="addguestbook.php">
<td>
<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><input name="name" type="text" id="name" size="40" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="email" type="text" id="email" size="40" /></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><textarea name="comment" cols="40" rows="3" id="comment"></textarea></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong><a href="viewguestbook.php">View Guestbook</a> </strong></td>
</tr>
</table>

<?php
$mysql_host = "myhost";
$mysql_database = "guest";
$mysql_user = "myusername";
$mysql_password = "mypassword";


$name    = $_POST['name'];
$email    = $_POST['email'];
$comment= $_POST['comment'];


$comment  = stripslashes($comment);
$email  = stripslashes($email);
$name = stripslashes($name);
$comment = str_replace ("<","<",$comment);
$comment = str_replace ("\n","<br>",$comment);

$email = str_replace ("<","<",$email);
$email = str_replace ("\n","<br>",$email);
$name = str_replace ("<","<",$name);
$name = str_replace ("\n","<br>",$name);
 

$db_name="guest"; // Database name
$tbl_name="guestbook"; // Table name

// Connect to server and select database.
mysql_connect("$mysql_host", "$mysql_user", "$mysql_password")or die("cannot connect server ");
mysql_select_db("$mysql_database")or die("cannot select DB");

if(empty($email) || empty($name) || empty($comment)) {
    echo "<h3>Sorry all fields are required</h3>";
} else {

$datetime=date("y-m-d H:i:s"); //date time

$sql="INSERT INTO $tbl_name(name, email, comment, datetime)VALUES('$name', '$email', '$comment', '$datetime')";
$result=mysql_query($sql);

//check if query successful
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='viewguestbook.php'>View guestbook</a>"; // link to view guestbook page
}

else {
echo "ERROR";
}

mysql_close();
}
?>

<table width="400" border="0" align="center" cellpadding="3" cellspacing="0">
<tr>
<td><strong>View Guestbook | <a href="guestbook.php">Sign Guestbook</a> </strong></td>
</tr>
</table>
<br>

<?php

$mysql_host = "myhost";
$mysql_database = "guest";
$mysql_user = "myusername";
$mysql_password = "mypassword";



$name    = $_POST['name'];
$email    = $_POST['email'];
$comment= $_POST['comment'];


$comment  = stripslashes($comment);
$email  = stripslashes($email);
$name = stripslashes($name);
$comment = str_replace ("<","<",$comment);
$comment = str_replace ("\n","<br>",$comment);

$email = str_replace ("<","<",$email);
$email = str_replace ("\n","<br>",$email);
$name = str_replace ("<","<",$name);
$name = str_replace ("\n","<br>",$name);


$db_name="guest"; // Database name
$tbl_name="guestbook"; // Table name

// Connect to server and select database.
mysql_connect("$mysql_host", "$mysql_user", "$mysql_password")or die("cannot connect server ");
mysql_select_db("$mysql_database")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);

while($rows=mysql_fetch_array($result)){
?>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td><table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td>ID</td>
<td>:</td>
<td><? echo $rows['id']; ?></td>
</tr>
<tr>
<td width="117">Name</td>
<td width="14">:</td>
<td width="357"><? echo $rows['name']; ?></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><? echo $rows['email']; ?></td>
</tr>
<tr>
<td valign="top">Comment</td>
<td valign="top">:</td>
<td><? echo $rows['comment']; ?></td>
</tr>
<tr>
<td valign="top">Date/Time </td>
<td valign="top">:</td>
<td><? echo $rows['datetime']; ?></td>
</tr>
</table></td>
</tr>
</table>
<BR>
<?
}
mysql_close(); //close database
?>


Also, now when I try to adust the date and time to ("M-D-Y H:i:s A") but when I change it, the time and date goes shows up like 0000-00-00 00:00:00.
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download