งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

การเชื่อมต่อฐานข้อมูล

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "การเชื่อมต่อฐานข้อมูล"— ใบสำเนางานนำเสนอ:

1 การเชื่อมต่อฐานข้อมูล
MySQL ด้วยภาษา PHP

2 ภาษา SQL SQL ย่อมาจากคำว่า Structured Query Language เป็นภาษา มาตรฐานกลางที่ใช้สำหรับจัดการข้อมูลในฐานข้อมูลด้านต่างๆ เช่น Microsoft Access, MySQL, Oracle เป็นต้น โดยคำสั่งที่ เกี่ยวข้องกับการจัดการฐานข้อมูล ประกอบด้วย คำสั่ง อ่านข้อมูลจากฐานข้อมูล เพิ่มข้อมูลลงในฐานข้อมูล แก้ไขข้อมูลในฐานข้อมูล ลบข้อมูลในฐานข้อมูล

3 1 คำสั่งที่ใช้สำหรับอ่านข้อมูลจากฐานข้อมูล
SELECT select_list FROM table_source [WHERE search_condition] [GROUP BY group_by_expression] [ORDER BY order_expression [ASC | DESC] ] โดยที่ - SELECT หมายถึง การเลือกข้อมูล - select_list หมายถึงชื่อฟิลด์ในตารางที่ต้องการ - From table_source หมายถึงเลือกข้อมูลจากตาราง table_source คือชื่อตารางที่ใช้ในเงื่อนไขการเลือกข้อมูล - WHERE search_ condition หมายถึง เงื่อนไขการเลือกข้อมูล - GROUP BY หมายถึงการจัดกลุ่ม - group_by_expression หมายถึง เงื่อนไขในการจัดกลุ่มข้อมูล - ORDER BY หมายถึง การจัดเรียงข้อมูลตามเงื่อนไข ASC ( Ascending) คือ เรียงลำดับจากน้อยไปมาก หรือ DESC (Descending) คือ เรียงลำดับจากมากไปหาน้อย หากไม่ระบุจะหมายถึงการจัดการข้อมูลแบบ ASC

4 ตัวอย่างการอ่านข้อมูลจากฐานข้อมูล
(1) select * from employees หมายถึง อ่านค่าข้อมูลทุกฟิลด์จากตาราง employees (2) select code,name,surname from employees หมายถึง อ่านข้อมูลฟิลด์ code, name และ surname จากตาราง employees (3) select * from employees where id= '0001' หมายถึง อ่านค่าข้อมูลทุกฟิลด์จากตาราง employees ที่มี id เท่ากับ '0001' (4) select id,name,surname,salary from employees where salary>10000 order by id หมายถึง อ่านค่าข้อมูล ฟิลด id,name,surname และ salary จากตาราง employees โดยที่ salary มีค่ามากกว่า และจัดเรียงข้อมูลตามฟิลด์ id จากน้อยไปมาก เนื่องจากไม่ระบุเงื่อนไขการจัดเรียง จะถือว่าเป็นการจัดเรียงแบบ ASC จากน้อยไปมาก (5) select id,name,surname,salary,position_name from employess,position where employees.position_id=position.position_id หมายถึง อ่านค่าข้อมูล ฟิล์ด id,name,surname,salary,position_name จากตาราง employees และตาราง position โดยที่ฟิลด์ position_id จากตาราง employees มีค่าเท่ากับฟิลด์ position_id จากตาราง position (6) select salary,position_id from employees group by position_id หมายถึง อ่านค่าข้อมูล ฟิลด์ salary และ position_id จากตาราง employees จัดกลุ่มตาม posstion_id

5 2 คำสั่งที่ใช้สำหรับเพิ่มข้อมูลลงในฐานข้อมูล
INSERT INTO tablename [(column_list)] VALUES (value_list) โดยที่ - INSERT INTO หมายถึงคำสั่งที่ใช้ระบุการเพิ่มข้อมูล - table_name หมายถึง ชื่อตารางที่ต้องการเพิ่มข้อมูล - [(column_list)] หมายถึง รายการของฟิล์ดในตารางที่ต้องการเพิ่มข้อมูล จะไม่ระบุก็ได้หากเป็น การเพิ่มข้อมูลครบทุกฟิลด์ในตาราง - VALUES (value_list) หมายถึง ค่าของข้อมูลตามฟิลด์ที่ระบุที่ต้องการเพิ่มลงในตาราง

6 ตัวอย่างการเพิ่มข้อมูลลงในฐานข้อมูล
(1) insert into goods(goods_id,goods_name,type_id) values ('001', 'pencil','01') หมายถึง เพิ่มข้อมูลลงตาราง goods โดยที่ฟิลด์ goods_id มีค่าเท่ากับ '001', ฟิลด์ goods_name มีค่าเท่ากับ 'pencil' และฟิลด์ type_id มีค่าเท่ากับ '01' (2) insert into goods values ('001', 'pencil','01') หมายถึง เพิ่มข้อมูลลงตาราง goods ทุกฟิลด์ โดยมีค่าตามข้อมูลที่ระบุ

7 3 คำสั่งที่ใช้สำหรับแก้ไขข้อมูลในฐานข้อมูล
มีรูปแบบการใช้งานดังนี้ UPDATE table_name SET Column_name = value [WHERE search_condition] โดยที่ - UPDATE หมายถึงคำสั่งให้แก้ไขข้อมูลในตาราง - table_name หมายถึง ชื่อตารางที่ต้องการแก้ไขข้อมูล - SET Column_name = value หมายถึง ระบุคอลัมน์และค่าข้อมูลที่ต้องการแก้ไข - WHERE search_ condition หมายถึง เงื่อนไขการเลือกข้อมูลที่แก้ไข จะไม่ระบุก็ได้

8 ตัวอย่างคำสั่งแก้ไขข้อมูลในฐานข้อมูล
(1) update goods set goods_name='pen' หมายถึง แก้ไขข้อมูลในตาราง goods กำหนดให้ฟิลด์ goods_name มีค่าเท่ากับ 'pen' ดังนั้นข้อมูล ในตาราง goods ฟิล์ด goods_name ทุกตัวจะถูกแก้ไขเป็น 'pen' (2) update goods set goods_name='pen',type_id='01' where goods_id='001' หมายถึง แก้ไขข้อมูลในตาราง goods กำหนดให้ฟิลด์goods_name มีค่าเท่ากับ 'pen' และฟิลด์ type_id มีค่าเท่ากับ '01' โดยเงื่อนไขคือฟิลด์ goods_id จะต้องมีค่าเท่ากับ '001',

9 4 คำสั่งที่ใช้สำหรับลบข้อมูลในฐานข้อมูล
DELETE [FROM] table_name [WHERE <search_condition> ] โดยที่ - DELETE [FROM] หมายถึง คำสั่งที่ใช้ในการลบข้อมูลออกจากฐานข้อมูล - table_name หมายถึง ชื่อตารางที่ต้องการลบข้อมูล - [ WHERE <search_condition>] } หมายถึงเงื่อนไขในการลบข้อมูลในตาราง

10 ตัวอย่างการใช้งานคำสั่งลบข้อมูล
(1) delete from goods where goods_id='001' หมายถึงลบข้อมูลจากตาราง goods ที่ค่าของฟิลด์ goods_id มีค่าเท่ากับ '001', (2) delete from goods หมายถึงลบข้อมูลจากตาราง goods ทุกรายการ

11 PHP 5 MySQLi Functions

12 ขั้นตอนการเชื่อมต่อฐานข้อมูล MySQL ด้วยภาษา PHP
ขั้นตอนที่ 1 เชื่อมต่อเซิร์ฟเวอร์ที่จัดการฐานข้อมูล MySQL ของเว็บไซต์ ขั้นตอนที่ 2 เลือกฐานข้อมูลที่ต้องการเชื่อมต่อ ขั้นตอนที่ 3 สร้างคำสั่ง SQL เพื่อจัดการข้อมูลในฐานข้อมูล เช่น คำสั่งอ่านข้อมูล คำสั่งเพิ่มข้อมูล คำสั่งแก้ไขข้อมูล และคำสั่งลบข้อมูลใน ฐานข้อมูล โดยเก็บไว้ในตัวแปรชนิดข้อความ เพื่อนำไปใช้ในขั้นตอนที่ 4 ขั้นตอนที่ 4 คิวรี่ข้อมูล คือการส่งคำสั่ง SQL ไปยังฐานข้อมูล นำไปใช้ในขั้นตอนที่ 5 ขั้นตอนที่ 5 การแสดงผลลัพธ์จากการคิวรี่ข้อมูล ขั้นตอนที่ 6 ยกเลิกการเชื่อมต่อฐานข้อมูล

13 ฟังก์ชั่นที่เกี่ยวข้องกับการทำงานบนฐานข้อมูล MySQL ด้วยภาษา PHP
(1) ฟังก์ชั่นติดต่อกับระบบฐานข้อมูล (2) ฟังก์ชั่นยกเลิกการติดต่อกับระบบฐานข้อมูล (3) ฟังก์ชั่นเลือกฐานข้อมูล (4) ฟังก์ชั่นคิวรี่ข้อมูล (5) ฟังก์ชั่นการตรวจสอบจำนวนแถวที่เปลี่ยนแปลงข้อมูล (6) ฟังก์ชั่นนับจำนวนข้อมูลผลลัพธ์ (7) ฟังก์ชั่นอ่านข้อมูล

14 ฟังก์ชั่นติดต่อกับระบบฐานข้อมูล

15 การเชื่อมต่อฐานข้อมูล
สามารถกระทำได้ 3 รูปแบบคือ Object-oriented Procedural PDO

16 การเชื่อมต่อฐานข้อมูล แบบ Object-oriented
<?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = new mysqli($servername, $username, $password); // Check connection if ($conn->connect_error) {     die("Connection failed: " . $conn->connect_error); }  echo "Connected successfully"; ?>

17 การเชื่อมต่อฐานข้อมูล แบบ Procedural
<?php $servername = "localhost"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password); // Check connection if (!$conn) {     die("Connection failed: " . mysqli_connect_error()); } echo "Connected successfully"; ?>

18 การเชื่อมต่อฐานข้อมูล แบบ PDO
<?php $servername = "localhost"; $username = "username"; $password = "password"; try {     $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password);     // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);     echo "Connected successfully";      } catch(PDOException $e)     {     echo "Connection failed: " . $e->getMessage();     } ?>

19 การเชื่อมต่อฐานข้อมูล
โดยที่ - hostname หมายถึง ชื่อของเครื่องที่ติดตั้งระบบฐานข้อมูลของ MySQL - username หมายถึง ชื่อของผู้ใช้ที่มีสิทธิ์เข้าใช้ระบบฐานข้อมูลของ MySQL - password หมายถึง รหัสผ่านของ username

20 ฟังก์ชั่นยกเลิกการติดต่อกับระบบฐานข้อมูล

21 ฟังก์ชั่นยกเลิกการติดต่อกับระบบฐานข้อมูล
- MySQLi Object-Oriented $conn->close(); - MySQLi Procedural mysqli_close($conn); - PDO $conn = null; โดยที่ $conn หมายถึง ตัวแปรการติดต่อกับระบบฐานข้อมูล

22 ฟังก์ชั่นเลือกฐานข้อมูล

23 ฟังก์ชั่นในการเลือกฐานข้อมูล
- MySQLi Object-Oriented $conn = new mysqli($servername, $username, $password, $dbname); - MySQLi Procedural $conn = mysqli_connect($servername, $username, $password, $dbname); - PDO $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $dbname หมายถึง ชื่อระบบฐานข้อมูลที่ต้องการเชื่อมต่อ

24 ฟังก์ชั่นคิวรี่ข้อมูล

25 ฟังก์ชั่นในการคิวรี่ข้อมูล
MySQLi Object-Oriented $conn->query($sql) MySQLi Procedural mysqli_query($conn, $sql) PDO  $conn->exec($sql); โดยที่ - $sql คือ คำสั่ง SQL ในรูปแบบของข้อความที่ใช้สำหรับจัดการข้อมูลในฐานข้อมูล

26 ตัวอย่างการเชื่อมต่อฐานข้อมูล

27 ตัวอย่างการเชื่อมต่อฐานข้อมูล (บันทึกข้อมูลลงตาราง)
<!DOCTYPE html> <html lang="th"> <head> <title>การเชื่อมต่อฐานข้อมูล MYSQL</title> <meta charset="utf-8"> <meta name="viewport" content="width=device- width, initial-scale=1"> </head> <body> <? $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; $conn = new mysqli($servername, $username, $password, $dbname); $conn->query ("insert into goods(goods_id,goods_name,type_id) values ('002', 'pencil','01')"); If ($conn) { echo "เพิ่มข้อมูลเรียบร้อยแล้ว"; } $conn->close(); ?> </body> </html>

28 ฟังก์ชั่นการตรวจสอบจำนวนแถวที่เปลี่ยนแปลงข้อมูล

29 ฟังก์ชั่นสำหรับตรวจสอบจำนวนแถวที่เปลี่ยนแปลงข้อมูล
รูปแบบคำสั่ง mysqli_affected_rows(connection); โดยที่ - connection หมายถึง ตัวแปรการติดต่อกับระบบฐานข้อมูล ใช้สำหรับแสดงผลลัพธ์จำนวนแถวที่เกิดการเปลี่ยนแปลงจากการเพิ่มข้อมูล หรือ ลบข้อมูล หรือปรับปรุงข้อมูลในตาราง

30 ตัวอย่างการใช้งานฟังก์ชั่น mysql_ affected _rows()
<!DOCTYPE html> <html lang="th"> <head> <title>การเชื่อมต่อฐานข้อมูล MYSQL</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <? $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; $conn = new mysqli($servername, $username, $password, $dbname); $conn->query("insert into goods(goods_id,goods_name,type_id) values ('004', 'pencil','01')"); If ($conn) { $num_rows =mysqli_affected_rows($conn); echo "เพิ่มข้อมูลเรียบร้อยแล้วจำนวน $num_rows เรคคอร์ด"; }

31 ฟังก์ชั่นนับจำนวนข้อมูลผลลัพธ์

32 ฟังก์ชั่นในการนับจำนวนข้อมูลผลลัพธ์ num_rows()
- MySQLi Object-Oriented $result->num_rows - MySQLi Procedural mysqli_num_rows($result) - PDO $result->num_rows  โดยที่ - result คือตัวแปรที่เก็บข้อมูลที่ได้จากการประมวลผลจากการ คิวรี่ข้อมูล ใช้กับการดึง ข้อมูลจากตารางมาแสดงผล

33 ฟังก์ชั่นอ่านข้อมูล

34 ฟังก์ชั่นในการอ่านข้อมูล
(1) fetch_assoc() (2) mysqli_fetch_row() (3) mysqli_fetch_array()

35 ฟังก์ชั่นในการอ่านข้อมูล
(1) fetch_assoc() อ่านผลลัพธ์จากการประมวลผลคำสั่ง SQL หลังจากส่งคำสั่ง SQL ไปประมวลผลโดยจะอ่าน ผลลัพธ์จากการประมวลผลแบบตามลำดับแถว เลื่อนพอยน์เตอร์ไปยังตำแหน่งที่ต้องการอ่านทีละ แถว ผลลัพธ์ที่ได้จะอยู่ในรูปแบบอะเรย์ รูปแบบคำสั่ง - MySQLi Object-Oriented $row=$result->fetch_assoc() - MySQLi Procedural $row=mysqli_fetch_assoc($result) โดยที่ resultคือตัวแปรที่เก็บข้อมูลที่ได้จากการประมวลผลจากคิวรี่ข้อมูล Index ของ อะเรย์ คือชื่อฟิลด์ที่อยู่ในตารางข้อมูล

36 ตัวอย่างการเชื่อมต่อฐานข้อมูล (การแสดงข้อมูลในตาราง) (1) fetch_assoc() MySQLi Object-Oriented
<!DOCTYPE html> <html lang="th"> <head> <title>การเชื่อมต่อฐานข้อมูล MYSQL แบบ MySQLi Object-Oriented </title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <? $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; $conn = new mysqli($servername, $username, $password, $dbname); $sql="select * from goods"; $result=$conn->query ($sql); if($result->num_rows>0){ while($row=$result->fetch_assoc()){ echo $row["goods_id"].$row["goods_nam e"].$row["type_id"]."<BR>"; }} $conn->close(); ?> </body> </html>

37 ตัวอย่างการเชื่อมต่อฐานข้อมูล (การแสดงข้อมูลในตาราง) (1) fetch_assoc() MySQLi Procedural
<!DOCTYPE html> <html lang="th"> <head> <title>การเชื่อมต่อฐานข้อมูล MYSQL แบบ MySQLi Procedural </title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <? $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; $conn = mysqli_connect($servername, $username, $password, $dbname); $sql="select * from goods"; $result=mysqli_query($conn, $sql); if(mysqli_num_rows($result)>0){ while($row=mysqli_fetch_assoc($res ult)){ echo $row["goods_id"].$row["goods_nam e"].$row["type_id"]."<BR>"; }} mysqli_close($conn); ?></body></html>

38 ฟังก์ชั่นในการอ่านข้อมูล
(2) mysqli_fetch_row เป็นฟังก์ชั่นอ่านผลลัพธ์จากการประมวลผลคำสั่ง SQL หลังจากส่ง คำสั่ง SQL ไปประมวลผลโดยจะอ่านผลลัพธ์จากการประมวลผลแบบตามลำดับแถว เลื่อนพอยน์เตอร์ไป ยังตำแหน่งที่ต้องการอ่านทีละแถว ผลลัพธ์ที่ได้จะอยู่ในรูปแบบอะเรย์ รูปแบบคำสั่ง - MySQLi Object-Oriented $row=$result->fetch_row() - MySQLi Procedural $row= mysqli_fetch_row($result) โดยที่ result คือตัวแปรที่เก็บข้อมูลที่ได้จากการประมวลผลจากการคิวรี่ข้อมูล Index ของ อะเรย์ คือ 0,1,…

39 ตัวอย่างการใช้งานฟังก์ชั่นอ่านข้อมูล MySQLi Object-Oriented fetch _row
<!DOCTYPE html> <html lang="th"> <head> <title>การเชื่อมต่อฐานข้อมูล MYSQL แบบ MySQLi Object-Oriented </title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> </head> <body> <? $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; $conn = new mysqli($servername, $username, $password, $dbname); $sql="select * from goods"; $result=$conn->query ($sql); if($result->num_rows>0){ while($row=$result->fetch_row()){ echo $row[0].$row[1].$row[2]."<BR>"; }} $conn->close(); ?> </body> </html>

40 ตัวอย่างการใช้งานฟังก์ชั่นอ่านข้อมูล MySQLi Procedural mysqli_fetch_row
<!DOCTYPE html> <html lang="th"> <head> <title>การเชื่อมต่อฐานข้อมูล MYSQL แบบ MySQLi Procedural </title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial- scale=1"> </head> <body> <? $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; $conn = mysqli_connect($servername, $username, $password, $dbname); $sql="select * from goods"; $result=mysqli_query($conn,$sql); if(mysqli_num_rows($result)>0){ while($row=mysqli_fetch_row($result)){ echo $row[0].$row[1].$row[2]."<BR>"; }} mysqli_close($conn); ?></body></html>

41 ฟังก์ชั่นในการอ่านและแสดงผลข้อมูล
( 3) mysqli_fetch_array() เป็นฟังก์ชั่นในการอ่านข้อมูลจาก result set ในรูปแบบของอะเรย์แบบคู่ ผลลัพธ์ของฟังก์ชั่นจะอยู่ในรูปแบบ key และ value โดยที่ key ก็คือชื่อฟิลด์หรือคอลัมน์ในฐานข้อมูล และ value คือข้อมูลที่อยู่ในฟิลด์หรือคอลัมน์นั้นๆ รูปแบบคำสั่ง - MySQLi Object-Oriented $row=$result->fetch_array() - MySQLi Procedural $row= mysqli_fetch_array ($result) โดยที่ result คือตัวแปรที่เก็บข้อมูลที่ได้จากการประมวลผลจากการคิวรี่ข้อมูล Index ของ อะเรย์ คือ 0,1,… หรือ ชื่อฟิลด์ที่อยู่ในตารางข้อมูล

42 ตัวอย่างการใช้งานฟังก์ชั่น MySQLi Object-Oriented fetch_array
<!DOCTYPE html> <html lang="th"> <head> <title>การเชื่อมต่อฐานข้อมูล MYSQL แบบ MySQLi Object-Oriented </title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial- scale=1"> </head> <body> <? $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; $conn = new mysqli($servername, $username, $password, $dbname); $sql="select * from goods"; $result=$conn->query ($sql); if($result->num_rows>0){ while($row=$result->fetch_array()){ echo $row[0].$row[1].$row[2]."<BR>"; }} $conn->close(); ?> </body> </html>

43 ตัวอย่างการใช้งานฟังก์ชั่น MySQLi Procedural mysqli_fetch_array
<!DOCTYPE html> <html lang="th"> <head> <title>การเชื่อมต่อฐานข้อมูล MYSQL แบบ MySQLi Procedural </title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial- scale=1"> </head> <body> <? $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "test"; $conn = mysqli_connect($servername, $username, $password, $dbname); $sql="select * from goods"; $result=mysqli_query($conn,$sql); if(mysqli_num_rows($result)>0){ while($row=mysqli_fetch_array($resu lt)){ echo $row[0].$row[1].$row[2]."<BR>"; }} mysqli_close($conn); ?></body></html>

44 กรณีศึกษา การเชื่อมต่อฐานข้อมูล

45 ตัวอย่างการพัฒนาเว็บไซต์เพื่อเชื่อมต่อฐานข้อมูล MySQL
ตัวอย่างการพัฒนาเว็บไซต์ระบบงานร้านขายหนังสือ ประกอบด้วยขั้นตอนการดำเนินงาน 4 ขั้นตอน ดังนี้ ขั้นตอนที่ 1 กำหนดวัตถุประสงค์และวางแผนการพัฒนาเว็บไซต์ระบบงานร้านขายหนังสือ โดย แสดงรายการหนังสือเพื่อให้ลูกค้าสามารถเลือกและสั่งซื้อหนังสือผ่านระบบออนไลน์ ขั้นตอนที่ 2 การกำหนดเนื้อหาเว็บไซต์ โดยแบ่งออกเป็น 2 ส่วนคือ ส่วนที่ 1 ส่วนจัดการข้อมูลหนังสือที่ขายในร้าน ประกอบด้วย - จัดการหมวดหมู่หนังสือ - จัดการหนังสือที่จำหน่ายในร้าน -แสดงรายการสั่งซื้อหนังสือ ส่วนที่ 2 หน้าแรกของร้านขายหนังสือ เป็นส่วนแสดงรายการหนังสือที่จำหน่ายในร้าน และเมื่อคลิกเลือกสั่งซื้อ จะบันทึกการสั่งซื้อหนังสือ

46 แผนผังโครงสร้างเว็บไซต์ระบบร้านขายหนังสือ
หน้าแรก ส่วนจัดการข้อมูลหนังสือ การจัดการหมวดหมู่หนังสือ จัดการหนังสือ แสดงรายการสั่งซื้อหนังสือ สั่งซื้อหนังสือ

47 ตัวอย่างการพัฒนาเว็บไซต์เพื่อเชื่อมต่อฐานข้อมูล MySQL
ขั้นตอนที่ 3 การออกแบบเว็บไซต์ ออกแบบฐานข้อมูลและหน้าเว็บเพจ ดังนี้ (1)ออกแบบฐานข้อมูลเพื่อเชื่อมต่อระบบงานร้านขายหนังสือชื่อ Books ประกอบด้วย ตารางจัดเก็บข้อมูล 4 ตารางดังนี้ ตารางที่ 1 หมวดหมู่หนังสือ category ตารางที่ 2 หนังสือ book ตารางที่ 3 การสั่งซื้อหนังสือ order ตารางที่ 4 รายละเอียดการสั่งซื้อหนังสือ order_detail

48 รายละเอียดตารางหมวดหมู่หนังสือ category
ชื่อฟิล์ด์ ชนิด หมายเหตุ category_ID int(1) รหัสหมวดหมู่หนังสือ กำหนดให้เป็น primary key category_NAME varchar(50) ชื่อหมวดหมู่หนังสือ

49 รายละเอียดตารางหนังสือ book
ชื่อฟิล์ด์ ชนิด หมายเหตุ book_ID int(3) รหัสหนังสือ กำหนดเป็น primary key book_NAME varchar(50) ชื่อหนังสือ book_price Double ราคาหนังสือ category_ID int(1) รหัสหมวดหมู่หนังสือ book_img รูปภาพ

50 รายละเอียดตารางการสั่งซื้อหนังสือ order
ชื่อฟิล์ด์ ชนิด หมายเหตุ order_ID varchar(13) เลขที่สั่งซื้อ กำหนดเป็น primary key customer_NAME varchar(50) ชื่อลูกค้า customer_TEL varchar(15) เบอร์โทรศัพท์ลูกค้า customer_ ที่อยู่อีเมล์ลูกค้า customer_ADD varchar(120) ที่อยู่สำหรับจัดส่งหนังสือ post_type varchar(1) วิธีการจัดส่ง 0=ธรรมดา, 1=ลงทะเบียน, 2=ems order_total Double มูลค่ารวมทั้งสิ้น

51 รายละเอียดตารางรายละเอียดการสั่งซื้อหนังสือ order_detail
ชื่อฟิล์ด์ ชนิด หมายเหตุ order_ID varchar(13) เลขที่สั่งซื้อหนังสือ กำหนดเป็น primary key book_ID int(3) รหัสหนังสือ กำหนดให้เป็น primary key book_price Double ราคาสั่งซื้อหนังสือ quantity Int(3) จำนวนเล่มหนังสือที่ซื้อ

52 ออกแบบหน้าเว็บเพจในเว็บไซต์ระบบงานร้านขายหนังสือ
ประกอบด้วย ส่วนที่ 1 ส่วนจัดการข้อมูลหนังสือที่ขายในร้าน กำหนดชื่อ admin.php แสดงการเชื่อมโยงไปยังหน้าเว็บ เพจที่เกี่ยวข้อง ดังนี้ (1) เว็บเพจจัดการหมวดหมู่หนังสือ กำหนดชื่อ category.php แสดงรายการข้อมูลหมวดหมู่หนังสือจากตาราง category ทั้งหมด พร้อมทั้ง เชื่อมโยงไปยังหน้าเว็บเพจที่เกี่ยวข้อง โดยเมื่อคลิกเลือก Edit ให้เชื่อมโยงไปยังหน้าแก้ไข ข้อมูลหมวดหมู่หนังสือ เว็บเพจชื่อ edit.php และคลิกเลือก Delete ให้เชื่อมโยงไปยังหน้า ลบข้อมูลหมวดหมู่หนังสือ delete.php จัดเก็บเว็บเพจไว้ใน Folder ชื่อ category (2) เว็บเพจเพิ่มข้อมูลหมวดหมู่หนังสือ กำหนดชื่อ input.php แสดงฟอร์มป้อนข้อมูลหมวดหมู่หนังสือ ประกอบด้วย category_NAME และเมื่อ คลิกบันทึกข้อมูล ให้บันทึกข้อมูลลงในตาราง category จัดเก็บใน Folder ชื่อ category

53 ออกแบบหน้าเว็บเพจในเว็บไซต์ระบบงานร้านขายหนังสือ
(3) เว็บเพจจัดการหนังสือที่จำหน่ายในร้าน กำหนดชื่อ book.php แสดงรายการหนังสือที่จำหน่ายในร้านจากตาราง book ทั้งหมด พร้อมทั้ง เชื่อมโยงไปยังหน้าเว็บเพจที่เกี่ยวข้อง โดยเมื่อคลิกเลือก Edit ให้เชื่อมโยงไปยังหน้าแก้ไข ข้อมูลหนังสือ เว็บเพจชื่อ edit.php และคลิกเลือก Delete ให้เชื่อมโยงไปยังหน้าลบข้อมูล หนังสือ delete.php จัดเก็บเว็บเพจไว้ใน Folder ชื่อ book (4) เว็บเพจเพิ่มข้อมูลหนังสือ กำหนดชื่อ input.php แสดงฟอร์มป้อนข้อมูลหนังสือ ประกอบด้วย book_NAME, book_price, category, book_img และเมื่อคลิกบันทึกข้อมูล ให้บันทึกข้อมูลลงในตาราง book จัดเก็บใน Folder ชื่อ book

54 ออกแบบหน้าเว็บเพจในเว็บไซต์ระบบงานร้านขายหนังสือ
(5) เว็บเพจแสดงรายการสั่งซื้อหนังสือทั้งหมด กำหนดชื่อ showorderall.php แสดงรายการสั่งซื้อหนังสือแยกตามรายการสั่งซื้อจากตาราง order และเมื่อคลิก รายการเลขที่สั่งซื้อหนังสือ order_ID จะเชื่อมโยงไปยังหน้ารายละเอียดการสั่งซื้อ (6) เว็บเพจข้อมูลรายละเอียดการสั่งซื้อหนังสือ กำหนดชื่อ showorderdetail.php แสดงรายการสั่งซื้อหนังสือทั้งหมด เมื่อคลิกเลือกเลขที่สั่งซื้อหนังสือ order_ID จะแสดงรายละเอียดการสั่งซื้อ ซึ่งประกอบด้วย ชื่อลูกค้า, เบอร์โทร, , ที่อยู่สำหรับ ส่ง วิธีการจัดส่ง และรายการสั่งซื้อหนังสือตามเลขที่สั่งซื้อหนังสือ

55 แผนผังโครงสร้างส่วนการจัดการข้อมูลหนังสือในร้าน
admin.php category/ category.php edit.php delete.php input.php book/ book.php showorderall.php showorderdetail.php

56 ออกแบบหน้าเว็บเพจในเว็บไซต์ระบบงานร้านขายหนังสือ
ส่วนที่ 2 หน้าแรกของร้านขายหนังสือ กำหนดชื่อ index.php แสดงรายการหนังสือที่จำหน่ายในร้านจากตาราง book และเมื่อคลิก เลือกปุ่มสั่งซื้อ ส่งข้อมูลรายการข้อมูลหนังสือที่ต้องการสั่งซื้อไปยังเว็บเพจ saveorder.php เพื่อบันทึกการสั่งซื้อหนังสือลงในตาราง order และ order_detail แผนผังโครงสร้างหน้าแรกของร้านขายหนังสือ index.php saveorder.php

57 ขั้นตอนที่ 4 การพัฒนาเว็บไซต์
นำข้อมูลที่ได้ออกแบบไว้มาพัฒนาเป็นว็บเพจ โดยมีรายละเอียดดังนี้ (1) สร้างฐานข้อมูลชื่อ books และสร้างตารางดังที่ได้ออกแบบไว้ (2) สร้าง Folder Website ชื่อสร้างไฟล์ชื่อ books จากนั้น สร้างไฟล์ connection.php เพื่อเก็บคำสั่งในการเชื่อมต่อ ฐานข้อมูล และตัวแปรที่สามารถใช้ได้ทุกเว็บเพจ

58 connection.php <? $servername = "localhost"; $username = "root"; $password = "root"; $dbname = "books"; $conn = new mysqli($servername, $username, $password, $dbname); mysqli_set_charset($conn,"utf8"); $post=array("ธรรมดา","ลงทะเบียน","ems"); ?>

59 ขั้นตอนที่ 4 การพัฒนาเว็บไซต์ (ต่อ)
(3) สร้างเว็บเพจตามที่ออกแบบไว้ 2 ส่วน คือ ส่วนจัดการข้อมูลหนังสือที่ขายในร้าน ชื่อเว็บเพจ admin.php ประกอบด้วย เว็บเพจที่เกี่ยวข้องคือ - category/category.php - category/edit.php - category/delete.php - category/input.php - book/book.php - book/edit.php - book/delete.php - book/input.php - showorderall.php - showorderdetail.php หน้าแรกของร้านขายหนังสือ กำหนดชื่อเว็บเพจ index.php

60 admin.php

61 admin.php <html><head><meta http-equiv=Content-Type content="text/html; charset=UTF-8"> <title>จัดการข้อมูลหมวดหมู่หนังสือ </title></head><body> <table width="80%" border="5" align="center" cellpadding="3" cellspacing="3" bordercolor="#999999"> <tr><TD width="20%" height="35" align="center"> <a href="category/category.php" target="content">จัดการข้อมูลหมวดหมู่หนังสือ</a></td> <TD width="20%" align="center"> <a href="book/book.php" target="content">จัดการหนังสือที่จำหน่ายในร้าน</a></td> <TD width="20%" align="center" rowspan="2"> <a href="showorderall.php" target="content">แสดงรายการสั่งซื้อหนังสือ</a></td></tr> <tr><TD height="33" align="center"> <a href="category/input.php" target="content">เพิ่มข้อมูลหมวดหมู่หนังสือ</a></td> <TD align="center"> <a href="book/input.php" target="content">เพิ่มข้อมูลหนังสือที่จำหน่ายในร้าน</a></td></tr> <TR><TD colspan="3"> <iframe name="content" width="100%" height="800px" frameborder="0"></iframe></TD></TR> </table></body></html>

62 category/category.php

63 category/category.php <? include("../connection.php"); ?> <html><head><meta http-equiv=Content-Type content="text/html; charset=UTF-8"> <title>จัดการข้อมูลหมวดหมู่หนังสือ </title> <style> table{ width:80%; margin:auto; border:2px solid #999999; } td{ padding:10px; }</style> </head><body> <? $sql="Select * from category"; $result=$conn->query($sql); ?> <h1 align="center">จัดการหมวดหมู่หนังสือ</h1> <div style="overflow-x:auto;"> <table> <tr bgcolor="lightblue"> <Td width="10%">category_ID</td> <td width="30%">category_NAME</td> <td width="10%">แก้ไข</td> <td width="10%">ลบ</td> </tr> while ($data=$result->fetch_array()){ echo "<tr><TD>".$data["category_ID"]."</td>";echo "<td>".$data["category_NAME"]."</td>"; echo "<Td><A Href='edit.php?id=".$data["category_ID"]."'>Edit</A></Td> "; echo "<Td><A Href=' delete.php?id=".$data["category_ID"]."'>Delete</A></Td> </tr>"; }$conn->close(); </table> </div></body></html>

64 category/input.php

65 category/input.php <? include("../connection.php"); ?> <!DOCTYPE html> <html lang="th"> <head> <title>เพิ่มข้อมูลหมวดหมู่หนังสือ</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link type="text/css" rel="stylesheet" href="../css.css" /> </head> <body> <h1 align="center">ฟอร์มป้อนข้อมูลหมวดหมู่หนังสือ</h1> <div class="container"> <form action=""> <div class="row"> <div class="col-25"> <label for="fname">category_ID</label> </div> <div class="col-75"> <input type="text" id="category_ID" name="category_ID" placeholder="ป้อน category_ID." value="<?=$_GET['category_ID'];?>"> </div> </div> <label for="fname">category_NAME</label> <input type="text" id="category_NAME" name="category_NAME" placeholder="ป้อน category_NAME." value="<?=$_GET['category_NAME'];?>"> </div> </div> <input type="submit" name="save" value="บันทึกข้อมูล"> <Input Type="Reset" name="clear" Value="เคลียร์ข้อมูล"> <Input Type="button" name="button" Value="กลับไปหน้า แสดงผล" onClick="window.location.replace('category.php');";> </div> </form></div> <? $category_ID=$_GET['category_ID']; $category_NAME=$_GET['category_NAME']; $save=$_GET['save']; if(isset($save)){ $query = "insert into category(category_ID,category_NAME) values ('$category_ID', '$category_NAME')"; $result=$conn->query($query); $num_rows =mysqli_affected_rows($conn); echo "<h4>เพิ่มข้อมูลลงฐานข้อมูลเรียบร้อยแล้ว จำนวน $num_rows เรคคอร์ด</h4>";} echo "<h4><a href='category.php'>กลับไปหน้าแสดงผล</a></h4>"; ?> </body></html>

66 category/edit.php

67 category/edit.php <? include("../connection.php"); ?> <!DOCTYPE html> <html lang="th"> <head> <title>แก้ไขข้อมูลหมวดหมู่หนังสือ</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link type="text/css" rel="stylesheet" href="../css.css" /> </head> <body> <? $id=$_GET['id']; $update=$_GET['update']; include("../connection.php"); if(isset($id)){ $sql="Select * from category where category_ID='".$id."'"; $result=$conn->query($sql); if($data=mysqli_fetch_array($result)){ $category_ID=$data[0]; $category_NAME=$data[1]; }}?> <h1 align="center">แก้ไขข้อมูลหมวดหมู่หนังสือ</h1> <div class="container"> <form action=""> <div class="row"> <div class="col-25"> <label for="fname">category_ID</label> </div> <div class="col-75"> <input type="text" id="category_ID" name="category_ID" placeholder="ป้อน category_ID." value="<?=$category_ID;?>"> </div> </div> <label for="fname">category_NAME</label> <input type="text" id="category_NAME" name="category_NAME" placeholder="ป้อน category_NAME." value="<?=$category_NAME;?>"> <input type="submit" name="update" value="แก้ไขข้อมูล"> <Input Type="Reset" name="clear" Value="เคลียร์ข้อมูล"> <Input Type="button" name="button" Value="กลับไปหน้าแสดงผล" onClick="window.location.replace('category.php');";> </div> </form></div> if(isset($update)){ $category_ID=$_GET['category_ID']; $category_NAME=$_GET['category_NAME']; $query = "update category set category_ID='$category_ID',category_NAME='$category_NAME' where category_ID='$category_ID'"; $result=$conn->query($query); echo "<h4 align='center'>แก้ไขข้อมูลลงฐานข้อมูลเรียบร้อยแล้ว</h4>"; } $conn->close(); ?> </body> </html>

68 category/delete.php <? include("../connection.php"); $id=$_GET['id']; //ค้นหาข้อมูลในตาราง Category $sql="Select * from category where category_ID='".$id."'"; $result=$conn->query($sql); if($data=$result->fetch_array()){ $category_ID=$data[0]; $category_NAME=$data[1]; } //ค้นหาหนังสือที่มี Category เท่ากับ $id หรือไม่ ถ้ามี จะไม่ให้ทำการลบข้อมูล $query="Select * from book where category_ID='$id'"; $result=$conn->query($query); $num_rows = mysqli_num_rows($result); //หาก num_rows=0 แสดงว่าไม่พบข้อมูลหนังสือตาม category_ID ลบข้อมูลได้ //หาก num_rows>0 แสดงว่าพบข้อมูลหนังสือตาม category_ID ไม่สามารถลบข้อมูลได้ if($num_rows==0){ $query = "delete from category where category_ID='$id'"; $conn->close(); echo "<h4 style='color:red;'>ลบข้อมูล category_ID =$id เรียบร้อยแล้ว</h4>"; }else { echo "<h4 style='color:red;'>มีหนังสือใน หมวดหมู่ $category_NAME ไม่สามารถลบได้ </h4>"; ?>

69 book/book.php

70 book/book.php <? include("connection.php"); ?> <html><head> <meta http-equiv=Content-Type content="text/html; charset=UTF-8"><title>จัดการข้อมูลหนังสือ </title> <style> table{ width:80%; margin:auto; border:2px solid #999999; } td{ padding:10px; </style> </head><body> <? $sql="Select book_ID,book_NAME,book_price,category_NAME,book_img from book,category where book.category_ID=category.category_ID"; $result=$conn->query($sql); ?> <h1 align="center">จัดการข้อมูลหนังสือ</h1> <table> <tr bgcolor="lightblue"><Td width="10%">book_ID</td><td width="30%">book_NAME</td> <Td width="10%">book_price</td><td width="20%">category_NAME</td> <td width="15%">book_img</td><td width="74">แก้ไข</td><td width="52">ลบ</td></tr> while ($data=$result->fetch_array()){ echo "<tr valign='top'><TD>".$data["book_ID"]."</td><td>".$data["book_NAME"]."</td>"; echo "<TD>".$data["book_price"]."</td><td>".$data["category_NAME"]."</td>"; echo "<TD><img src='../".$data["book_img"]."' width='100px'></td></td>"; echo "<Td><A Href='edit.php?id=".$data["book_ID"]."'>Edit</A></Td>"; echo "<Td><A Href=' delete.php?id=".$data["book_ID"]."'>Delete</A></Td></tr>"; $conn->close(); ?></table></body></html>

71 php_upload.php <?php $pictureup=$_FILES["book_img"]; if (empty($pictureup["size"])) { $des = "" ;} else { $imglocate="tmp/"; $fileupload1=$pictureup["tmp_name"]; if($fileupload1){ $g_img=explode(".",$pictureup["name"]); $lastname=$g_img[1]; $file_up=time().".".$lastname; if($lastname=="gif" or $lastname=="jpg" or $lastname=="jpeg" or $lastname=="swf" or $lastname=="png"){ $des=$imglocate.$file_up; copy($fileupload1,$des); }}} $strobj.="<img name='img1' id='img1' src='".$des."' style='height:300px;overflow:auto;' alt='Thumbnail'/>"; $str.= "<script language=\"javascript\">"; $str.="var pl=parent.document.getElementById('mypic1');"; $str.="pl.innerHTML=\"$strobj\";"; $str.="</script>"; echo $str; ?>

72 book/edit.php

73 book/edit.php(1 จาก 6) <? include("../connection.php"); ?> <html><head> <title>แก้ไขข้อมูลหนังสือ</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link type="text/css" rel="stylesheet" href="../css.css" /> <script type="text/javascript"> function show_pic(){ document.myformid.action='php_upload.php'; document.myformid.target='mypre'; document.myformid.submit(); } function submitdata(){ document.getElementById("myformid").action=""; document.getElementById("myformid").target="_self"; document.getElementById("myformid").submit(); </Script> </head> <body>

74 book/edit.php(ต่อ)(2 จาก 6)
<? $id=$_GET['id']; $update=$_POST['update']; if(isset($update)){ $book_ID=$_POST['book_ID']; $book_NAME=$_POST['book_NAME']; $book_price=$_POST['book_price']; $category_ID=$_POST['category_ID']; $book_img1=$_POST['book_img1']; $book_img=$_FILES["book_img"]; $save=$_POST['save']; $dir ="../image/"; $filedelete="../tmp/*"; $filetemp=""; if(!empty($book_img["size"])){ $pic = $dir.$book_img["name"]; //copy file to Folder image $savefile="image/".$book_img["name"]; $filetemp = $book_img["tmp_name"]; copy($filetemp,$pic); $files = glob($filedelete); // get all file names foreach($files as $file){ // iterate files if(is_file($file)) unlink($file); // delete file } }else {$savefile=$book_img1;} $query = "update book set book_NAME='$book_NAME',book_price='$book_ price',category_ID='$category_ID',book_img='$sa vefile' where book_ID='$book_ID'"; echo $query; $result=$conn->query($query); echo "<h4 align='center'>แก้ไขข้อมูลลง ฐานข้อมูลเรียบร้อยแล้ว</h4>";

75 book/edit.php(ต่อ)(3 จาก 6)
else{ if(isset($id)){ $query="Select * from book where book_ID='".$id."'"; $result=$conn->query($query); if($data=$result->fetch_array()){ $book_ID=$data[0]; $book_NAME=$data[1]; $book_price=$data[2]; $category_ID=$data[3]; $book_img=$data[4]; } ?>

76 book/edit.php(ต่อ)(4 จาก 6)
<h1 align="center">แก้ไขข้อมูลหนังสือ</h1> <div class="container"> <form action="" method="post" enctype="multipart/form-data" name="myformid" target="_self" id="myformid"> <div class="row"> <div class="col-25"> <label for="fname">book_ID</label> </div> <div class="col-75"> <input type="text" id="book_ID" name="book_ID" placeholder="ป้อน book_ID." value="<?=$book_ID;?>"> <label for="fname">book_NAME</label> <input type="text" id="book_NAME" name="book_NAME" placeholder="ป้อน book_NAME." value="<?=$book_NAME;?>"> <label for="fname">book_price</label> <input type="number" id="book_price" name="book_price" placeholder="ป้อน book_price." value="<?=$book_price;?>">

77 book/edit.php(ต่อ)(5 จาก 6)
<div class="row"> <div class="col-25"> <label for="fname">category</label> </div> <div class="col-75"> <select name="category_ID"> <? $query="Select category_ID,category_NAME from category"; $result=$conn->query($query); while ($data=$result->fetch_array()){ if($data['category_ID']==$category_ID){ echo "<option value='".$data['category_ID']."' selected>".$data['category_NAME']."</opti on>"; }else{ echo "<option value='".$data['category_ID']."'>".$data['cat egory_NAME']."</option>";} } ?> </select> </div> </div>

78 book/edit.php(ต่อ)(6 จาก 6)
<div class="row"> <Input Type="Submit" name="update" Value="แก้ไขข้อมูล" onClick="submitdata();"> <div class="col-25"> <Input Type="Reset" name="clear" Value="เคลียร์ข้อมูล"> <label for="fname">book_img</label> </div> <Input Type="button" name="button" Value="กลับไปหน้าแสดงผล" onClick="window.location.replace('category. php');";> <div class="col-75"> <Input Type="file" Name="book_img" id="book_img" Size="60" onChange="show_pic();"> </div> </form></div> <Input Type="text" Name="book_img1" Size="60" value="<?=$book_img;?>"> <? } <div id="mypic1" align="center" style="height:300px;"> $conn->close(); ?> <img src="../<?=$book_img;?>" style="height:300px;overflow:auto;" > </body> </html> <iframe id="mypre" name="mypre" width="0" height="0" frameborder="0"></iframe> </div> </div>

79 book/delete.php <? include("../connection.php"); $id=$_GET['id']; $query = "delete from book where book_ID='$id'"; $result=$conn->query ($query); $conn->close(); echo "<h4>ลบข้อมูล<Font color=red> book_ID =$id </Font> เรียบร้อยแล้ว</h4>"; ?>

80 book/input.php

81 book/input.php (1 จาก 4) <? include("../connection.php"); ?> <!DOCTYPE html> <html lang="th"> <head> <title>เพิ่มข้อมูลหนังสือ</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link type="text/css" rel="stylesheet" href="../css.css" /> </head> <script type="text/javascript"> function show_pic(){ document.myformid.action='php_upload.php'; document.myformid.target='mypre'; document.myformid.submit(); } function submitdata(){ document.getElementById("myformid").action=""; document.getElementById("myformid").target="_self"; document.getElementById("myformid").submit(); </Script> <body>

82 book/input.php (ต่อ)(2 จาก 4)
<? $book_NAME=$_POST['book_NAME']; $book_price=$_POST['book_price']; $category_ID=$_POST['category_ID']; $book_img=$_FILES["book_img"]; $save=$_POST['save']; $dir ="../image/"; $filedelete="tmp/*"; $filetemp=""; if(isset($save)){ if(!empty($book_img["size"])){ $pic = $dir.$book_img["name"]; //copy file to Folder image $savefile="image/".$book_img["name"] ; $filetemp = $book_img["tmp_name"]; copy($filetemp,$pic); $files = glob($filedelete); // get all file names foreach($files as $file){ // iterate files if(is_file($file)) unlink($file); // delete file }} $query = "INSERT INTO book(book_ID,book_NAME,book_price, category_ID,book_img) VALUES(NULL , '$book_NAME', '$book_price', '$category_ID', '$savefile');"; $result=$conn->query ($query); $conn->close(); echo "<h4 align='center'>เพิ่มข้อมูลลง ฐานข้อมูลเรียบร้อยแล้ว</h4>"; }else{ ?>

83 book/input.php (ต่อ)(3 จาก 4)
<h1 align="center">ฟอร์มป้อนข้อมูลหนังสือ</h1> <div class="container"> <form action="" method="post" enctype="multipart/form-data" name="myformid" target="_self" id="myformid"> <div class="row"> <div class="col-25"> <label for="fname">book_ID</label> </div> <div class="col-75"> <input type="text" id="book_ID" name="book_ID" placeholder="ป้อน book_ID." value="<?=$_GET['book_ID'];?>"> <label for="fname">book_NAME</label> <input type="text" id="book_NAME" name="book_NAME" placeholder="ป้อน book_NAME." value="<?=$_GET['book_NAME'];?>"> <label for="fname">book_price</label> <input type="number" id="book_price" name="book_price" placeholder="ป้อน book_price." value="<?=$_GET['book_price'];?>">

84 book/input.php (ต่อ)(4 จาก 4)
<div class="row"> <div class="col-25"> <label for="fname">category</label> </div> <div class="col-75"> <select name="category_ID"> <? $query="Select category_ID,category_NAME from category"; $result=$conn->query($query); while ($data=$result->fetch_array()){ echo "<option value='".$data['category_ID']."'>".$data['category_NAM E']."</option>"; } ?> </select> <label for="fname">book_img</label> <Input Type="file" Name="book_img" id="book_img" Size="60" onChange="show_pic();"> <div id="mypic1" align="center" style="height:300px;"></div> <iframe id="mypre" name="mypre" width="0" height="0" frameborder="0"></iframe> <Input Type="Submit" name="save" Value="บันทึกข้อมูล" onClick="submitdata();"> <Input Type="Reset" name="clear" Value="เคลียร์ข้อมูล"> <Input Type="button" name="button" Value="กลับไปหน้าแสดงผล" onClick="window.location.replace('category.php');";> </form> $conn->close(); } ?> </body></html>

85 showorderall.php

86 showorderall.php (1 จาก 2)
<html><head><meta http- equiv=Content-Type content="text/html; charset=UTF-8"> <title>แสดงรายการสั่งซื้อหนังสือ </title></head><body> <img src="image/bg1.jpg" height="300px" width="100%"> <?php echo "<h2>แสดงรายการสั่งซื้อหนังสือทั้งหมด </H2>"; include("connection.php"); $query="select order_ID,customer_NAME,customer_T EL,customer_ ,customer_ADD,post _type,order_total from `order` "; $result=$conn->query($query); ?> <table border="2" width="99%" align="cener"> <tr> <Td>order_ID</td> <td width="15%">customer_NAME</td> <Td width="15%">customer_TEL</td> <td width="15%">customer_ </td> <td width="15%">customer_ADD</td> <td width="15%">post_type</td> <td width="15%">order_total</td> </tr>

87 showorderall.php(ต่อ) (2 จาก 2)
<? while ($data =$result->fetch_array()){ echo "<tr valign='top'>"; echo "<TD><a href='showorderdetail.php?id=".$data["order_ID"]."'>".$data["order_ID"]."</a></t d>"; echo "<td>".$data["customer_NAME"]."</td>"; echo "<td>".$data["customer_TEL"]."</td>"; echo "<td>".$data["customer_ "]."</td>"; echo "<td>".$data["customer_ADD"]."</td>"; echo "<td>".$post[$data["post_type"]]."</td>"; echo "<td>".$data["order_total"]."</td>"; echo "</tr>"; } $conn->close(); ?> </table></body></html>

88 showorderdetail.php

89 showorderdetail.php (1 จาก 3)
<html><head><meta http-equiv=Content-Type content="text/html; charset=UTF- 8"> <title>แสดงรายละเอียดการสั่งซื้อหนังสือ</title></head><body> <form name="orderbook"><? $id=$_GET['id']; include("connection.php"); $query="select order_ID,customer_NAME,customer_TEL,customer_ ,customer_ADD,post_typ e,order_total from `order` where order_ID='$id'"; $result=$conn->query($query); if($data =$result->fetch_array()){ $customer_NAME=$data['customer_NAME']; $customer_TEL=$data['customer_TEL']; $customer_ =$data['customer_ ']; $customer_ADD=$data['customer_ADD']; $post_type=$data['post_type']; $book_img=$data['book_img'];}?>

90 showorderdetail.php(ต่อ) (2 จาก 3)
<img src="image/bg1.jpg" height="300px" width="100%"> <h2 align="center">ข้อมูลรายละเอียดการสั่งซื้อ</center> <table width="80%" border="1" align="center" cellpadding="3" cellspacing="3" bordercolor="#999999" bgcolor="lightblue"> <TR valign="top"><TD rowspan="15" width="40%"><img src="image/bg2.png" width="90%"></td> <TD>ชื่อลูกค้า</TD><TD>:</TD><TD><input type="text" name="customer_NAME" size="25px" value="<?=$customer_NAME;?>"></TD></TR> <TR valign="top"><TD>เบอร์โทร</TD><TD>:</TD><TD><input type="text" name="customer_TEL" size="25px" value="<?=$customer_TEL;?>"></TD></TR> <TR valign="top"><TD> </TD><TD>:</TD><TD><input type=" " name="customer_ " size="25px" value="<?=$customer_ ;?>"></TD></TR> <TR valign="top"><TD nowrap>ที่อยู่สำหรับส่ง</TD><TD valign="top">:</TD> <TD><textarea name="customer_ADD" cols="50px" rows="5"><?=$customer_ADD;?></textarea></TD></TR> <TR valign="top"><TD nowrap>วิธีการจัดส่ง</TD><TD valign="top">:</TD> <TD><?=$post[$post_type];?></TD></TR><TR><TD colspan="3"> <table width="80%" align="center" border="0"><? $querydetail="select order_detail.book_ID,book_NAME,order_detail.book_price,quantity,book_img from order_detail,book where order_detail.book_ID=book.book_ID and order_ID='$id'"; $resultdetail=$conn->query($querydetail);

91 showorderdetail.php(ต่อ) (3 จาก 3)
while($datadetail =$resultdetail->fetch_array()){ $book_ID=$datadetail[0]; $book_NAME=$datadetail[1]; $book_price=$datadetail[2]; $quantity=$datadetail[3]; $book_img=$datadetail[4]; echo "<TR valign='top'><Td><img height='180px' src='".$book_img."'></td>"; echo "<TD align='center'><B>เล่มละ ".$book_price." บาท</B></TD>";echo "<TD>จำนวน</TD>"; echo "<td><input type='number' name='bookid[".$book_ID."]' min='1' max='20' value='$quantity'></td>"; echo "<TD>เล่ม</TD>";echo "</TR>";}?></tr></Table></TD></TR></table></form> <?$conn->close();?> </body></html>

92 ส่วนที่ 2 ส่วนหน้าแรกของร้านขายหนังสือ
index.php ประกอบด้วยเว็บเพจที่เกี่ยวข้องคือ saveorder.php

93 index.php

94 index.php (1 จาก 2) <html><head><meta http-equiv=Content-Type content="text/html; charset=UTF-8"> <title>Book Store</title></head><body> <img src="image/bg1.jpg" width="100%" height="310px" align="center"> <h2 align="center">เลือกสรรสิ่งที่ชื่นชอบ</h2> <form name="order" action="saveorder.php"> <? include("connection.php"); $query="Select book_ID,book_NAME,book_price,category_NAME,book_img from book,category where book.category_ID=category.category_ID"; $result=$conn->query($query); ?>

95 index.php(ต่อ) (2 จาก 2) <table border="0" width="70%" align="center" cellpadding="2px"> <? $i=0; echo "<TR>"; while ($data =$result->fetch_array()){ if($i%3==0){echo "</tR><TR>";} echo "<Td align='center'><h3 align='center'>$data[1]</h3><img src='$data[4]' height='220px'>"; echo "<p><input type='checkbox' name='chk[]' value='$data[0]'>"; echo "ราคา $data[2] บาท"; echo "</p></td>"; $i++; }?> <TR><TD colspan="3" align="center"> <input type="submit" value="สั่งซื้อ"> <input type="reset" value="เคลียร์"></TD></tr></Table> </form></body></html>

96 saveorder.php

97 saveorder.php(1 จาก 4) <html><head><meta http-equiv=Content-Type content="text/html; charset=UTF-8"> <title>บันทึกรายการสั่งซื้อ</title></head><body> <form name="orderbook"><? $chk=$_GET['chk']; include("connection.php"); ?> <img src="image/bg1.jpg" height="300px" width="100%"> <h2 align="center">ข้อมูลการสั่งซื้อ</center> <table width="80%" border="1" align="center" cellpadding="3" cellspacing="3" bordercolor="#999999" bgcolor="lightblue"> <TR valign="top"><TD rowspan="15" width="40%"><img src="image/bg2.png" width="90%"></td> <TD>ชื่อลูกค้า</TD><TD>:</TD><TD><input type="text" name="customer_NAME" size="25px" placeholder="ชื่อลูกค้า" required></TD></TR> <TR valign="top"><TD>เบอร์โทร</TD><TD>:</TD> <TD><input type="text" name="customer_TEL" size="25px" placeholder="เบอร์โทร" required></TD></TR> <TR valign="top"><TD> </TD><TD>:</TD> <TD><input type=" " name="customer_ " size="25px" placeholder="อีเมล์" required></TD></TR> <TR valign="top"><TD nowrap>ที่อยู่สำหรับส่ง</TD><TD valign="top">:</TD> <TD><textarea name="customer_ADD" cols="50px" rows="5" placeholder="ที่อยู่สำหรับจัดส่ง" required></textarea></TD></TR><TR valign="top"><TD nowrap>วิธีการจัดส่ง</TD><TD valign="top">:</TD><TD>

98 saveorder.php(ต่อ) (2 จาก 4)
<? for($i=0;$i<count($post);$i++){echo "<input type='radio' name='post_type' value='$i'>$post[$i]<BR>";}?> </TD></TR><TR><TD colspan="3"><table width="80%" align="center" border="0"> <?$i=0;for($i=0;$i<count($chk);$i++){ $query="Select book_ID,book_NAME,book_price,book_img from book where book_ID=".$chk[$i]; $result=$conn->query ($query); if($data =$result->fetch_array()){ $book_NAME=$data[1]; $book_price=$data[2]; $book_img=$data[3]; echo "<TR valign='top'><Td><img height='180px' src='".$book_img."'></td>"; echo "<TD align='center'><B>เล่มละ ".$book_price." บาท</B></TD>"; echo "<TD>จำนวน<input type='hidden' name='bookprice[".$chk[$i]."]' value='$book_price'></TD>"; echo "<td><input type='number' name='bookid[".$chk[$i]."]' min='1' max='20' value='1'></td>"; echo "<TD>เล่ม</TD>";echo "</TR>"; }}?></td></tr> <tr><TD colspan="5" align="center"><Input Type="Submit" name="save" Value="บันทึกการ สั่งซื้อ"></TD></tr></Table></TD></TR></table></form>

99 saveorder.php(ต่อ) (3 จาก 4)
<? $save=$_GET['save']; if(isset($save)){ $order_ID=time(); $customer_NAME=$_GET['customer_NAME']; $customer_TEL=$_GET['customer_TEL']; $customer_ =$_GET['customer_ ']; $customer_ADD=$_GET['customer_ADD']; $post_type=$_GET['post_type']; $book_img=$_GET['book_img']; $bookprice=$_GET['bookprice']; $bookid=$_GET['bookid']; $total=0;

100 saveorder.php(ต่อ) (4 จาก 4)
while(list($k,$v)=each($bookid)){ $query = "INSERT INTO order_detail(order_ID,book_ID,book_price,quantity) VALUES('$order_ID','$k','$bookprice[$k]','$v');"; $result=$conn->query ($query); $total+=$bookprice[$k]*$v;} $queryorder = " INSERT INTO `books`.`order`(order_ID,customer_NAME,customer_TEL,customer_ ,custome r_ADD,post_type,order_total) VALUES ('$order_ID', '$customer_NAME', '$customer_TEL', '$customer_ ', '$customer_ADD', '$post_type', '$total');"; $resultorder=$conn->query($queryorder); echo "<h4 align='center'>เพิ่มข้อมูลลงฐานข้อมูลเรียบร้อยแล้ว</h4>"; echo "<a href='index.php'><h4 align='center'>คลิกกลับไปหน้ารายการหนังสือ</h4></a>"; }$conn->close(); ?></body></html>


ดาวน์โหลด ppt การเชื่อมต่อฐานข้อมูล

งานนำเสนอที่คล้ายกัน


Ads by Google