緣起
我們公司最近要徵募新人,說到要給新人出怎麼樣的題目….談到出個簡單的部落格實做(包含登入登出)….說真的這個題目的範圍可大可小….對我來說就只是要考所謂CRUD的操作-就是新增-查詢-更新-刪除。於是就問我這個非常菜的菜鳥能不能在這短時間內做出來…..說真的我是有點躍躍欲試…於是便開啟這樣的練習….
所有程式碼都在這裡:https://github.com/r567tw/easy_blog_practice
這裡我應用了PDO的技術,並且也記錄一些筆記……
Initial
1
|
$db = new PDO($dsn, $user,$password );
|
Create
1
2
3
4
|
$insert=$db->prepare("insert into posts (title,post) values (:title,:post)");
$insert->bindParam(":title",$_POST['title']);
$insert->bindParam(":post",$_POST['posts']);
$insert->execute();
|
Review
1
2
3
|
$sql="select * from posts";
$posts=$db->query($sql);
$posts=$posts->fetchAll();
|
Update
1
2
3
4
5
6
|
$update=$db->prepare("update posts set title=:title,post=:post,update_time=:update where id=:id");
$update->bindParam(":title",$_POST['title']);
$update->bindParam(":post",$_POST['posts']);
$update->bindParam(":update",date("Yⓜ️d H:i:s"));
$update->bindParam(":id",$_POST['id']);
$update->execute();
|
Delete
1
2
3
|
$delete=$db->prepare("delete from posts where id=:id");
$delete->bindParam(":id",$_GET['id']);
$delete->execute();
|
判斷是否有成功執行
登入登出
首先必須先開啟Session
當登入時必須宣告一個session的參數
之後就判斷這個session實數就能時做出登入登出啦
這麼簡單的東西我花了一個半小時做完~ 是不是有點久阿哈哈?
工作上學習到的小技巧
1簡單輸出成Excel的程式碼
1
|
header("Content-type:application/vnd.ms-excel"); header("Content-Disposition:filename=filename.xls");
|
接下來只要在此段程式碼底下宣告HTML的Table,只要打開這個網頁就會輸出Excel檔案
2.MySQL 應用CASE 子句
1
|
SELECT count(CASE ("欄位名") WHEN "條件1" THEN 1 ELSE NULL END) FROM "表格名";
|
可以在同一條SQL查詢出來這個欄位在這個條件下所出來的個數…
這樣如果要計算另外一個條件就不用再用另外的SQL查詢這麼蠢的操作了
未來展望
說真的! 這些都是很多Native php的東西,做多了就是一直在重複造輪子….這種叫做練功哈哈,這網路上有很多人做了很多東西,就像是框架,一下子東西就出來了! 這都是我應該要學習的地方~~
小君曰:為自己加油,希望可以越來越強!!