Last active
November 18, 2019 08:44
-
-
Save showroom101/27bb8d960c4c530b5d4903f083e58b2c to your computer and use it in GitHub Desktop.
ตัวอย่างการทำ CRUD ของ php สอนน้องๆ ปี2 รหัส 60
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ขั้นตอนการติดตั้งและทดสอบ code | |
- หลังจากลง xampp เสร็จแล้วสร้าง table ชื่อว่า db_portfolio | |
- ทำการ import file db_portfolio ลงฐานข้อมูลที่สร้างมา | |
- copy folder เก็บไฟล์ทั้งหมดนี้ไปวางที่ htdoc ใน xampp ของ drive c: ของเครื่อง | |
- เปิด browser และเข้าที่ url localhost (หากมีการเปลี่ยน port ให้ใส่ด้วย เช่น localhost:8888) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// เรียกใช้ไฟล์ เชื่อมต่อ database | |
include("connect_db.php"); | |
// $insert_date = date("Y-m-d H:i:s"); | |
$contact_name = $_POST['Name']; | |
$contact_email = $_POST['Email']; | |
$contact_phone = $_POST['Phone']; | |
$contact_message = $_POST['Message']; | |
$sql = "INSERT INTO tb_contact VALUES (NULL, '$contact_name', '$contact_email', '$contact_phone', '$contact_message', NOW(), NOW())"; | |
$query = $conn->query($sql); //mysql_query($sql); | |
if($query){ | |
echo "successfully <br>"; | |
echo "<a href='index.php'>ย้อนกลับ</a>"; | |
} else | |
{ | |
echo "Error: " . $sql . "<br>" . $conn->error; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$servername = "localhost"; | |
$username = "root"; | |
$password = ""; | |
$dbname = "db_portfolio"; | |
// Create connection | |
$conn = new mysqli($servername, $username, $password, $dbname); | |
// Check connection | |
if ($conn->connect_error) { | |
die("Connection failed: " . $conn->connect_error); | |
} | |
$conn->query("set names utf8"); | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- phpMyAdmin SQL Dump | |
-- version 4.8.0 | |
-- https://www.phpmyadmin.net/ | |
-- | |
-- Host: db | |
-- Generation Time: Apr 30, 2018 at 05:45 PM | |
-- Server version: 5.7.22 | |
-- PHP Version: 7.2.4 | |
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; | |
SET AUTOCOMMIT = 0; | |
START TRANSACTION; | |
SET time_zone = "+00:00"; | |
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; | |
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; | |
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; | |
/*!40101 SET NAMES utf8mb4 */; | |
-- | |
-- Database: `db_portfolio` | |
-- | |
-- -------------------------------------------------------- | |
-- | |
-- Table structure for table `tb_contact` | |
-- | |
CREATE TABLE `tb_contact` ( | |
`contact_id` int(11) NOT NULL, | |
`contact_name` varchar(32) NOT NULL, | |
`contact_email` varchar(64) NOT NULL, | |
`contact_phone` varchar(10) NOT NULL, | |
`contact_message` text NOT NULL, | |
`created_at` datetime NOT NULL, | |
`updated_at` datetime NOT NULL | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
-- | |
-- Indexes for dumped tables | |
-- | |
-- | |
-- Indexes for table `tb_contact` | |
-- | |
ALTER TABLE `tb_contact` | |
ADD PRIMARY KEY (`contact_id`); | |
-- | |
-- AUTO_INCREMENT for dumped tables | |
-- | |
-- | |
-- AUTO_INCREMENT for table `tb_contact` | |
-- | |
ALTER TABLE `tb_contact` | |
MODIFY `contact_id` int(11) NOT NULL AUTO_INCREMENT; | |
COMMIT; | |
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; | |
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; | |
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// เรียกใช้ไฟล์ เชื่อมต่อ database | |
include("connect_db.php"); | |
$contact_id = $_GET['id']; | |
$sql = "DELETE FROM tb_contact WHERE contact_id = '".$contact_id."'"; | |
$query = $conn->query($sql); | |
if($query){ | |
echo "successfully <br>"; | |
echo "<a href='index.php'>ย้อนกลับ</a>"; | |
} else | |
{ | |
echo "Error: " . $sql . "<br>" . $conn->error; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// เรียกใช้ไฟล์ เชื่อมต่อ database | |
include("connect_db.php"); | |
$contact_id = $_POST['contact_id']; | |
$contact_name = $_POST['Name']; | |
$contact_email = $_POST['Email']; | |
$contact_phone = $_POST['Phone']; | |
$contact_message = $_POST['Message']; | |
$sql = "UPDATE tb_contact SET contact_name='$contact_name', contact_email='$contact_email', contact_phone='$contact_phone', contact_message='$contact_message', updated_at=NOW() WHERE contact_id = '".$contact_id."'"; | |
$query = $conn->query($sql); | |
if($query){ | |
echo "successfully <br>"; | |
echo "<a href='index.php'>ย้อนกลับ</a>"; | |
} else | |
{ | |
echo "Error: " . $sql . "<br>" . $conn->error; | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php // เรียกใช้ไฟล์ เชื่อมต่อ database | |
include("connect_db.php"); | |
?> | |
<!doctype html> | |
<html lang="en"> | |
<head> | |
<!-- Required meta tags --> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<!-- Bootstrap CSS --> | |
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" | |
crossorigin="anonymous"> | |
<title>basic php CRUD</title> | |
</head> | |
<body> | |
<section> | |
<div class="container"> | |
<div class="row"> | |
<table class="table"> | |
<thead> | |
<tr> | |
<th scope="col">Name</th> | |
<th scope="col">Email</th> | |
<th scope="col">Phone</th> | |
<th scope="col">Message</th> | |
<th></th> | |
<th></th> | |
</tr> | |
</thead> | |
<tbody> | |
<?php | |
// แสดงข้อมูลในตาราง | |
$sql = "SELECT * FROM tb_contact"; | |
$result = $conn->query($sql); | |
if ($result->num_rows > 0) { | |
while($row = $result->fetch_assoc()) { | |
echo "<tr>"; | |
echo "<th>".$row['contact_name']."</th>"; | |
echo "<th>".$row['contact_email']."</th>"; | |
echo "<th>".$row['contact_phone']."</th>"; | |
echo "<th>".$row['contact_message']."</th>"; | |
echo "<th><a href='".$_SERVER['PHP_SELF']."?id=".$row['contact_id']."'>แก้ไขข้อมูล</th>"; | |
echo "<th><a href='del_contact.php?id=".$row['contact_id']."'>ลบข้อมูล</th>"; | |
echo "</tr>"; | |
} | |
} else { | |
echo "<tr>"; | |
echo "<th colspan='4'>No data available</th>"; | |
echo "</tr>"; | |
} | |
?> | |
</tbody> | |
</table> | |
</div> | |
<?php | |
// isset ใช้สำหรับเช็คค่าว่างหรือไม่ | |
if(isset($_GET['id'])){ | |
$sql = "SELECT * FROM tb_contact WHERE contact_id = '".$_GET['id']."'"; | |
$result = $conn->query($sql); | |
$row = $result->fetch_assoc(); | |
?> | |
<!-- สำหรับแก้ไขข้อมูล --> | |
<form action="edit_contact.php" method="POST" > | |
<input type="hidden" name="contact_id" value="<?php echo $row['contact_id']; ?>" > | |
<div class="form-group"> | |
<label >Name</label> | |
<input type="text" name="Name" value="<?php echo $row['contact_name']; ?>" class="form-control" placeholder="Name"> | |
</div> | |
<div class="form-group"> | |
<label >Email</label> | |
<input type="email" name="Email" value="<?php echo $row['contact_email']; ?>" class="form-control" placeholder="Email"> | |
</div> | |
<div class="form-group"> | |
<label >Phone</label> | |
<input type="text" name="Phone" value="<?php echo $row['contact_phone']; ?>" class="form-control" placeholder="Phone"> | |
</div> | |
<div class="form-group"> | |
<label >Message</label> | |
<textarea class="form-control" name="Message" rows="3"> | |
<?php echo $row['contact_message']; ?> | |
</textarea> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</form> | |
<?php | |
} else { | |
?> | |
<!-- สำหรับเพิ่มข้อมูล --> | |
<form action="add_contact.php" method="POST" > | |
<div class="form-group"> | |
<label >Name</label> | |
<input type="text" name="Name" value="" class="form-control" placeholder="Name"> | |
</div> | |
<div class="form-group"> | |
<label >Email</label> | |
<input type="email" name="Email" value="" class="form-control" placeholder="Email"> | |
</div> | |
<div class="form-group"> | |
<label >Phone</label> | |
<input type="text" name="Phone" value="" class="form-control" placeholder="Phone"> | |
</div> | |
<div class="form-group"> | |
<label >Message</label> | |
<textarea class="form-control" name="Message" value="" rows="3"></textarea> | |
</div> | |
<button type="submit" class="btn btn-primary">Submit</button> | |
</form> | |
<?php | |
} | |
?> | |
</div> | |
</section> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment