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

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

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

本文實(shí)例講述了php批量添加數(shù)據(jù)與批量更新數(shù)據(jù)的實(shí)現(xiàn)方法。分享給大家供大家參考。具體分析如下:

php如果要批量保存數(shù)據(jù)我們只要使用sql的insert into語句就可能實(shí)現(xiàn)數(shù)據(jù)批量保存了,如果是更新數(shù)據(jù)使用update set就可以完成更新了,操作方法都非常的簡單,下面整理兩個(gè)例子.

批量數(shù)據(jù)錄入

設(shè)計(jì)方法:同時(shí)提交多條表單記錄,為每一條記錄設(shè)置相同的文本域名稱,然后在表單處理頁中,通過for循環(huán)來讀取提取表單提交的數(shù)據(jù),最后以數(shù)據(jù)的形式將數(shù)據(jù)逐條添加到數(shù)據(jù)庫中.

其中,應(yīng)用一個(gè)count()函數(shù)來獲取數(shù)組中元素的個(gè)數(shù).int count(mixed var);

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

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

表單處理頁,代碼如下:
復(fù)制代碼 代碼如下:
<?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í)理數(shù)據(jù)的批量更新,list()函數(shù)用于一次性為多個(gè)變量賦值,代碼如下:

復(fù)制代碼 代碼如下:
<?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="凍結(jié)">
</tr>
</form>

表單處理頁,代碼如下:
復(fù)制代碼 代碼如下:
<?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=="凍結(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處理接受頁面都會(huì)使用for 或while來實(shí)現(xiàn)遍歷了,下面我就簡單的給大家分析這兩個(gè)例子.

counts[]:這個(gè)在表單中是代表數(shù)組,如果你有10個(gè)表單那么我們name=counts[] 意思他們內(nèi)個(gè)都是一樣數(shù)組,知道這個(gè)是數(shù)組了就知道下面知道為什么會(huì)使用遍歷了.

for或while:因?yàn)楸韱芜^來的是數(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)文章

最新評(píng)論

屯留县| 抚宁县| 云浮市| 冀州市| 和平县| 绍兴市| 潜江市| 渝中区| 铜山县| 洛阳市| 藁城市| 靖州| 天镇县| 安多县| 石台县| 永城市| 夏邑县| 库车县| 巴东县| 渑池县| 商都县| 广宁县| 大宁县| 石阡县| 临汾市| 黄石市| 广东省| 宁陕县| 青冈县| 新邵县| 寿光市| 汕头市| 蒲城县| 新宾| 河北省| 沾益县| 白水县| 台中县| 伊川县| 沭阳县| 栾城县|