最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

php批量添加數(shù)據(jù)與批量更新數(shù)據(jù)的實現(xiàn)方法

 更新時間:2014年12月16日 11:47:52   投稿:shichen2014  
這篇文章主要介紹了php批量添加數(shù)據(jù)與批量更新數(shù)據(jù)的實現(xiàn)方法,涉及針對表單的處理與sql語句的靈活使用,非常具有實用價值,需要的朋友可以參考下

本文實例講述了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);

表單提交頁面,代碼如下:

復制代碼 代碼如下:
<form name="form1" method="post" action="index_ok.php">
<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ù)庫連接頁,代碼如下:
復制代碼 代碼如下:
<?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());
?>

表單處理頁,代碼如下:
復制代碼 代碼如下:
<?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"提交成功";
    else
        echo"提交失敗";}
}
?>

批量更新數(shù)據(jù)

主要通過while, list(),each()函數(shù)來實理數(shù)據(jù)的批量更新,list()函數(shù)用于一次性為多個變量賦值,代碼如下:

復制代碼 代碼如下:
<?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="激活">
<input type="submit" name="submit2" value="凍結">
</tr>
</form>

表單處理頁,代碼如下:
復制代碼 代碼如下:
<?php session_start(); include("conn/conn.php")
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程序設計有所幫助。

相關文章

最新評論

石家庄市| 安龙县| 永和县| 镇坪县| 丹棱县| 阿巴嘎旗| 花莲市| 邵阳县| 临高县| 黎平县| 诏安县| 绥棱县| 利辛县| 大英县| 岐山县| 双峰县| 怀来县| 灌云县| 泸定县| 新民市| 错那县| 托里县| 松桃| 浮山县| 布尔津县| 大港区| 霸州市| 华阴市| 楚雄市| 商丘市| 吉首市| 綦江县| 普安县| 化州市| 霞浦县| 广州市| 南靖县| 陇南市| 镇坪县| 五原县| 都兰县|