自兩天的折騰後,我還是回來踏實的打程式吧!,今天主要是做各個需要傳值得表單,傳到各個資料表,今天先來做管理員的上傳商品好了。
我的上傳表單:
<form class="" action="./upload.php" method="post"enctype="multipart/form-data">
<table>
<tr><td>商品名稱<td><input type="text" name="gdname" value="">
<tr><td>商品圖片<td><input type="file" name="gdpic" value="">
<tr><td>商品敘述<td><textarea name="gdcontent" rows="8" cols="80"></textarea>
<tr><td>商品價格<td><input type="number" name="gdprice" value="">
<tr><td colspan="2"><input type="submit" name="" value="送出">
</table>
</form>
我是用PDO寫得,比較方便 只要改一下$link的資料就可以了,推一下PDO的用法,網路上很多資料可以參考,順便抱怨一下,到現在還是有很多書都在用mysql()函式,這麼老的東西別再教了,拜託~~!
<?php
$link = array(
'host' => "localhost",
'port' => "8889",
'account' => "root",
'password' => "root",
'dbname' => "buysite"
);
$dbconnect = 'mysql:host='.$link['host'].';port='.$link['port'].';dbname='.$link['dbname'];
// echo $dbconnect ;
// try 判斷是否連上 否:顯示訊息
try {
$connect = new PDO($dbconnect,$link['account'],$link['password']);
$connect -> query("SET NAMES 'utf8'");
$connect -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo "Connection failed: ".$e->getMessage();
exit();
}
?>
首先我比較特殊的是我是直接把照片傳至imgur,減少網站所佔的空間,這個算是很實用的API功能,這是一位16的印度學生寫的順便推他的部落格,超強的!!
基本上我的管理者上傳商品的功能已經做好了。
<?php
ini_set("display_errors", "On");
date_default_timezone_set("Asia/Taipei");
$name = $_POST['gdname'];
$gdpic = $_FILES['gdpic'];
$fileName = $gdpic['tmp_name'];
$content = $_POST['gdcontent'];
$price = $_POST['gdprice'];
$uptime = date("Y-m-d H:i:s");
if($_FILES["fileName"]["error"]==0){
$client_id = "5b982131a30952e";
$handle = fopen($fileName,"r");
$data = fread($handle,filesize($fileName));
$pvars = array('image' => base64_encode($data));
$timeout = 30;
$curl = curl_init();
curl_setopt($curl,CURLOPT_URL,'https://api.imgur.com/3/image.json');
curl_setopt($curl,CURLOPT_TIMEOUT,$timeout);//讀取時間30秒為上限
curl_setopt($curl,CURLOPT_HTTPHEADER,array('Authorization: Client-ID ' . $client_id));
curl_setopt($curl,CURLOPT_POST,1);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, false);//關閉SSL安全
curl_setopt($curl,CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_POSTFIELDS, $pvars);
$out = curl_exec($curl);
curl_close ($curl);
$pms = json_decode($out,true);
$filelink=$pms['data']['link'];
}else {
echo"fileErrorCode:".$_FILES["file"]["error"];
}
require_once "../method/connect.php";
$insert = $connect -> prepare( "INSERT INTO
goods (
name,
picture,
content,
price,
uptime
) VALUES (
?,?,?,?,?
)");
$insert -> execute(
array( $name,
$filelink,
$content,
$price,
$uptime
));
header("location:".$_SERVER["HTTP_REFERER"]);
?>
?>
今天就先這樣吧明天要做會員註冊系統,我要繼續看書了掰掰~
我的專案會放在github,我只會把程式放上來但系統架構還是要去github看才會明白