PHP Bulk Add data and batch update data implementation method, PHP add Data _php Tutorial

Source: Internet
Author: User

PHP Bulk Add data and batch update data implementation method, PHP add data


In this paper, we explain how to implement PHP batch data addition and batch update data. Share to everyone for your reference. The specific analysis is as follows:

PHP If you want to bulk save the data we can use the SQL INSERT INTO statement can be implemented in bulk data storage, if the update data using update set to complete the update, the operation method is very simple, the following two examples.

Batch data entry

Design method: Submit multiple form records at the same time, set the same text field name for each record, and then in the form processing page, through the For loop to read the extracted form submitted data, and finally in the form of data to add data to the database.

where you apply a count () function to get the number of elements in the array. int count (mixed Var);

Form submission page, the code is as follows:
Copy the code code as follows:
Database Connection page, the code is as follows:
Copy code code 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 with the following code:
Copy code code as follows: <?php session_start (); Include ("conn/conn.php");
if ($submit ==true) {
for ($i =0; $i $path =$_post["Sp_name" [$i];
$path 1=$_post["Sp_number" [$i];
$path 2=$_post["Price" [$i];
$path 3=$_post["counts" [$i];
$path 4=$_post["Address" [$i];
$query =mysql_query ("INSERT into tb_products (sp_name,sp_number,price,counts,address,data) VALUES (' $path ', ' $ Path1 ', ' $path 2 ', ' $path 3 ', ' $path 4 ', ' $data ');}
if ($query ==true) {
echo "submitted successfully";
Else
Echo "Commit failed";}
}
?
Bulk Update Data

Mainly through the while, the list (), each () function to implement the bulk update of data, the list () function is used to assign multiple variables at once, the code is as follows:
Copy the Code code as follows: <?php session_start (); Include ("conn/conn.php");? >

Form processing page, the code is as follows:
Copy the Code code as follows: <?php session_start (); Include ("conn/conn.php")
if ($submit = = "Active") {
while (list ($name, $value) =each ($_post)) {
$result =mysql_query ("Update tb_user set operation= ' activates ' where id= '". $name. "");
if ($result ==true) {
echo "";}}

if ($submit 2== "freeze") {
while (list ($name, $value) =each ($_post)) {
$result =mysql_query ("Update tb_user set operation= ' freezes ' where id= '". $name. "");
if ($result ==true) {
echo "";}}
}
?>
Summary: Cautious's friends will find that two examples have a few things in common, one is the form from the table sole name is in the form of counts[] array, and in the PHP processing accept the page will use for or while to implement traversal, I would like to give you a simple analysis of these two examples.

Counts[]: This is represented in the form of an array, if you have 10 forms then we name=counts[] meaning they are all the same array, know that this is an array and know the following to know why the use of traversal.

For or while: because the form comes in an array, we can iterate through the array and save the data, as in the following code:

while (list ($name, $value) =each ($_post)) {or

for ($i =0; $i < p="">

I hope this article is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/928217.html www.bkjia.com true http://www.bkjia.com/PHPjc/928217.html techarticle PHP Bulk Add data and batch update data implementation method, PHP add data in this paper, we describe the implementation of PHP Bulk add data and batch update data. Share for everyone to join us ...

  • 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.