This article mainly introduces how to develop a simple php news management system through php. Such a system is worth learning for new php beginners. First, we have mastered the basic logic and code capabilities, in order to write better code simple php news publishing system tutorial (first version) Lecture 1: Use phpmyadmin to create a database first, named yayu. Create a data table named news in this database. Next we will enter a key point, that is, to create fields under the news table.
So what is a field? It is a general term for a type of things. For example, all the press releases are represented by one term. (in my experience, fields created using phpmyadmin can be used in Chinese, but in practice, they can still be used in English, the best computer in the United States ). We use "time" for representation. There can be a lot of "posting time" in the field time. how can we differentiate these time? this can be used to query the content in other fields, for example, when we create a news title field as "title", the content in the title field is not the same according to common sense. Therefore, you can use a title to query the posting time. In fact, we can make each title or time content correspond to a number, that is, the field --
"Id": the field "id" is a default preferred field. content in other fields can be repeated, but this field is an Arabic number that increases from 1. When setting this field, you must set the primary key, index, unique, and auto-increment. This auto-increment is automatically added. When content is added to any field, this field is automatically increased by 1, that is, any field corresponds to a unique id, such as 1, 2, 0 27 ......
Next we will talk about the establishment of some fields in news.
1. id: indicates the number of each news item. it is unique and the type is tingint. the length of the tingint type is not required. The system defaults to 4. in the "extra" field, select auto-increment, and select the primary key.
2. author: it indicates the author (News publisher). set the type to varchar. when setting the length of this field, if the authors are all Chinese, the maximum length of 8 bytes is (4 Chinese characters ), however, given that the author may be a foreigner and the number of 8 bytes is obviously too small, the same problem exists for other fields. here we will set the length to 8.
3. title: indicates the news title. the type is varchar, the length is 60, and the attribute is primany key.
4. content: indicates the news content. the type is text. You do not need to set the length for this type.
5. source: indicates the news source. the type is varchar and the length is 60.
6. date: indicates the publication time. the type is datetime. you do not need to set the length. the attribute is primarykey.
The following describes the field types:
1. date: time and date type. The time and date types also include:
Required datetime: 0000-00-00 00:00:00
Expiry date: 0000-00-00
⊕ Timestamp: 00000000000000 (14 0, length depends on the display size)
Elapsed time: 00:00:00
Seasonal year: 0000
2. the conten t field indicates the news content. because of its large capacity, the text type is used (up to 65535 bytes are supported)
3. the title field is set to primarykey. If no news item has the same posting time, the date field can also be used? Primany key, which makes it easier to sort and retrieve news in the future.
4. although a text field belongs to a character type, its size cannot be specified. if the length is set, an error occurs in the SQL statement.
Now, the news data table is created.
Since not everyone can add news, only administrators can do it, so now let's create a data table users to store administrators.
1: id: tinyint type, auto-increment and primary key.
2: name: indicates the administrator name. The type is varchar. the length is 8 and the attribute is primarykey.
3: password: indicates a password of the varchar type. the length is 32.
4: mail: same as the mail address. the type is varchar and the length is 30.
Now, the two databases have been set up, and we will enter the development of the news program below.
Original php News system tutorial (first version) Lecture 2: Basics of news programs
I. Basics of database connection
To add, retrieve, modify, and delete news, you must first connect to the host, select a database, and send a request to the database. Otherwise, everything is on paper. The following describes three important MySQL statements: mysql_pconnect () (connect to the host), mysql_select_db () (select the data Library), and mysql_query () (send requests to the database ).
1. mysql_pconnect ()
It is used to connect to the host.
Syntax: mysql_pconnect ("host to be logged in", "username at login", "password ");
For example, mysql_pconnect ("localhost", "root ","");
"Localhost" and "root" are all default host names and usernames under phpmyadmin, and the password is empty.
The same function also has mysql_connect (). The syntax is the same. The difference is that the former enables a long-term connection and cannot be closed using the mysql_close () function, while the latter must be closed in time using mysql_close () after use. For a website, the former is better than the latter. This reduces the burden on the MySQL server to process and close connections.
After the function is successfully connected, a connection ID is returned. Therefore, this function is generally written as follows:
$ Link = mysql_pconnect ();
Speaking of this, add a function mysql_close (),
Syntax: mysql_close ("connection ID to close ");
For example, mysql_close ($ link );
Note: All () values are strings, and $ is not required.
2. mysql_select_db ()
It is used to select a database as the current database. Subsequent operations will be performed in this database. If this function is successfully executed, true is returned; otherwise, false is returned.
Syntax: mysql_select_db ("database name", "Connection ID ");
The second parameter can be omitted. it will automatically find and connect to the last connection ID.
In this program, this statement is written as mysql_select_db ("yayu", $ link );
3. mysql_query ()
It is used to send a request string to the server.
Syntax: mysql_query ("The question string", connection ID );
The first parameter is a complete MySQL statement, and the second parameter can be omitted.
Before using this function, use mysql_select_db () to specify the database to be used.
When the query string is update, insert, or delete, the function returns true or false, indicating whether the query is successful. if the query string is a select statement, a result ID is returned, if an error occurs in select, false is returned.
After learning about the three important functions above, we can give orders to the database. What do we use to send orders? Let's take a look at four MySQL statements!
2. basic knowledge of sending request statements to databases
They are: insert (insert data to the database), delete (delete data in the data table), select (retrieve data), and update (update data ).
1 insert (): insert data to the database.
Syntax A: insert into data table name (Field 1, field 2 ,......) Values ("content of Field 1 d", "2 content of field "...... )
Syntax B: insert into data table name set Field 1 = "content of Field 1", Field 2 = "content of field 2 ",......
The field name can be omitted for A, but the content of the values section must be the same as the field order defined in phpmyadmin.
See the example below:
A: insert into news (title, date, author, source, content) values ($ title, $ date, $ author, $ source, $ content)
Note: The above "$ ...... "Indicates the content of the field to be added. when it is defined: $ ...... = Content;
B: insert into news set title = $ title, author = $ author
Note: If the content is a number, "" can be omitted.
2 delete (): delete data from a data table
Syntax: delete from data table name where specified location limit number
If no "where specified location" exists, all data in the table is lost. "Limit number" indicates the maximum number of lines that can be deleted by the server.
Example: delete from news where id = $ id limit 1
3. select (): Retrieve data
Syntax: select field name 1, field 2 ,...... From data table name where location
To list the data of all columns in all records, you can use "*" to represent the field name.
Example: A: select id, author from news where id = $ id
B: select * from news where id = $ id
4 update (): update data
The syntax is almost the same as that of insert.
For example, update news set author = $ author, title = $ title where id = $ id
It is worth noting that when using the update statement, you must use the where clause. Otherwise, a large amount of data may be lost. For example:
Update news set author = "Bud Rain"
This operation changes all the authors in the table to "Bud rain.
Another positive example is to record the administrator's table users. if there is a field named age, it is used to store the user's age. one year later, they will increase by one year, you can use the following statement:
Update users set age = age + 1
That's good. I have mastered most of the basics of the program now, and a few of them will be able to grasp it later in the example.
Now we are faced with the problem of writing algorithms.
Original php News system tutorial (first version) Lecture 3: news program algorithm (I) ------ Add News
I. Add News
Adding News is to add new data to the database.
The entire algorithm is as follows: The administrator fills in the news content in the form, including: title, author, source, content, and the content of the other two fields (id, time) are completed on the server, of course, you have to write programs by yourself, but you can't help yourself manually. After submitting the form, use the MySQL statement to add them to the database.
Below is a brief introduction to the formLabels andLabel.
Label is a single-line text box. Commonly used attributes include name and type. The name attribute specifies the variable name, which represents. The type attribute specifies. If type = text, it is a general text. If type = password, the"The content in the browser is displayed in black spots, so that when you enter the content, it will not be accidentally seen by others, resulting in data security problems.
The label is a multi-line text box, and the common attribute is name.
In this program, the program is as follows:
In "name =" author "," author "represents". Similarly, this "author" can also be "title" or "others". it is worth noting that this "author" is different from the field author. I mentioned two very similar concepts here: The field author and the variable $ author (the above "author" is actually $ author, because it represents"). Although they are almost identical, they are definitely not the same thing. Author is a field name in the data table news. PHP uses it to access data in MySQL with limited conditions. it cannot be replaced with other characters in the program; $ author is the variable symbol set by the user in this program. its value is. Since it is only a variable symbol, we can use any field, as long as it is consistent with the name attribute of the corresponding element in the form. The reason why we use the same character as the field name is that we don't have to remember another variable name.
Let's take a look.Label content:
</P> <textarea>The content in is used to obtain the content of the content field. because there is too much content in this field, you can only use this label.
After filling in the content, we only need to submit the content. how can this process be implemented? Let's take a look at the following program:
Here, type = "submit"/type = "reset" represents the submission and rewriting of News respectively. The content of the value attribute is displayed on this button. Name = "reset"/name = "submit" has the same meaning as above.
The form element in HTML is specifically responsible for interactive operations by users. When you click the submit button, all the elements in the form will be submitted to the file indicated by the action in the form of variables for processing. The variable name is determined by the name attribute of the element. In this program, the code is as follows:
";
Echo $ edit;
}
In the above "if (@ $ _ GET [" id "])", "@" indicates that an error message is returned when the function is disabled. In this way, when a program encounters an exception, the common user will not be able to see the inexplicable error information.
It is worth noting that we put the modifiable content in the attribute value, such as "value =". $ row ["title.
In
}
?>
Let's look at the login and logout programs.
If (@ $ _ POST ["submit"])
{
If (empty ($ _ POST ["username"])
Die ("Enter the user name ");
// The die () function is used here to prompt the user when the name is null.
Else
$ Username = $ _ POST ["username"];
// Record the user name.
If (empty ($ _ POST ["password"])
Die ("enter the password ");
Else
$ Password = $ _ POST ["password"];
$ Link = mysql_connect ("localhost", "root ","");
$ Selectdb = mysql_select_db ("yayu", $ link );
$ SQL = "select id, password, name from users where name = '$ username '";
$ Result = mysql_query ($ SQL) or die (mysql_error ());
While ($ pa = mysql_fetch_assoc ($ result ))
{// The mysql_fetch_assoc () function stores the returned data in an array. the subscript of the array corresponds to the field name //
$ Pwd = $ pa [password];
$ Userid = $ pa [id];
// Check whether the user name is correct
If (! $ Result)
{
Echo "wrong user name ";
Echo"
Log On Again ";
Mysql_close ($ link );
Exit;
}
// Check whether the password is correct
If ($ pwd! = $ Password)
{
Echo "dear". $ username ."
";
Echo "You entered the wrong password ";
Echo"
Please log on again ";
Mysql_close ($ link );
Exit;
}
// If everything is normal, set the Cookie
Setcookie ("username", $ _ POST ["username"]);
Header ("Location: admin. php ");
}
If (@ $ _ GET ["action"] = "logout ")
{
Setcookie ("username ","");
Header ("Location: index. php ");
}
}
?>
In the above program, there is a very important thing, that is, Cookie.
Cookie is a small data packet stored by the Web server on the client. when the user connects to the Web server where the Cookie is placed again, the Web server can read it again to prevent data stored in the Cookie from being passed through the Cookie, the server can store specific information on the visitor's machine.
We can see a Cookie function setcookie () in the program, which is used to store the user name. Syntax: setcookie ("custom username", "user name ");
In a program that is occasionally written, "user name" is used. when "user name" is blank,
It is equivalent to logging out.
When the administrator needs to perform some client operations, the Cookie is required. Even if (@ $ _ COOKIE ["username"]) (is this method similar to POST)
For judgment. If username has a value, it is regarded as 1 in the logical volume.