利用PHP將資料存入MySQL PHP 5 Tutorial http://www.w3schools.com/php/ 範例: <!DOCTYPE html> <html> <body> <?php echo "My first PHP script!"; ?> </body> </html> 存成first.php (utf-8),上傳到學校網路空間 (於html建立mis資料夾),再到瀏覽器測試 http://www1.pu.edu.tw/~帳號/mis/first.php 若改成 echo "楊子青的第一個PHP動態網頁"; 執行後變亂碼
1. PHP正確顯示中文碼:作法1 加入head網頁標籤,宣告utf-8編碼方式 <!DOCTYPE html> <meta charset="utf-8"> </head> <body> <?php echo "楊子青的第一個PHP動態網頁"; ?> </body> </html>
PHP正確顯示中文碼:作法2 全部去掉html網頁標籤,直接在php宣告 <?php header("Content-Type:text/html; charset=utf-8"); echo "楊子青的第一個PHP動態網頁"; ?>
2. PHP 5 MySQLi Functions https://www.w3schools.com/php/php_ref_mysqli.asp
PHP mysqli_connect() Function http://www.w3schools.com/php/func_mysqli_connect.asp mysql主機, 資料庫使用者, 資料庫密碼, 資料庫名稱
先確認自己的MySQL資料庫 db4free.net (以下是老師的資料庫) https://db4free.net/phpMyAdmin/ 資料庫名稱: mismis 使用者: pucsim 密碼及密碼驗證: 12345678 port 預設為3306.
利用PHP連線到自己的SQL Server header("Content-Type:text/html; charset=utf-8"); $con = mysqli_connect("db4free.net","pucsim","12345678","mismis"); // Check connection if (mysqli_connect_errno()) { echo "無法連線到MySQL: " . mysqli_connect_error(); } ?> 存成insert.php上傳,如果資料庫資訊有誤(例如密碼錯誤): mysql主機, 資料庫使用者, 資料庫密碼, 資料庫名稱
PHP mysqli_query() Function http://www.w3schools.com/php/func_mysqli_query.asp
3. 新增一筆記錄 存成insert.php上傳,用瀏覽器執行 <?php header("Content-Type:text/html; charset=utf-8"); $con = mysqli_connect("db4free.net","pucsim","12345678","mismis"); // Check connection if (mysqli_connect_errno()) { echo "無法連線到MySQL: " . mysqli_connect_error(); } // Perform queries mysqli_query($con,"INSERT INTO Member (UserName,UserPass,UserBest) VALUES ('test','xyz',33)"); mysqli_close($con); ?> 存成insert.php上傳,用瀏覽器執行
查詢關聯表內容
修改1:提供新增是否成功訊息 此時若再執行一次,會出現錯誤訊息 (此因帳號為主鍵,不允許重複) <?php header("Content-Type:text/html; charset=utf-8"); $con = mysqli_connect("db4free.net","pucsim","12345678","mismis"); // Check connection if (mysqli_connect_errno()) { echo "無法連線到MySQL: " . mysqli_connect_error(); } // Perform queries $sql="INSERT INTO Member (UserName,UserPass,UserBest) VALUES ('test','xyz',33)"; if (mysqli_query($con,$sql)){ echo "您的資料新增成功"; else { echo "您的資料新增失敗"; mysqli_close($con); ?> 此時若再執行一次,會出現錯誤訊息 (此因帳號為主鍵,不允許重複)
修改2:密碼欄位加密 <?php header("Content-Type:text/html; charset=utf-8"); $con = mysqli_connect("db4free.net","pucsim","12345678","mismis"); // Check connection if (mysqli_connect_errno()) { echo "無法連線到MySQL: " . mysqli_connect_error(); } // Perform queries $sql="INSERT INTO Member (UserName,UserPass,UserBest) VALUES ('test2',SHA1('xyz'),33)"; if (mysqli_query($con,$sql)){ echo "您的資料新增成功"; else { echo "您的資料新增失敗"; mysqli_close($con); ?>
中文欄位值也OK <?php header("Content-Type:text/html; charset=utf-8"); $con = mysqli_connect("db4free.net","pucsim","12345678","mismis"); // Check connection if (mysqli_connect_errno()) { echo "無法連線到MySQL: " . mysqli_connect_error(); } // Perform queries $sql="INSERT INTO Member (UserName,UserPass,UserBest) VALUES ('靜宜資管',SHA1('xyz'),33)"; if (mysqli_query($con,$sql)){ echo "您的資料新增成功"; else { echo "您的資料新增失敗"; mysqli_close($con); ?>
4. 讓PHP讀取使用者輸入之變數 參數傳遞方法1: GET (網址傳遞參數,較不安全) <?php header("Content-Type:text/html; charset=utf-8"); $user = $_GET['user']; $pwd = $_GET['pwd']; $best = $_GET['best']; echo $user."; ".$pwd."; ".$best; ?> 存成get.php (utf-8)上傳,再到瀏覽器測試 http://www1.pu.edu.tw/~帳號/mis/get.php?user=楊子青&pwd=123456&best=50 ?後接欲傳入的參數 並以&相連接
讓PHP讀取使用者輸入之變數 參數傳遞方法2: POST (非透過網址參數,較安全) <?php header("Content-Type:text/html; charset=utf-8"); $user = $_POST['user']; $pwd = $_POST['pwd']; $best = $_POST['best']; echo $user."; ".$pwd."; ".$best; ?> 存成post.php (utf-8)上傳,再到瀏覽器測試 http://www1.pu.edu.tw/~帳號/mis/post.php?user=楊子青&pwd=123456&best=50
GET與POST比較 http://www.w3schools.com/tags/ref_httpmethods.asp 於網址列顯示傳遞參數,安全性堪慮 參數傳遞較安全
如何將參數透過POST傳遞? 撰寫網頁,利用表單方式傳遞參數 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </head> <body> <form action="post.php" method="post"> 帳號: <input type="text" name="user"><br> 密碼: <input type="password" name="pwd"><br> 最高分: <input type="number" name="best" min="0" max="100"><br> <input type="submit" value="送出"> </form> </body> </html> 存成form.html (utf-8)上傳,再到瀏覽器測試 http://www1.pu.edu.tw/~帳號/mis/form.html,輸入測試資料
5.表單及POST傳遞參數,新增資料 利用表單方式傳遞參數,呼叫insert.php新增資料 <!DOCTYPE html> <html> <head> <meta charset="utf-8"></head> <body> <form action="insert.php" method="post"> 帳號: <input type="text" name="user"><br> 密碼: <input type="password" name="pwd"><br> 最高分: <input type="number" name="best" min="0" max="100"><br> <input type="submit" value="新增資料"> </form> </body> </html> 存成insert.html (utf-8)上傳
讓insert.php傳入參數 <?php header("Content-Type:text/html; charset=utf-8"); $con = mysqli_connect("db4free.net","pucsim","12345678","mismis"); // Check connection if (mysqli_connect_errno()) { echo "無法連線到MySQL: " . mysqli_connect_error(); } $user = $_POST['user']; $pwd = $_POST['pwd']; $best = $_POST['best']; // Perform queries $sql="INSERT INTO Member (UserName,UserPass,UserBest) VALUES ('$user',SHA1('$pwd'),'$best')"; if (mysqli_query($con,$sql)){ echo "您的資料新增成功"; else { echo "您的資料新增失敗"; mysqli_close($con); ?>