Php batch add data and batch update data implementation method, php add data _ PHP Tutorial

Source: Internet
Author: User
Php implements batch data addition and batch data update. php adds data. Php batch addition of data and batch update of data implementation method, php to add data this article instance describes the php batch addition of data and batch update of data implementation method. We will share with you how to add and update data in batches in php.

This example describes how to add and update data in batches in php. Share it with you for your reference. The specific analysis is as follows:

If php wants to save data in batches, we only need to use the SQL insert into statement to save data in batches. If update set is used to update data, the update can be completed, the operation methods are very simple. below are two examples.

Batch data input

Design Method: submit multiple form records at the same time, set the same text domain name for each record, and then read and extract the data submitted by the form on the form processing page through the for loop, finally, add the data to the database one by one in the form of data.

Use the count () function to obtain the number of elements in the array. int count (mixed var );

Form submission page. the code is as follows:

The code is as follows:


Database Connection page, the code is as follows:

The code is as follows:

<? Php
$ Id = mysql_connect ("localhost", "root", "password") or die ('connection failed'. mysql_error ());
If (mysql_select_db ('mydatabase', $ id ))
Echo "";
Else
Echo ('select db failed: '. mysql_error ());
?>


Form processing page, the code is as follows:

The code is as follows:

<? Php session_start (); include ("conn/conn. php ");
If ($ submit = true ){
For ($ I = 0; $ I $ Path = $ _ POST ["sp_name"] [$ I];
$ Path1 = $ _ POST ["sp_number"] [$ I];
$ Path2 = $ _ POST ["price"] [$ I];
$ Path3 = $ _ POST ["counts"] [$ I];
$ Path4 = $ _ POST ["address"] [$ I];
$ Query = mysql_query ("insert into tb_products (sp_name, sp_number, price, counts, address, data) values ('$ path',' $ path1 ',' $ path2 ', '$ path3', '$ path4', '$ data ');}
If ($ query = true ){
Echo "submitted successfully ";
Else
Echo "failed to submit ";}
}
?>


Batch update data

The while, list (), and each () functions are used to update data in batches. the list () function is used to assign values to multiple variables at a time. the code is as follows:

The code is as follows:

<? Php session_start (); include ("conn/conn. php");?>


Form processing page, the code is as follows:

The code is as follows:

<? Php session_start (); include ("conn/conn. php ")
If ($ submit = "activate "){
While (list ($ name, $ value) = each ($ _ POST )){
$ Result = mysql_query ("update tb_user set operation = 'activated' where id = '". $ name ."'");
If ($ result = true ){
Echo "script" alert ('activated successfully'); window. location. href = 'index. php'; script ";}}

If ($ submit2 = "freeze "){
While (list ($ name, $ value) = each ($ _ POST )){
$ Result = mysql_query ("update tb_user set operation = 'freeze 'Where id = '". $ name ."'");
If ($ result = true ){
Echo "script" alert ('Frozen successfully'); window. location. href = 'index. php'; script ";}}
}
?>


Summary:You will find that the two examples have several similarities. One is that the form name from is in the form of a counts [] array, in php processing, the accept page will use for or while for traversal. below I will give you a simple analysis of these two examples.

Counts []: This indicates an array in the form. if you have 10 forms, our name = counts [] indicates that all of them are the same array, once you know that this is an array, you will know why traversal is used.

For or while: because the form is an array, we can traverse the array and save the data. the following code:

While (list ($ name, $ value) = each ($ _ POST) {OR

For ($ I = 0; $ I

I hope this article will help you with php programming.

Examples in this article describes how to add data in batches and update data in batches in php. I will share it with you for your reference...

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.