PHP scripts and programs for Beginner to Master

php programs, php ready to use scripts & functions, css tutorials, html, javascripts, ajax examples for Beginner to Master

This is a small script which will help you find the number of rows from a mysql database for your query.

First connect to your database, just create a database connection file.



so now you can use this $num_rows variable throughout your program for any manipulation.

Hi everyone,
Today i am going to show you how to Create Guestbook / Shoutbox in php mysql, in a very easy way and with simple code.
First thing that you might be thinking is what is a Guestbook or Shoutbox ?
These are the places where your website visitors or users can type their views or some information in a simple box.
In this program we are going to create a simple shoutbox / Guestbook which will allow your website visitor to enter some simple data about your website.

So lets start,
First we need to create the table which will store our data into database.
Create the table structure in your MySQL database as follows



After creating database and table you need to create a database connection file.
If you don't know how to create a database connection file then you should check this.
Specify the settings in database connection file.

Now we will be creating a form which will accept user's name and their shout or opinion.
So lets design it.
If the form is not posted then we will display the form and show the existing records from the database.



if (!isset($_POST["submit"]))
{
$query = "SELECT id, name, message, DATE_FORMAT(date, '%D %M, %Y') as newdate FROM guests ORDER BY id DESC LIMIT 0, 8";
$result = mysql_query($query);
?>

Post a message

















while ($row = mysql_fetch_object($result))
{
?>

message; ?>


Posted by name; ?> on newdate; ?>

}
?>

}
?>



So now you will see a form which will show you a textbox and a textarea for entering name and some shout. Just enter name and shout and click submit.

Now is the actual part of entering shout into database and come back to entry page.
So now we will be checking if the form is submitted then we will process the form.

For security we will be adding a simple spam comment prevention logic, we will not allow one ip to enter more than 5 shouts, comments to our form on same day.



$name = stripslashes($_POST['name']);
$message = stripslashes($_POST['msg']);


if(isset($_POST["submit"]))
{
// Add new entry to the database
$ip = $_SERVER['REMOTE_ADDR'];
$now = date("Y-m-d");

$query1 = "SELECT * FROM guests WHERE guestip='$ip' and date ='$now'";
$result1 = mysql_query($query1);
$cnt = mysql_num_rows($result1);

if($cnt<5)
{
$now = date("Y-m-d");
$query = "INSERT INTO guests (name, message, date, guestip) VALUES ('$name','$message','$now','$ip')";
$result = mysql_query($query) or die(mysql_error());
// go back to entry page
$referer = $_SERVER['HTTP_REFERER'];
header ("Location: $referer");
}
else
{
echo "Thank you for your comments. But you cannot submit more comments today as its a spam prevention mechanism to avoid spammers.";
echo "
If you are a spammer then just GO away.";
}
}



And you are done. Our shoutbox is ready to accept your website viewers shouts.

Now some interesting part to design your form and display with css






So you are ready with a nice looking shoutbox.
See here is the full source code for your testing


//code
<?php

// Database Settings

include "connect.php";

//connecting to databae and selecting database
mysql_connect($host, $user, $pass) OR die ("Error connecting to the server.");
mysql_select_db($db) OR die("Error selecting the database.");

$name = stripslashes($_POST['name']);
$message = stripslashes($_POST['msg']);


if(isset($_POST["submit"]))
{
// Add new entry to the database
$ip = $_SERVER['REMOTE_ADDR'];
$now = date("Y-m-d");

$query1 = "SELECT * FROM guests WHERE guestip='$ip' and date ='$now'";
$result1 = mysql_query($query1);
$cnt = mysql_num_rows($result1);

if($cnt<5)
{
$now = date("Y-m-d");
$query = "INSERT INTO guests (name, message, date, guestip) VALUES ('$name','$message','$now','$ip')";
$result = mysql_query($query) or die(mysql_error());
// go back to entry page
$referer = $_SERVER['HTTP_REFERER'];
header ("Location: $referer");
}
else
{
echo "Thank you for your comments. But you cannot submit more comments today as its a spam prevention mechanism to avoid spammers.";
echo "
If you are a spammer then just GO away.";
}
}

if (!isset($_POST["submit"]))
{
$query = "SELECT id, name, message, DATE_FORMAT(date, '%D %M, %Y') as newdate FROM guests ORDER BY id DESC LIMIT 0, 8";
$result = mysql_query($query);
?>

Post a message

















<?php
while ($row = mysql_fetch_object($result))
{
?>

<?php echo $row->message; ?>


Posted by <?php echo $row->name; ?> on <?php echo $row->newdate; ?>

<?php
}
?>

<?php
}
?>




Let us know whether you liked it or not. This is just a basic sample example for you.
Its not fully secure. If you know some simple function to prevent bots using this for data entry then let me know.

Hi,

Here is a simple function which can be used to parse the URL.

parse_url is a built in php function which can be used to parse url and get the type of request like whether its http or https or ftp, gives the domain name like www.google.com and path and the query.


In http://www.chaprak.com/articles.php?cat_id=6 this url if you execute parse_url function then it will show the following result.

scheme - http
url - www.chaprak.com
path - /articles.php
query - cat_id=6






For more information click here.