커뮤니티

고용노동부, 산업인력공단과 함께하는 강원도 유일한 기업중심 IT전문교육기관 ICT융합캠퍼스만의 특별한교육입니다.
공인 IT숙련기술인의 다양한 접근방법으로 전문가다운 실무교육을 받을 수 있습니다.

Category

교육강좌

WEB 웹 애플리케이션 만들기 - 라이브러리 1 (직접 만들기)

페이지 정보

작성자 관리자 댓글 0건 조회 3,147회 작성일 20-06-08 11:51

본문

라이브러리 1 (직접 만들기)

라이브러리 1 : 개론

라이브러리 2 : 직접 만들기

 

index.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
<?php
require("config/config.php");
require("lib/db.php");
$conn = db_init($config["host"], $config["duser"], $config["dpw"], $config["dname"]);
$result = mysqli_query($conn, "SELECT * FROM topic");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="http://localhost/style.css">
</head>
<body id="target">
<header>
<h1><a href="http://localhost/index.php">JavaScript</a></h1>
</header>
<nav>
<ol>
<?php
while( $row = mysqli_fetch_assoc($result)){
echo '<li><a href="http://localhost/index.php?id='.$row['id'].'">'.htmlspecialchars($row['title']).'</a></li>'."\n";
}
?>
</ ol>
</nav>
<div id="control">
<input type="button" value="white" onclick="document.getElementById('target').className='white'"/>
<input type="button" value="black" onclick="document.getElementById('target').className='black'" />
<a href="http://localhost/write.php">쓰기</a>
</div>
<article>
<?php
if(empty($_GET['id']) === false ) {
$sql = "SELECT topic.id,title,name,description FROM topic LEFT JOIN user ON topic.author = user.id WHERE topic.id=".$_GET['id'];
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
echo '<h2>'.htmlspecialchars($row['title']).'</h2>';
echo '<p>'.htmlspecialchars($row['name']).'</p>';
echo strip_tags($row['description'], '<a><h1><h2><h3><h4><h5><ul><ol><li>');
}
?>
</article>
</body>
</html>

lib/db.php

1
2
3
4
5
6
7
<?php
function db_init($host, $duser, $dpw, $dname){
$conn = mysqli_connect($host, $duser, $dpw);
mysqli_select_db($conn, $dname);
return $conn;
}
?>

config/config.php

1
2
3
4
5
6
7
8
<?php
$config = array(
"host"=>"localhost",
"duser"=>"root",
"dpw"=>"111111",
"dname"=>"opentutorials"
);
?>

write.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
<?php
require("config/config.php");
require("lib/db.php");
$conn = db_init($config["host"], $config["duser"], $config["dpw"], $config["dname"]);
$result = mysqli_query($conn, "SELECT * FROM topic");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="http://localhost/style.css">
</head>
<body id="target">
<header>
<h1><a href="http://localhost/index.php">JavaScript</a></h1>
</header>
<nav>
<ol>
<?php
while( $row = mysqli_fetch_assoc($result)){
echo '<li><a href="http://localhost/index.php?id='.$row['id'].'">'.$row['title'].'</a></li>'."\n";
}
?>
</ ol>
</nav>
<div id="control">
<input type="button" value="white" onclick="document.getElementById('target').className='white'"/>
<input type="button" value="black" onclick="document.getElementById('target').className='black'" />
<a href="http://localhost/write.php">쓰기</a>
</div>
<article>
<form action="process.php" method="post">
<p>
제목 : <input type="text" name="title">
</p>
<p>
작성자 : <input type="text" name="author">
</p>
<p>
본문 : <textarea name="description"></textarea>
</p>
<input type="submit" name="name">
</form>
</article>
</body>
</html>

process.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?php
require("config/config.php");
require("lib/db.php");
$conn = db_init($config["host"], $config["duser"], $config["dpw"], $config["dname"]);
$title = mysqli_real_escape_string($conn, $_POST['title']);
$author = mysqli_real_escape_string($conn, $_POST['author']);
$description = mysqli_real_escape_string($conn, $_POST['description']);
$sql = "SELECT * FROM user WHERE name='".$author."'";
$result = mysqli_query($conn, $sql);
if($result->num_rows == 0){
$sql = "INSERT INTO user (name, password) VALUES('".$author."', '111111')";
mysqli_query($conn, $sql);
$user_id = mysqli_insert_id($conn);
} else {
$row = mysqli_fetch_assoc($result);
$user_id = $row['id'];
}
$sql = "INSERT INTO topic (title,description,author,created) VALUES('".$title."', '".$description."', '".$user_id."', now())";
$result = mysqli_query($conn, $sql);
header('Location: http://localhost/index.php');
?>

소스코드

github

  • 트위터로 보내기
  • 페이스북으로 보내기
  • 구글플러스로 보내기

답변목록

등록된 답변이 없습니다.