PDA

View Full Version : Simple Guestbook/Blog Style PHP Script


Tombo
07-16-2003, 10:03 PM
Hullo,

I know next to nothing about php. I do, however, have unlimited webspace on a friend's server which can run PHP, as well as mySQL, Perl, etc.

Basically, here's what I'm looking to do. I've been running a blog on my previous websites, some I've updated manually, others using blogger.com, and other using windows-based programs. What I want is to create a blogging tool in my webspace that I can access from anywhere.

What I'm looking for is simple. Imagine a blank page with 2 form elements, a large text box and a one-line text box, and a submit button.

I enter data in the large text box, and in the one-line text box, then click submit.

What I entered becomes a blog on a new page. It will have the date as a header, then the time underneath it, then the data from the large text box, then the data from the one-line text box. This will be one post for the blog.

If a second post is created, the same method is used, and it is added to the page at the very top, before all previous blog entries.

The form will be simple. No password needed, no security. Just a simple type and click will put an entry on my blog page.

Once I've figured out that small amount, hopefully with help from you guys, I can expand it from there, making posts made on the same day appear under each other (so as not to have the date pop up with every post, just once that day).

Anyways, I'm looking for someone to lead me from here. Thanks guys.

pardicity3
07-16-2003, 10:26 PM
Are you wanting to utilize your available MySQL database or do you just want to write to the html file?

If you want to go the MySQL route, I can give you some of the basic coding:

You need to make the HTML page with a form, something like so:

<form method="post" action="submitLog.php">
Date:<br />
<input class="date" type="text" value name="date" /><br />
Title:<br />
<input class="title" type="text" name="title" /><br />
Message:<br />
<textarea class="message" name="message" cols="60" rows="10"></textarea><br /><br />
<input class="button" type="submit" value="Submit" />
</form>

Then make the file submitLog.php. It is actually relativley simple, and will probably look something like so:

<?php

//get values
$title = $_POST["title"];
$date = $_POST["date"];
$message = $_POST["message"];

//database settings
$username = "mysql_username";
$password = "mysql_password";
$url = "mysql_location";
$database = "database_name";

mysql_connect($url,$username,$password);
@mysql_select_db($database) or die("Couldn't select database");

$query="INSERT INTO weblog VALUES ('','$title','$date','$message')";
@mysql_query($query) or die("Couldn't query");

mysql_close();

?>

Then of course you will need to output the text to your page...which isn't too hard either:

<?php
//database settings
$username = "mysql_username";
$password = "mysql_password";
$url = "mysql_location";
$database = "database_name";

mysql_connect($url,$username,$password);
@mysql_select_db($database) or die("Could not select database");
$query = "SELECT * FROM weblog ORDER BY id DESC";
$result = mysql_query($query);
$num = mysql_numrows($result);

//For each row, fetch results into array
for($i=0; $i < $num; $i++) {
$r_array = mysql_fetch_row($result);
//clear slashes
$r_array[1] = stripslashes($r_array[1]);
$r_array[2] = stripslashes($r_array[2]);
$r_array[3] = stripslashes($r_array[3]);

?>
<div>
<span class="title"><?php echo $r_array[1]; ?></span>
<span class="date"><?php echo $r_array[2]; ?></span>
<p><?php echo $r_array[3]; ?></p>
</div>

<?php

}

mysql_close();

?>

Granted, that's not the most graceful code, nor is it probably going to work the first time. But, with just a little bit of tinkering, I think it should work...:rolleyes:

Tombo
07-18-2003, 05:14 AM
I'll have to look over all that code a number of times in order for it to make sense, and if it didn't work, I wouldn't know what tinkering to do. :P But hey, that's awesome for the help.

I'm assuming that example you gave me uses MySQL.. is it possible to do it with just PHP? I recall doing something similar back in a grade 11 computer class.. that was 3 years ago. I was hoping to see something similar, maybe it would ring a bell and I could go from there.

Spookster
07-18-2003, 05:33 AM
There are many ready made scripts available at

http://www.hotscripts.com/cgi-bin/search.cgi?bool=AND&query=blog&catid=2

Tombo
07-18-2003, 03:02 PM
I actually went through about 10 different scripts from that page already... and I tried b2, grey matter, moveable type, and scripts from freshmeat.net...

I just want to make my own, simple and fresh.