php批量添加數(shù)據(jù)與批量更新數(shù)據(jù)的實(shí)現(xiàn)方法
本文實(shí)例講述了php批量添加數(shù)據(jù)與批量更新數(shù)據(jù)的實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:
php如果要批量保存數(shù)據(jù)我們只要使用sql的insert into語(yǔ)句就可能實(shí)現(xiàn)數(shù)據(jù)批量保存了,如果是更新數(shù)據(jù)使用update set就可以完成更新了,操作方法都非常的簡(jiǎn)單,下面整理兩個(gè)例子.
批量數(shù)據(jù)錄入
設(shè)計(jì)方法:同時(shí)提交多條表單記錄,為每一條記錄設(shè)置相同的文本域名稱,然后在表單處理頁(yè)中,通過(guò)for循環(huán)來(lái)讀取提取表單提交的數(shù)據(jù),最后以數(shù)據(jù)的形式將數(shù)據(jù)逐條添加到數(shù)據(jù)庫(kù)中.
其中,應(yīng)用一個(gè)count()函數(shù)來(lái)獲取數(shù)組中元素的個(gè)數(shù).int count(mixed var);
表單提交頁(yè)面,代碼如下:
<tr>
<td>商品名稱</td>
<td>編號(hào)</td>
<td>單價(jià)</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ù)庫(kù)連接頁(yè),代碼如下:
$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());
?>
表單處理頁(yè),代碼如下:
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ù)
主要通過(guò)while, list(),each()函數(shù)來(lái)實(shí)理數(shù)據(jù)的批量更新,list()函數(shù)用于一次性為多個(gè)變量賦值,代碼如下:
<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="凍結(jié)">
</tr>
</form>
表單處理頁(yè),代碼如下:
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=="凍結(jié)"){
while(list($name,$value)=each($_POST)){
$result=mysql_query("update tb_user set operation='凍結(jié)' where id='".$name."'");
if($result==true){
echo "<script> alert('凍結(jié)成功');window.location.href='index.php';</script>";}}
}
?>
總結(jié):心細(xì)的朋友會(huì)發(fā)現(xiàn)兩個(gè)例子都有幾個(gè)共同點(diǎn),一個(gè)是表單from的表單名是以counts[]數(shù)組形式了,而在php處理接受頁(yè)面都會(huì)使用for 或while來(lái)實(shí)現(xiàn)遍歷了,下面我就簡(jiǎn)單的給大家分析這兩個(gè)例子.
counts[]:這個(gè)在表單中是代表數(shù)組,如果你有10個(gè)表單那么我們name=counts[] 意思他們內(nèi)個(gè)都是一樣數(shù)組,知道這個(gè)是數(shù)組了就知道下面知道為什么會(huì)使用遍歷了.
for或while:因?yàn)楸韱芜^(guò)來(lái)的是數(shù)組我們就可以遍歷數(shù)組然后對(duì)數(shù)據(jù)進(jìn)行保存了,如下代碼:
while(list($name,$value)=each($_POST)){ 或
for($i=0;$i<count($sp_name);$i++){ 兩個(gè)實(shí)現(xiàn)結(jié)果是一樣的.
希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。
相關(guān)文章
php通過(guò)baihui網(wǎng)API實(shí)現(xiàn)讀取word文檔并展示
這篇文章主要介紹了php通過(guò)baihui網(wǎng)API實(shí)現(xiàn)讀取word文檔并展示的相關(guān)資料,需要的朋友可以參考下2015-06-06php實(shí)現(xiàn)的支持?jǐn)帱c(diǎn)續(xù)傳的文件下載類
這篇文章主要介紹了php實(shí)現(xiàn)的支持?jǐn)帱c(diǎn)續(xù)傳的文件下載類及其用法實(shí)例,詳細(xì)講述了PHP實(shí)現(xiàn)斷點(diǎn)續(xù)傳下載的原理及代碼實(shí)現(xiàn)方法,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-09-09