Php and mysql message board program implementation code

Source: Internet
Author: User
Tags php and mysql php database setcookie


I won't talk about the PHP syntax. Let's talk about connecting to mysql!

The code is as follows: Copy code
$ Conn = @ mysql_connect ("localhost", "root", "mydown") or die ("database link error ");
Mysql_select_db ("bbs", $ conn );
Mysql_query ("set names 'gbk '");

The first statement describes the database connection statement. If a database connection error is prompted, the preceding three parameters indicate the database address localhost, and the database username is root, connect to the database password mydown;

The second sentence describes the database that $ conn represents when connected to the database table bbs.

The third sentence is to convert the encoding format to display Chinese characters.

After the database is connected, let's take a look at how Database statements are submitted and run in PHP!

The code is as follows: Copy code
$ SQL = "INSERT INTO message (id, user, title, content, lastdate) values ('', '$ _ POST [user]', '$ _ POST [title]', '$ _ POST [content]', now ())";
Mysql_query ($ SQL );

We should be familiar with the blue font above, that is, inserting information in the database. The second sentence is to run the statement in the database. Think of variables similar to $ _ POST [title]. You should know that this is the information obtained through web page submission.


Instance

Install

The code is as follows: Copy code

<Html>
<Head>
<Title> message board installation </title>
</Head>
<Body>
<Form action = "check_install.php" method = "POST">
<Table>
<Tr> <td align = "right"> <font color = "Red"> * </font> enter the address of the database server: </td> <input name = "llcc_server" type = "text" value = "localhost"> </td> </tr>
<Tr> <td align = "right"> <font color = "Red"> * </font> enter the database server user name: </td> <input name = "llcc_user_name" type = "text" value = ""> </td> </tr>
<Tr> <td align = "right"> <font color = "Red"> * </font> enter the database server password: </td> <input name = "llcc_user_pass" type = "text" value = ""> </td> </tr>
<Tr> <td align = "right"> <font color = "Red"> * </font> enter the database name: </td> <input name = "llcc_data_name" type = "text" value = ""> </td> </tr>
<Tr> <td> <input type = "submit" name = "bt_install" value = "submit"> </td> </tr>
</Table>
</Form>
</Body>
</Html>

Check_install.php

The code is as follows: Copy code

<? Php
// Obtain the submitted database value
$ Llcc_server =$ _ POST ['llcc _ server'];
$ Llcc_user_name = $ _ POST ['llcc _ user_name '];
$ Llcc_user_pass =$ _ POST ['llcc _ user_pass '];
$ Llcc_data_name = $ _ POST ['llcc _ data_name '];
// Open the database configuration file
$ Fp = fopen ("../config. php", 'w ');

If (! $ Fp ){
Echo "sorry, try again! ";
}
// Write file content
$ Outconfig = "<? Phpn ";
$ Outconfig. = '$ server = ';
$ Outconfig. = "". $ llcc_server. "; // IP address of the database. If it is local, localhostn is used by default ";
$ Outconfig. = '$ server_name = ';
$ Outconfig. = "". $ llcc_user_name. "; // database connection username, generally rootn ";
$ Outconfig. = '$ server_pass = ';
$ Outconfig. = "". $ llcc_user_pass. "; // database connection password. N ";
$ Outconfig. = '$ server_data = ';
$ Outconfig. = "". $ llcc_data_name. "; // database name n ";
$ Outconfig. = "?> ";
$ Fw = fwrite ($ fp, $ outconfig );

If ($ fw ){
Echo "<br> <center> installation is complete! Manually delete the install folder in the directory <br> <a href = '../'> Return to the home page </a> </center> ";
}


?>

Login. php

The code is as follows: Copy code

<? Php
Include ("header. php ");
Include ("conn. php ");

If ($ _ GET [out]) {
Setcookie ("cookie", "out ");
Echo "<script language =" javascript "> location. href = 'login. Php'; </script> ";
  }


If ($ _ POST [id] = 'admin '){
$ Pw = md5 ($ _ POST [pw]);
If ($ pw = '1b3ae77a2e06580c8cb57e0000ddc4a26 '){
Setcookie ("cookie", "OK ");
Echo "<script language =" javascript "> location. href = 'login. Php'; </script> ";
    }
  }
If ($ _ COOKIE ['cooker']! = 'OK '){
?>

<SCRIPT language = javascript>
Function Checklogin ()
{
If (myform. id. value = "")
 {
Alert ("enter your login name ");
Myform. id. focus ();
Return false;
 }
If (myform. pw. value = "")
 {
Alert ("The password cannot be blank ");
Myform. pw. focus ();
Return false;
 }
}
</SCRIPT>
& Lt; table width = "400" & gt;
<Form action = "" method = "post" name = "myform" onsubmit = "return Checklogin ();">
<Tr> <th> login name: </th> <td> <input type = "text" name = "id"/> </td> </tr>
<Tr> <th> password: </th> <td> <input type = "password" name = "pw"/> </td> </tr>
<Tr> <td colspan = "2"> <input type = "submit" name = "submit" value = "login"/> </td> </tr>
</Form>
</Table>
<?
} Else {
?>
<A href = '? Out = login '> exit </a> <form name = "ff">
<Input type = "submit" name = "sub" value = "clear database"> </form>
<? Php
If ($ _ GET [sub] = "clear database ")
 {
$ SQL = "delete from message ";
$ Query = mysql_query ($ SQL );
 }
?>
<? Php
}
?>

Conn. php database connection

The code is as follows: Copy code
<? Php
Include ("config. php ");
$ Conn = @ mysql_connect ($ server, $ server_name, $ server_pass) or die ("database link error ");
Mysql_select_db ($ server_data, $ conn); // The name of the database to be opened. "bbs" is my database name and changed to my own name!
Mysql_query ("set names 'gbk'"); // use GBK Chinese encoding;
?>

Index. Php file

The code is as follows: Copy code

<? Php
Include ("conn. php ");
Include ("header. php ");
?>
Free House Message Board <? Php
$ SQL = "select * from message order by id DESC ";
$ Query = mysql_query ($ SQL );
While ($ row = mysql_fetch_array ($ query )){
?>
<Tr bgcolor = "# eff3ff">
<Td> <B> <? Php echo $ row [id];?> Username: </B> <? Php echo $ row [user];?> & Nbsp; <B> Title: </B> <? Php echo $ row [title];?> & Nbsp; <B> Published on: </B> <? Php echo $ row [lastdate];?> </Td>
</Tr>
<Tr bgColor = "# ffffff">
<Td> <B> Content: <? Php echo $ row [content];?> </B> </td>
</Tr>
<? Php
  }
?>
</Table>

Add. php add a program

The code is as follows: Copy code

<? Php
Include ("header. php ");
Include ("conn. php ");
If ($ _ POST ["submit"])
{
$ SQL = "INSERT INTO message (id, user, title, content, lastdate) values ('', '$ _ POST [user]', '$ _ POST [title]', '$ _ POST [content]', now ())";
Mysql_query ($ SQL );
Echo "the message has been submitted ";
};
?> <Center>

<Table border = "0" width = "450">
<Form name = "add" method = "post" onsubmit = "return CheckPost ();">
<Tr> <th> User: </th> <td> <input type = "text" name = "user" size = "10"> </td> </tr>
<Tr> <th> title: </th> <td> <input type = "text" name = "title" size = "30"> </td> </tr>
<Tr> <th> content: </th> <td> <textarea name = "content"> </textarea> </td> </tr>
<Tr> <td colspan = "2" align = "center"> <input type = "submit" name = "submit" value = "post message"> & nbsp; & nbsp; <input type = "reset" value = "reset"> </td> </tr>
</Form>
</Table>
</Center>

<SCRIPT language = javascript>
Function CheckPost ()
{
If (add. user. value = "")
 {
Alert ("Enter the user name ");
Add. user. focus ();
Return false;
 }
If (add. title. value. length <5)
 {
Alert ("the title cannot be less than five characters ");
Add. title. focus ();
Return false;
 }
If (add. content. value = "")
 {
Alert ("message content is required ");
Add. content. focus ();
Return false;
 }
}
</SCRIPT>

Test. php database structure

The code is as follows: Copy code

<? Php
Include ("config. php ");
Include ("conn. php ");
$ SQL = "CREATE TABLE 'message '(
'Id' tinyint (1) not null auto_increment,
'User' varchar (25) not null,
'Title' varchar (50) not null,
'Content' tinytext not null,
'Lastdate' date not null,
Primary key ('id ')
) ENGINE = InnoDB default charset = gbk AUTO_INCREMENT = 1 ;";
$ Query = mysql_query ($ SQL );
If ($ query ){
Echo "executed successfully ";}
Else echo "execution failed! ";
?>

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.