Php Programming Code Examples
Php > Databases Code Examples
Ok, so heres the deal. This is a simple shout box that can display the comments
Ok, so heres the deal. This is a simple shout box that can display the comments
in your site usong inline frames with Mysql as a backend. Here goes :)
This is the strcture. Yourew gonna have to create 2 files and edit the file in which you want your shout box.
SQL:
CREATE TABLE `comtbl` (
`postID` INT NOT NULL AUTO_INCREMENT ,
`postTITLE` TEXT NOT NULL ,
`posterNAME` TEXT NOT NULL ,
`posterEMAIL` TEXT NOT NULL ,
`postTIME` TIMESTAMP NOT NULL ,
`postTXT` TEXT NOT NULL ,
PRIMARY KEY ( `postID` )
);
comment.php
<?
//Edit only the line below
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("comments");
$result = @mysql_query("SELECT * FROM comtbl ORDER BY postID DESC");
if (!$result) {
echo("<b>Error performing query: " . mysql_error() . "</b>");
exit();
}
while ($row = mysql_fetch_array($result) ) {
$msgTxt = $row["postTXT"];
$msgId = $row["postID"];
$SigName = $row["posterNAME"];
$SigDate = $row["postTIME"];
$msgTitle = $row["postTITLE"];
$url = $row["posterEMAIL"];
$yr = substr($SigDate, 2, 2);
$mo = substr($SigDate, 4, 2);
$da = substr($SigDate, 6, 2);
$hr = substr($SigDate, 8, 2);
$min = substr($SigDate, 10, 2);
if ($hr > "11") {
$x = "12";
$timetype = "PM";
$hr = $hr - 12;
}else{
$timetype = "AM";
}
if (!$url) {
$url = "#";
}else{
$stat = $url;
$url = "mailto:" . $url . "";
}
echo("<p><b>$msgTitle</b> $msgTxt<br><div align=right>
$hr:$min $timetype | $mo/$da/$yr | $msgId, <a href='$url'>$SigName</a></div></p>");
}
?>
commentadd.php
<?
$assume = $_POST['assume'];
$posterEMAIL = $_POST['postemail'];
$postTXT = $_POST['posttxt'];
$posterNAME = $_POST['poster'];
$postTITLE = $_POST['posttitle'];
if ($assume == "true") {
$dbcnx = mysql_connect("localhost", "username", "password");
mysql_select_db("comments");
$sql = "INSERT INTO comtbl SET posterNAME='$posterNAME', posterEMAIL='$posterEMAIL',
postTXT='$postTXT', postTITLE='$postTITLE'";
if (mysql_query($sql)) {
echo("Your comment has been added");
} else {
echo("Error adding entry: " . mysql_error() . "");
}
}
?>
Now in the file where you want this shout box to be displayed, use the following script where you want the box to appear
<iframe name="shoutbox" width="175" height="225" src="comment.php">
Your browser does not support inline frames or is currently configured not to display inline frames.</iframe>
<form action="commentadd.php" method=post>
<input type="text" name="poster" size="23" value="name"><br />
<input type="text" name="posttitle" size="23" value="title"><br />
<input type="text" name="postemail" size="23" value="[email protected]"><br />
<textarea cols=19 rows=3 name="posttxt" size=24 wrap="VIRTUAL">message</textarea><br />
<input type=hidden name=assume value=true>
<input type="submit" value="submit">
Best of luck with your new shout box ;)