PHP+MySqli EP.1 การเพิ่มข้อมูลเข้า Database
PHP+MySqli EP.1 การเพิ่มข้อมูลเข้า Database การเพิ่มข้อมูลเข้า Database โดยใช้ภาษา PHP+MySqli ตัวอย่างตาราง (อย่าลืมสร้างDatabaseใหม่ก่อน)
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
-- phpMyAdmin SQL Dump
-- version 4.7.0
-- https://www.phpmyadmin.net/
--
-- Host: localhost
-- Generation Time: Oct 08, 2018 at 06:56 PM
-- Server version: 10.1.25-MariaDB
-- PHP Version: 5.6.31
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: `sqli_1`
--
-- --------------------------------------------------------
--
-- Table structure for table `teble1`
--
CREATE TABLE `teble1` (
`id` int(11) NOT NULL,
`name` varchar(20) DEFAULT NULL,
`city` text,
`img` varchar(250) NOT NULL,
`date` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Indexes for dumped tables
--
--
-- Indexes for table `teble1`
--
ALTER TABLE `teble1`
ADD PRIMARY KEY (`id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `teble1`
--
ALTER TABLE `teble1`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=38;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 */;
|
ไฟล์ connect
|
1
2
3
4
5
6
7
|
<?php
$con= mysqli_connect("localhost","root","","sqli_1") or die("Error: " . mysqli_error($con));
mysqli_query($con, "SET NAMES 'utf8' ");
?>
|
สร้างไฟล์ชื่อ insert.php แล้ว ก๊อปโค๊ดไปวางได้เลย
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<center><br /><br /><br />
<p >โปรแกรมเพิ่มข้อมูลแบบ Sqli</p>
<form action="save_insert.php" method="post" enctype="multipart/form-data" name="form1" id="form1"><table width="20%" border="1" cellpadding="1" cellspacing="1">
<tr>
<td colspan="2" align="center">Insert</td>
</tr>
<tr>
<td>name</td>
<td><label for="name"></label>
<input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td>city</td>
<td><input type="text" name="city" id="city" /></td>
</tr>
<tr>
<td>img</td>
<td><label for="img"></label>
<input type="file" name="img" id="img" /></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="button" id="button" value="Save" /></td>
</tr>
</table>
</form>
<br />
</center>
</body>
</html>
|
สร้างไฟล์ชื่อ save_insert.php แล้ว ก๊อปโค๊ดไปวางได้เลย
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
<mate charset ="utf-8" />
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
<?php include ('Connections/consqli.php');
//ส่วนของการเพิ่มข้อมูลรูปภาพ
//Set ว/ด/ป เวลา ให้เป็นของประเทศไทย
date_default_timezone_set('Asia/Bangkok');
//สร้างตัวแปรวันที่เพื่อเอาไปตั้งชื่อไฟล์ที่อัพโหลด
$date1 = date("Ymd_his");
//สร้างตัวแปรสุ่มตัวเลขเพื่อเอาไปตั้งชื่อไฟล์ที่อัพโหลดไม่ให้ชื่อไฟล์ซ้ำกัน
$numrand = (mt_rand());
//รับชื่อไฟล์จากฟอร์ม
$img = (isset($_REQUEST['img']) ? $_REQUEST['img'] : '');
$upload=$_FILES['img'];
if($upload <> '') {
//โฟลเดอร์ที่เก็บไฟล์
$path="img/";
//ตัวขื่อกับนามสกุลภาพออกจากกัน
$type = strrchr($_FILES['img']['name'],".");
//ตั้งชื่อไฟล์ใหม่เป็น สุ่มตัวเลข+วันที่
$newname =$numrand.$date1.$type;
$path_copy=$path.$newname;
$path_file_img="img/".$newname;
//คัดลอกไฟล์ไปยังโฟลเดอร์
move_uploaded_file($_FILES['img']['tmp_name'],$path_copy);
}
//สร้างตัวแปร
$name = $_POST['name'];
$city = $_POST['city'];
//เพิ่มข้อมูล
$sql = " INSERT INTO teble1
(name, city, img)
VALUES
('$name', '$city', '$newname')";
$result = mysqli_query($con, $sql) or die ("Error in query: $sql " . mysqli_error());
//ปิดการเชื่อมต่อ database
mysqli_close($con);
//ถ้าสำเร็จให้ขึ้นอะไร
if ($result){
echo "<script type='text/javascript'>";
echo"alert('Register Success');";
echo"window.location = 'insert.php';";
echo "</script>";
}
else {
//กำหนดเงื่อนไขว่าถ้าไม่สำเร็จให้ขึ้นข้อความและกลับไปหน้าเพิ่ม
echo "<script type='text/javascript'>";
echo "alert('error!');";
echo"window.location = 'insert.php'; ";
echo"</script>";
}
?>
|
