ThinkPHP架構 _ 學習13

來源:互聯網
上載者:User

標籤:修改   new   roman   family   exec   ack   font   strong   智能   

【收集表單資料入庫操作】
  1. 製作一個表單
  2. 通過$_POST收集資訊
  3. 通過create()方法實現資料收集,該方法對於非法的欄位會自動進行過濾(它只是收集資料,不儲存資料,如果要儲存資料,則需要add方法)

 

  1. 在create()收集表單方法內部會自動過濾非法的欄位資訊(如果收集的資料欄位名字不正確的話,那麼它會自動過濾掉這個欄位)

 

 

【資料修改操作】

select()

add()

save()  實現資料修改,返回受影響的記錄條數

delete()

具體有兩種方式實現資料修改,與添加類似(數組、AR方式)

  1. 數組方式

a) $goods = D(“Goods”);

b) $ar = array(‘goods_name’=>’lenovo手機’,’goods_price’=>1200);

c) $goods ->where(‘goods_id>50’)-> save($ar);

  1. AR方式

a) $goods = D(“Goods”);

c) $goods -> goods_name = “三星手機”;

d) $goods -> goods_price = 2000;

e) $goods -> where(‘goods_price>10000’)->save();

以上兩種方式如果可行,即要修改全部資料

以上sql語句從技術上可行,從業務上不可行(事故)

tp架構有智能考慮,以上情況的sql語句不被允許執行。

如何執行:

① 明確告訴系統那條sql語句被update更新

② 可以設定where進行sql語句更新操作

 

save()  方法傳回值

0:之前沒有問題,執行前後資料沒有變化

自然數:受影響的記錄條數

false:執行失敗

 

 

【資料刪除及執行原生sql語句】

select()

add()

save()  

delete()  返回受影響的記錄條數

 

$goods -> delete(30);   刪除主索引值等於30的記錄資訊

$goods -> delete(“10,12,13”);   刪除主索引值等於10 12 13的三條記錄

$goods -> where(“goods_id>60”)->delete()   把合格記錄都給刪除

 

 

執行原生sql語句
  1. 查詢語句query()  返回一個二維數組資訊
  2. 添加、修改、刪除 execute()  返回受影響的記錄條數

$goods = D(“Goods”);

$sql = “select * from sw_goods”;

$rst = $goods -> query($sql);

 

$sql = “select goods_category_id,avg(goods_price) from sw_goods group by goods_category_id having  avg(goods_price)>1000”;

$goods -> query($sql);

 

$sql = “update sw_goods set goods_name = ‘htc_two’ where goods_id=100”;

$goods -> execute($sql);

ThinkPHP架構 _ 學習13

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.