Insert operations with multiple fields and a large number of records (PHP) _ MySQL

Source: Internet
Author: User
When using php + mysql for development, it is inevitable to insert and delete data. I think everyone is used to the data input mode, such as bbs Publishing, one message book, and one article. however, in many projects, data input one by one is not allowed. for example, if you enter your student's test scores one by one, you need to click submit for more than 40 times. this greatly consumes the patience and time of the class teacher. how can you use php + mysql for development, data insertion/deletion is inevitable. I think you are used to the data input mode, such as bbs Publishing, one message book, and one piece.
However, in many projects, data input one by one is not allowed.
For example, if you enter a student's exam score one by one, you need to click "submit" for more than 40 times. this greatly consumes the patience and time of the class teacher, how to input the data of a class in one breath is a key point of the campus management system (of course, this requirement is also frequently used in other occasions)
The following article is just my work experience for reference by PHP colleagues)
There are two main ideas: 1. dynamic generation of form and field attributes
2. use variables to represent variables

First, let's look at the data structure:
Create table score
(
Score_id int unsigned primary key auto_increment,
S_no int unsigned not null,
Grade tinyint not null,
Class tinyint not null,
Class2 tinyint,
Subject char (6) not null,
Score tinyint default '0'
) Type = MYISAM;

Create table students
(
Imitate int unsigned primary key,
S_no int unsigned,
S_name char (10) not null,
......
) Type = MYISAM;
S_no is the student ID, which is the key to the entire program. because the number of languages in this school is divided into eight class classes, there is one class2 field in the orders table.
The objective is to input the "classification class, score" data of more than 40 people in the class in a form and submit the data at one time.
Because the input involves multiple fields and more than one record needs to be inserted, it makes programming more difficult. two fields are involved, but we can solve the problem of any multiple fields. (Of course, forms must be dynamically generated .)

Let's take a look at the source code:
(Because this site does not filter out html tags, please copy the following code to the code View of dreamweaver .)




// Connect to the database

Include ("const. php ");

Include ("link. php ");

// If no form is submitted, perform the following operations:

If (! Empty ($ HTTP_POST_VARS )){

// Obtain The POST variable

$ Grade = $ _ POST ["grade"];

$ Tclass = $ _ POST ["tclass"];

// Since the last two student IDs cannot exceed 60, we receive them one by one.

For ($ I = 1; $ I <60; $ I ++ ){

// The "variable name" of the classification class and score recorded in the first record (note that it is not a numerical value)

$ Fenlei = "a". $ I;

$ Score = "B". $ I;

// Because the student ID may have an interval, first determine whether the value exists.

If (! Empty ($ _ POST ["$ fenlei"]) &! Empty ($ _ POST ["$ score"])
{

// If register_globals = on, use $ fenlei and $ score.

// Assume register_globals = on. if your setting is not, define two other variables to receive their values.

// Regenerate the complete student ID.

$ S_no = $ grade * 10000 + $ class * 100 + $ I;

// Generate the query statement and use "variable". of course, if register_globals = off, it is different.

$ Query = "insert into score (s_no, grade, class, class2, score) values ('$ s_no',' $ grade ',' $ tclass ','". $ fenlei. "','". $ score. "')";

// Display to see if the statement is correct.

Echo $ query ."
";

// I did not write the statement.

}

}

}

?>



TEST














The results are as follows:
Insert into score (s_no, grade, class, class2, score) values ('000000', '99', '6', 'A', '90 ')
Insert into score (s_no, grade, class, class2, score) values ('000000', '99', '6', 'A', '86 ')
Insert into score (s_no, grade, class, class2, score) values ('000000', '99', '6', 'A', '84 ')
Insert into score (s_no, grade, class, class2, score) values ('000000', '99', '6', 'B', '75 ')
Insert into score (s_no, grade, class, class2, score) values ('000000', '99', '6', 'B', '80 ')
Insert into score (s_no, grade, class, class2, score) values ('000000', '99', '6', 'B', '79 ')
A class requires more than 40 cycles, and the mysql_query () function is executed more than 40 times. in a good network environment, it takes about 1 second (mysql is fast, A super-long SQL statement can be generated and executed once. for which efficiency is higher, let's try it.

The advantage of this method is that you do not have to click "submit" again and again. The disadvantage is that all data must be input at one time! (A little contradictory ?) Yes! What if I use this function again and input duplicate data? The solution is to determine whether the record exists before the query is executed in each loop. this seems to have to execute another query. that is to say, to ensure that the data is correct, and if there are n persons in a class, a total of N * 2 queries are required.
Another way is to restrict the generation of forms so that the system load can be distributed, significantly better than the previous solution.

I don't know if there is any better way to achieve both efficiency and stability. thanks for your advice.

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.