本文實例講述了php批量添加數(shù)據(jù)與批量更新數(shù)據(jù)的實現(xiàn)方法。分享給大家供大家參考。具體分析如下:
php如果要批量保存數(shù)據(jù)我們只要使用sql的insert into語句就可能實現(xiàn)數(shù)據(jù)批量保存了,如果是更新數(shù)據(jù)使用update set就可以完成更新了,操作方法都非常的簡單,下面整理兩個例子.
批量數(shù)據(jù)錄入
設計方法:同時提交多條表單記錄,為每一條記錄設置相同的文本域名稱,然后在表單處理頁中,通過for循環(huán)來讀取提取表單提交的數(shù)據(jù),最后以數(shù)據(jù)的形式將數(shù)據(jù)逐條添加到數(shù)據(jù)庫中.
其中,應用一個count()函數(shù)來獲取數(shù)組中元素的個數(shù).int count(mixed var);
表單提交頁面,代碼如下:
<tr>
<td>商品名稱</td>
<td>編號</td>
<td>單價</td>
<td>數(shù)量</td>
<td>產(chǎn)地</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="提交">
<input type="reset" name="reset" value="重置">
</form>
數(shù)據(jù)庫連接頁,代碼如下:
$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());
?>
表單處理頁,代碼如下:
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"提交成功";
else
echo"提交失敗";}
}
?>
批量更新數(shù)據(jù)
主要通過while, list(),each()函數(shù)來實理數(shù)據(jù)的批量更新,list()函數(shù)用于一次性為多個變量賦值,代碼如下:
<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="激活">
<input type="submit" name="submit2" value="凍結">
</tr>
</form>
表單處理頁,代碼如下:
if($submit=="激活"){
while(list($name,$value)=each($_POST)){
$result=mysql_query("update tb_user set operation='激活' where id='".$name."'");
if($result==true){
echo "<script> alert('激活成功');window.location.href='index.php';</script>";}}
if($submit2=="凍結"){
while(list($name,$value)=each($_POST)){
$result=mysql_query("update tb_user set operation='凍結' where id='".$name."'");
if($result==true){
echo "<script> alert('凍結成功');window.location.href='index.php';</script>";}}
}
?>
總結:心細的朋友會發(fā)現(xiàn)兩個例子都有幾個共同點,一個是表單from的表單名是以counts[]數(shù)組形式了,而在php處理接受頁面都會使用for 或while來實現(xiàn)遍歷了,下面我就簡單的給大家分析這兩個例子.
counts[]:這個在表單中是代表數(shù)組,如果你有10個表單那么我們name=counts[] 意思他們內(nèi)個都是一樣數(shù)組,知道這個是數(shù)組了就知道下面知道為什么會使用遍歷了.
for或while:因為表單過來的是數(shù)組我們就可以遍歷數(shù)組然后對數(shù)據(jù)進行保存了,如下代碼:
while(list($name,$value)=each($_POST)){ 或
for($i=0;$i<count($sp_name);$i++){ 兩個實現(xiàn)結果是一樣的.
希望本文所述對大家的php程序設計有所幫助。