PHP and MySQL combined to create a simple message this program

Source: Internet
Author: User
Tags exit empty ereg header insert php and php and mysql prev

Database structure: (Library name: LYB)
Table One: admin
Field: ID (int11) name (VARCHVR) password (VARCHVR)

Table II: LO
Field: ID (int11) Username (VARCHVR) Sex (VARCHVR) QQ (VARCHVR) Email (VARCHVR) info (text) IP (VARCHVR) submit_time (datetim E

1 conn.php (Connection database file)

<?php
mysql_connect ("localhost", "root", "");/Connection Database
mysql_select_db ("Lyb");//Select Database
?>

2 header.php (Common header file)

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "/>
<title>php and MySQL combined to create a simple message this program </title>
<link href= "Css.css" rel= "stylesheet" type= "Text/css"/>
<body>
<div id= "Head" >
<div id= "head_l" >
<ul>
<li><a href= "index.php" > I want to see message </a></li>
<li><a href= "post.php" > I want to publish </a></li>
<?php
Session_Start ();
if ($_session["key"]==1) {//if the obtained session is 1 then the admin item is displayed
?>
<li><a href= "adminexit.php" > Exit management </a></li>
?
}
Else
{
?>
<li><a href= "admin.php" > I want to manage </a></li>
?? >
</ul>
</div>
<div id= "Head_r" >php and MySQL combined to create a simple message this program </div>
</div>

3 footer.php (common bottom file)

<?php
$counterFile = "Conter.xml";
function Displaycounter ($counterFile) {
$fp = fopen ($counterFile, "RW");
$num = Fgets ($fp, 5);
$num + 1;
Print "<div id=\" footer\ > Simple message board You are the first "." $num "." A bit of a dull silver </div> ";
EXEC ("Rm-rf $counterFile");
EXEC ("echo $num > $counterFile");
}
if (!file_exists ($counterFile)) {
EXEC ("Echo 0 > $counterFile");
}
Displaycounter ($counterFile);
?>
</body>

4 index.php (Home)

<?php
Require_once ("conn.php");
Require_once ("header.php");
Session_Start ();
Start the paging code
$pagesize = 10;//Sets the number of display bars per page
$rs = mysql_query ("SELECT count (*) from lo");//Get total number of records, calculate total pages with
$myrow = Mysql_fetch_array ($RS);
$numrows = $myrow [0];//calculation total record
$pages = Intval ($numrows/$pagesize);
if ($numrows% $pagesize) $pages ++;//set Number of pages
if (Isset ($_get[' page '))
{
$page = intval ($_get[' page '));
}
Else
{
$page = 1;//Set as first page
}
$offset = $pagesize * ($page-1);//Calculate record offset
Pagination Code End
$sql = "Select Id,username,sex,qq,email,info,ip,date_format (submit_time, '%y%m month%d days%T ') from lo Order BY id DESC limit $ Offset, $pagesize ";//used Date-format format date format
$result = mysql_query ($sql);
$num = mysql_num_rows ($result);
if ($num >0) {
while ($row = Mysql_fetch_array ($result))
{
echo Print_r ($row);
if ($row [2]== "male")//This makes the gender the name you want ^_^
{
$sex = "Shuai pan";
}
Else
{
$sex = "Beauty";
}
?>
<div id= "Show" >
<p class= "num" > [<?= $row [0]] message <p>
<p class= "Unline" > Message person: <span class= "Blue" ><?= $row [1]?></span> Sex: <?= $sex?> Message time: = $row [7]?> if ($row [3]) {?> <a href= "Http://wpa.qq.com/msgrd?" V=1&uin=<?= $row [3]? >&site=http://www.webjx.com&menu=yes "> "/&GT;&LT;/A&GT;&LT;? ><? if ($row [4]) {?> <a href= "mailto:<?= $row [4]?>" > "/></a>"? }?><? if ($_session["key"]==1) {?> ip:<?= $row [6]?> <a href= "update.php?wuleying&id=<?= $row [0]?>" > Change </a> <a href= "delete.php?wuleying&id=<?= $row [0]?>" > Delete </a>< ></a></p>
<p class= "Blue" > Message content:</p>
<div id= "Show_info" ><?= nl2br (Htmlspecialchars ($row [5])?></div>
</div>
<?php
}
}
Else
{
echo "<div id=\" show\ "> No data ......</div>";
}
?>
<div id= "Show_page" >
<p>
<?php
$first = 1;
$prev = $page-1;
$next = $page +1;
$last = $pages;
if ($page ==1&& $pages >1)
{
echo "Home |";
echo "prev |";
echo "<a href=\" index.php?page= ". $next." \ > next Page </a> | ";
echo "<a href=\" index.php?page= ". $last." \ > Last </a> | ";
}
ElseIf ($page >=1&& $page!= $pages && $num >0)
{
echo "<a href=\" index.php?page= ". $first." \ > Home </a> | ";
echo "<a href=\" index.php?page= ". $prev." \ "> Prev </a> | ";
echo "<a href=\" index.php?page= ". $next." \ > next Page </a> | ";
echo "<a href=\" index.php?page= ". $last." \ > Last </a> | ";
}
ElseIf ($page = = $pages && $page!=1)
{
echo "<a href=\" index.php?page= ". $first." \ > Home </a> | ";
echo "<a href=\" index.php?page= ". $prev." \ "> Prev </a> | ";
echo "Next page |";
echo "last |";
}
ElseIf ($page = = $pages)
{
echo "Home |";
echo "prev |";
echo "Next page |";
echo "last |";
}
Else
{
echo "Home |";
echo "prev |";
echo "Next page |";
echo "last |";
}
?>
Total <span><?= $pages?></span> page | Current page <span><?= $page?></span> | Total <span><?= $numrows?></span> message </p>
</div>
<?php
Mysql_close ();
?>
<?php
Require_once ("footer.php");
?>

5 post.php (Submit message page)

?
Require_once ("header.php");
?>
<div id= "Input" >
<form method= "POST" action= "input.php" Name= "Form1" >
<p> Name: <input type= "text" name= "name" size= "class=" y "/> </p>
<p> Sex: <input name= "Sex" type= "Radio" value= "Men" checked/> 帅锅 "<input type=" Radio "name=" "Sex" value= "women" > Beauty </p>
<p>q Q: <input type= "text" name= "QQ" class= "Y"/> (optional) </p>
<p>email:<input type= "text" name= "Email" class= "y"/> (optional) </p>
<p> Message Content:</p>
<p><textarea name= "Info" rows= "5" cols= "></textarea></p>"
<p class= "cen" >
<input type= "Submit" value= "I filled out"/>
<input type= "Reset" value= "I want to rewrite" >
</p>
<p class= "CEN1" > Message board Version 1.0</p>
</form>
</div>
</body>

6 input.php (Insert message)

<?php
Require_once ("conn.php");
$username = $_post[' name '];
$sex = $_post[' sex '];
$QQ = $_post[' QQ '];
$email = $_post[' email '];
$info = $_post[' info '];
if (Strrpos ($username, "<")!==false | | strrpos ($username, ">")!==false| | Strrpos ($username, "@")!==false| | Strrpos ($username, "\")!==false| | Strrpos ($username, "'")!==false| | Strrpos ($username, "_")!==false)
{
Echo ' <script>alert (' name cannot have special characters! '); location.href= ' post.php ';</script> ';
Exit ();
}
if (!ereg ("^[0-9]{0,}$", $QQ))//check QQ format with regular
{
echo "<script>alert (' OICQ information has errors!) Must be a number! '); location.href= ' post.php ';</script> ';
Exit ();
}
if ($email)
{///If you fill out the mailbox, check the mailbox format with regular checks
if (!ereg ("^[a-za-z0-9_\-\.] +@[a-za-z0-9\-]+\. [A-za-z0-9\-\.] +$ ", $email))
{
echo "<script>alert (' email format is not correct!) '); location.href= ' post.php ';</script> ';
Exit ();
}
}
if (! $username)
{
echo "<script>alert" (' name cannot be empty! '); location.href= ' post.php ';</script> ';
Exit ();
}
ElseIf (! $info)
{
echo "<script>alert (' message cannot be empty Oh! '); location.href= ' post.php ';</script> ';
Exit ();
}
Else
{
$ip = getenv (' remote_addr ');//Get Client IP address
$sql = "INSERT into lo (username,sex,qq,email,info,ip,submit_time) VALUES (' $username ', ' $sex ', ' $qq ', ' $email ', ' $info ', ' $ip ', Now ()) ";
$result = mysql_query ($sql);
Mysql_close ();
echo "<script>alert (' Commit success! Back to Home '); location.href= ' index.php ';</script> ';
}
?>

7 update.php (Change Message page)

<?php
Session_Start ();
Require_once (' conn.php ');
Require_once (' header.php ');
if ($_session["key"]==1)
{
$id = $_get[' id '];
$sql = "Select * from lo where id=". $id;
$result = mysql_query ($sql);
$row = Mysql_fetch_array ($result);
?>
<div id= "Update" >
<form action= "updatepost.php?wuleying&id=<?= $row [0]?>" method= "POST" >
<p> Name: <input type= "text" value= "<?= $row [1]?>" name= "name" class= "Y" ></input></p>
<p> message:</p>
<p><textarea name= "Info" rows= "5" cols= "><?=" $row [5]?></textarea></p>
<p class= "cen" ><input type= "Submit" value= "I want to modify"/></p>
<p class= "CEN1" > Message board Version 1.0</p>
</form>
<div>
<?php
}
Else
{
Header (' location:index.php ');
}
?>

8 Updatepost (modify Submission page)

<?php
Require_once (' conn.php ');
$username = $_post[' name '];
$info = $_post[' info '];
$id = $_get[' id '];
echo $id;
$sql = "Update lo set Username= '". $username. "', info= '". $info. "' Where id= '. $id;
mysql_query ($sql);
echo "<script>alert" (' Modified successfully! '); location.href= ' index.php ';</script> ';
?>

9 delete.php (Delete Message page)

<?php
Session_Start ();
Require_once (' conn.php ');
$id =$_get[' id '];
if ($_session["key"]==1)
{
$sql = "Delete from lo where id=". $id;
mysql_query ($sql);
echo "<script>location.href= ' index.php ' </script>";
}
Else
{
Header (' location:index.php ');
}
?>

Ten admin.php (admin login page)

<?php
Require_once (' conn.php ');
Require_once (' header.php ');
?>
<div id= "Admin" >
<form method= "POST" action= "adminpost.php" >
<p> Name: <input type= "text" name= "name" size= "class=" y "/> </p>
<p> Password: <input type= "password" name= "password" size= "" class= "Y"/> </p>
<p class= "cen" ><input type= "Submit" value= "Admin Login"/></p>
<p class= "CEN1" > Message board Version 1.0</p>
</form>
</div>

One adminpost.php (admin authentication page)

<?php
Session_Start ();
Require_once (' conn.php ');
$name = $_post[' name '];
$password = $_post[' password '];
$sql = "SELECT * from admin where name= '". $name. "'";
$result = mysql_query ($sql);
$num = mysql_num_rows ($result);
if (Strrpos ($name, "<")!==false | | strrpos ($name, ">")!==false| | Strrpos ($name, "@")!==false| | Strrpos ($name, "\")!==false| | Strrpos ($name, "'")!==false| | Strrpos ($name, "_")!==false)
{
Echo ' <script>alert (' can't have special characters! '); location.href= ' admin.php ';</script> ';
}
if ($num)
{///If the user exists, check that the password is correct
$rs = Mysql_fetch_array ($result);
if ($rs [2]!= $password)
{
echo "<script>alert (' password is incorrect, please confirm input!") '); location.href= ' admin.php ';</script> ';
}
Else
{//username, password is correct, register session variable, and then jump to the first page
$_session["Key"]=1;
echo "<script>alert" (' Login successful! '); location.href= ' index.php ';</script> ';
}
}
Else
{//If there is no such user
echo "<script>alert (' No this user, please confirm input!") '); location.href= ' admin.php ';</script> ';
}
?>

adminexit.php (Exit Admin page)

<?php
Session_Start ();
$_session["key"] = 0;//make session not 1,0 for tourists, 1 for administrators
Header (' location:index.php ');
?>

Just some basic stuff, like MD5 encryption, UBB, facial images, add admin account, reply message (-_!!) When I did not add to go, interested students can be perfected.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.