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:
<Form name = "form1" method = "post" action = "index_ OK .php">
<Tr>
<Td> Product name </td>
<Td> No. </td>
<Td> unit price </td>
<Td> quantity </td>
<Td> origin </td>
<Input name = "data" type = "hidden" value = "<? Php echo $ data;?> ">
</Tr>
<Tr>
<Td> <input name = "sp_name []" type = "text" id = "sp_name" size = "15"> </td>
<Td> <input name = "sp_number []" type = "text" id = "sp_number" size = "10"> </td>
<Td> <input name = "price []" type = "text" id = "price" size = "8"> </td>
<Td> <input name = "counts []" type = "text" id = "counts" size = "8"> </td>
<Td> <input name = "address []" type = "text" id = "address" size = "15"> </td>
</Tr>
<Input type = "submit" name = "submit" value = "submit">
<Input type = "reset" name = "reset" value = "reset">
</Form>
Database Connection page, 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:
<? Php session_start (); include ("conn/conn. php ");
If ($ submit = true ){
For ($ I = 0; $ I <count ($ sp_name); $ 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:
<? Php session_start (); include ("conn/conn. php");?>
<Form name = "form1" method = "post" action = "index_ OK .php">
<? Php $ query = "select * from tb_users ";
$ Result = mysql_query ($ query );
If ($ result = true ){
While ($ myrow = mysql_fetch_array ($ result )){
?>
<Tr>
<Td> <input name = "<? Php echo $ myrow [id];?> Type = "checkbox" value = "<? Php echo $ myrow [id];?> </Td>
<Td> <? Php echo $ myrow [user];?> </Td>
<Td> <? Php echo $ myrow [popedom];?> </Td>
<Td> <? Php echo $ myrow [operation];?> </Td>
</Tr>
<? Php }}?>
<Tr>
<Input type = "submit" name = "submit" value = "activate">
<Input type = "submit" name = "submit2" value = "freeze">
</Tr>
</Form>
Form processing page, 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 <count ($ sp_name); $ I ++) {the two implementations have the same results.