Jump to content
xisto Community
nickmealey

I Want Learn How To Make A Guestbook In Php And Mysql - I want to learn how

Recommended Posts

Ok, I'm just now learning PHP and mySql. I really want there to be a place on my site where the users can post on my site. Is this easy? I would be like super happy if somone gave my instructions on how to make the database, and script it in PHP. Let me try not to confuse you, I want a place where my visitor can simply post articles, not a forum, just post. Thanks in advance.
nickmealey

Share this post


Link to post
Share on other sites

Hi,

I created months ago a guestbook PHP/MySQL script. It ist simple but i think that it is beauty also because of that. I use also Javascript to validate contributions.

The SQL query to create the table:

CREATE TABLE `guestbook` (`id` int(5) NOT NULL auto_increment,`name` text NOT NULL,`email` varchar(30) NOT NULL default '',`content` text NOT NULL,PRIMARY KEY  (`id`))

This is the primary file PHP file of the guestbook:

<?php echo '<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"'.'?>'; ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"   "http://forums.xisto.com/no_longer_exists/;  <head>    <title>GuestBook</title>    <link rel="stylesheet" type="text/css" href="./guestbook.css"/>    <script type="text/javascript">      <!--      function open_new_window_new(url) {      new_window = window.open(url,'window_name','toolbar=0,menubar=0,resizable=0,dependent=0,status=0,width=495,height=400,left=25,top=25')      }      -->    </script></head><body>    <h1><a href="../index.html">Home</a> Guestbook</h1>    <div><a href="javascript:open_new_window_new('./form_new.html');"><img src="edit.gif" width="18" height="13" alt="write"/>Write a message.</a></div>    <hr class="separator"></hr>    <?php    $sql_db     = "dabase_name";    $sql_user   = "mysql_user";    $sql_pass   = "mysql_pwd";    $sql_table  = "guestbook";        if(isset($pos)==0)        $pos=0; // start position for the page indexer    $count=3; // number of displayed items per page        mysql_connect("localhost", $sql_user, $sql_pass);    mysql_select_db($sql_db);    $result = mysql_query("select MAX(id) from ".$sql_table.";");    $data = mysql_fetch_assoc($result);    $size = $data['MAX(id)'];    $result = mysql_query("SELECT * FROM ".$sql_table." where id<=".($size-$pos)." order by id desc;");    if($result) {        $i=0;        while(($data = mysql_fetch_assoc($result)) && ($i < $count)) {        ?>            <?php            print "<div>"."\n";            print "<div><strong>Name</strong>: ". $data['name']."</div>\n";            if(isset($data['email'])) {                print "<div><strong>Email</strong>:      <a href=\"mailto:" . $data['email'] . "\">" . $data['email'] . "</a></div>\n";            }            print "<div><strong>Comments:</strong> <div>".$data['content']."</div></div>\n";             print "</div>"."\n";            ?>            <hr></hr>            <?php            $i++;        }    }    mysql_close();            print "<div class=\"center\">Pages: ";            for($j=0; $j<$size; $j++)                if(!($j % $count))                    print "<a href=\"book.php?pos=".$j."\">".(($j/$count)+1)."</a>\n";            print " - Total number of entries: ".$size." - Ir: \n";            if ($pos > 0)                print "<a href=\"book.php?pos=".($pos-$count)."\">Back</a>\n";            if($size > $pos+$count)                print "<a href=\"book.php?pos=".($pos+$count)."\">Next</a>\n";            ?>   </div></body></html>
Here it is the form_new.html code that it is called when the "write message" link is pressed:

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://forums.xisto.com/no_longer_exists/;<head>	<title>New</title>	<link rel="stylesheet" type="text/css" href="./form.css"/>	<script type="text/javascript">	<!--		function chkreply()  {  	if(document.formular.name.value == "")  	{    alert("Please enter a name!");    document.formular.name.focus();    return false;  	}  	if(document.formular.content.value == "")  	{    alert("The content field is empty!");    document.formular.content.focus();    return false;  	}  	var chkZ = 1;  	for(i=0;i<document.formular.name.value.length;++i)    if(document.formular.name.value.charAt(i) > "0" && document.formular.name.value.charAt(i) < "9")    	chkZ = -1;  	if(chkZ == -1)  	{    alert("Numbers are not admited in the name field");    document.formular.name.focus();    return false;  	}  	if((document.formular.name.value.indexOf('@') != -1) || (document.formular.name.value.indexOf('>') != -1) || (document.formular.name.value.indexOf('<') != -1) || (document.formular.name.value.indexOf('.') != -1) || (document.formular.name.value.indexOf(':') != -1) || (document.formular.name.value.indexOf(';') != -1) || (document.formular.name.value.indexOf('/') != -1) || (document.formular.name.value.indexOf('\\') != -1))  	{    alert("The simbols (@ > < .; : /) are not allowed in the name field");    document.formular.name.focus();    return false;  	}  	if((document.formular.email.value.indexOf('>') != -1) || (document.formular.email.value.indexOf('<') != -1) || (document.formular.email.value.indexOf('/') != -1) || (document.formular.email.value.indexOf('\\') != -1))  	{    alert("The simbols (> < /) are not allowed in the email field");    document.formular.email.focus();    return false;  	}  }	//-->	</script></head><body>    <form name="formular" action="./action_new.php" method="post" onsubmit="return chkreply()">	<div>  <table border="0\" cellpadding="3" cellspacing="0">  	<tr>    <td class="description">Name:</td>    <td><input name="name" type="text" class="field" size="30" maxlength="30" /></td>  	</tr>  	<tr>    <td class="description">*Email: </td>    <td><input name="email" type="text" class="field" size="30" maxlength="30" /></td>  	</tr>  	<tr>    <td class="description" valign="top">Content:</td>    <td><textarea name="content" class="Area" rows="10" cols="50"></textarea></td>  	</tr>  	<tr>    <td></td>    <td>    	<input type="submit" class="Button" value="Submit" />    	<input type="reset" class="Button" value="Erase" />    </td>  	</tr>  </table>	</div>	</form></body></html>

Next action_new.php insert the post into the database.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://forums.xisto.com/no_longer_exists/;<head>	<title>Data Transfer</title></head><body>	<div>	<?php     $sql_db = "Database_name";    $sql_user = "mysql_user";    $sql_pass = "mysql_pwd";	// We connect to the mysql and select the database    $name = $_POST['name'];    $email = $_POST['email'];     $content = $_POST['content'];	$link = mysql_connect("localhost",$sql_user,$sql_pass) or die("Could not connect<br/>");  	mysql_select_db($sql_db) or die("Could not select database<br/>");    mysql_query("INSERT INTO guestbook VALUES (0, '$name', '$email', '$content')");    mysql_close();    ?>    </div>    Thanks for your contribution    <a href="javascript:window.close()">close</a></body></html>[CODE]And to finish both CSS files:[CODE]body{   background: #2E466E;   text-align : justify;    font-size : 10pt;    font-family : verdana, arial, helvetica, sans-serif;    color : #DDDDDD;    margin-right:  20%;}h1{   font-size : 18pt;    font-family : verdana, arial, helvetica, sans-serif;    text-align : center;}a:visited {   background: #2E466E;   text-decoration : none;    color :#FFA500;}a:link {   background: #2E466E;   text-decoration : none;    color :#FFA500;}a:hover {   background: #2E466E;   color :#FFA500;   text-decoration : underline; }a:active {   background: #2E466E;   text-decoration : none;    color :#FF8C00;}img{   border: none;}.separator {    color: white;    border-style: solid;}.center {    text-align : center;}.field {    width : 80px;}
And form.css:

form {    padding:20px;}td, input, select, textarea {    font-size:13px;    font-family:Verdana,sans-serif;    font-weight:bold;}input, select, textarea {    color:#0000CC;}.Area, .field {    background-color:#ffffff;    width:300px;    border:1px solid #003366;}.description {     text-align:right;}.Radio   {   border:1px;}.title_name {    font-size:16px;    color:#0000CC;}

 

If you want to see it working in the real life:

La Ponderosa Guestbook

 

If you have anydoubt do not heasite to ask. You are welcome

Share this post


Link to post
Share on other sites

Thanks, like a ton! I really liked that, easy, got to the point, and it worked! I changed it to fit my site, but...hey. Thanks!

Share this post


Link to post
Share on other sites

Thanks, like a ton! I really liked that, easy, got to the point, and it worked! I changed it to fit my site, but...hey. Thanks!

69147[/snapback]


It is nice to helpful. :-) Can you post a link to your guestbook. I would like to take a look to your changes.

Share this post


Link to post
Share on other sites

I have a Xisto host. Where do I type the SQL query? Becuase I've been looking for something like this for a while.

69409[/snapback]


You log in CPanel, go to MySQL databases and there you can create MySQL users and grant permision to them. Then go to PHPAdmin. At the bottom of the "MySQL databases" you will find a link to enter PHPAdmin. There you can create databases, tables and run queries.

Good luck!

Share this post


Link to post
Share on other sites

You log in CPanel, go to MySQL databases and there you can create MySQL users and grant permision to them. Then go to PHPAdmin. At the bottom of the "MySQL databases" you will find a link to enter PHPAdmin. There you can create databases, tables and run queries.

Good luck!

69463[/snapback]


Addition, or you will create setup file.

for example..

 

$sql = "CREATE TABLE `guestbook` (`id` int(5) NOT NULL auto_increment,`name` text NOT NULL,`email` varchar(30) NOT NULL default '',`content` text NOT NULL,PRIMARY KEY  (`id`))";$result = mysql_db_query($database_name, $sql);if ($result) {   echo "Setup succesful!!";}else {   echo "Setup fail...";}

Share this post


Link to post
Share on other sites

Hi i want to create an install file for one of my scripts, the above code looks great but can someone repost with the connection code included in it also? Im really not very familiar with sql as most of the scripts i have created are using flatfile db but i have a very nice wap top list script an i must make a nice install for it ;-) thanks in advancePlease can anyone repost the install file code with mysql connection code included? Sorry its confusing me =[

Share this post


Link to post
Share on other sites

I have a Xisto host. Where do I type the SQL query? Becuase I've been looking for something like this for a while.

Just create a new empty php page, type in the sql query, (within the php tags of course). If you do have phpmyadmin, you could do it without code. It does all the coding for you while you see a nice graphical representation of your table. :)

You log in CPanel, go to MySQL databases and there you can create MySQL users and grant permision to them. Then go to PHPAdmin. At the bottom of the "MySQL databases" you will find a link to enter PHPAdmin. There you can create databases, tables and run queries. Good luck!


...I dont understand the meaning of your post. If he has phpmyadmin he either wouldn't ask how to insert sql querys or he dosn't know how to use phpmyadmin. The purpose of phpmyadmin is so YOU DON'T have to type up your own sql queries. phpmyadmin only has that query box for a few rare cases. You should either explain that you can type up the sql query in a php page, or taht he can do it easily without even worrying about the code.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | 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.