This article illustrates the method of PHP batch adding data and batch updating data. Share to everyone for your reference. The specific analysis is as follows:
PHP If you want to save the data in bulk, we just use the SQL INSERT INTO statement can be implemented in the bulk of the data saved, if the update data using update set can complete the update, the operation method is very simple, the following two examples of collation.
Batch data entry
Design method: Simultaneously submits several form records, sets the same text domain name for each record, then in the form processing page, through the For loop to read extracts the form submits the data, finally takes the data the form to add the data to the database one by one.
where you apply a count () function to get the number of elements in the array. int count (mixed Var);
Form submission page with the following code:
Copy Code code as follows:
<form name= "Form1" method= "Post" action= "index_ok.php" >
<tr>
<td> Product Name </td>
<td> number </td>
<td> 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= "></td>
<td><input name= "sp_number[]" type= "text" id= "Sp_number" size= "ten" ></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= "></td>
</tr>
<input type= "Submit" name= "Submission" value= "submitted" >
<input type= "reset" name= "reset" value= "reset" >
</form>
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, the code is as follows:
Copy Code code 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];
$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 ', ' $path 1 ', ' $path 2 ', ' $path 3 ', ' $path 4 ', ' $data ');
if ($query ==true) {
echo "submitted successfully";
Else
echo "Commit failed";}
}
?>
Batch Update Data
The list () function is used to assign values to multiple variables at once, mainly through the while, the list (), each () function to implement the batch update of the data, and the code is as follows:
Copy Code code 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= "activated" >
<input type= "Submit" Name= "Submit2" value= "Frozen" >
</tr>
</form>
Form processing page, the code is as follows:
Copy 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 "<script> alert (' Activation succeeded '); window.location.href= ' index.php ';</script> ';}}
if ($submit 2== "Frozen") {
while (list ($name, $value) =each ($_post)) {
$result =mysql_query ("Update tb_user set operation= ' Freeze ' where id= '". $name. "");
if ($result ==true) {
echo "<script> alert (' Freeze succeeded '); window.location.href= ' index.php ';</script> ';}}
}
?>
Summary:Forestall's friends will find that two examples have a few things in common, one is the form from the table Single-name is in the counts[] array, and in the PHP processing accept page will use for or while to implement the traversal, I will briefly give you an analysis of these two examples.
Counts[]: This represents an array in the form, and if you have 10 forms then we name=counts[the meaning that they are all the same array, knowing that this is an array and knowing why the traversal is used.
For or while: because the form comes with an array, we can iterate through the array and save the data as follows:
while (list ($name, $value) =each ($_post)) {or
for ($i =0; $i <count ($sp _name); $i + +) {The two implementation results are the same.
I hope this article will help you with your PHP program design.