2020. 4. 9. 01:02ㆍ생활코딩/생활코딩웹
개요
실습을 통해 index.php와 mysql database의 정보를 연동해보겠습니다.
실습
처음에는 저번에 만든 index.php를 가져와 데이터베이스를 연동합니다
<?php
$conn = mysqli_connect("localhost", "root", "jh1245");
mysqli_select_db($conn, "mytester");
$result = mysqli_query($conn, "SELECT * FROM topic2");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="http://localhost/phpjs/style.css">
</head>
<body id="target">
<header>
<img src="https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" alt="생활코딩">
<h1><a href="http://localhost/index.php">JavaScript</a></h1>
</header>
<nav>
</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>
</body>
</html>
위의 코딩을 보면 맨위의 PHP 코드가 보일 겁니다 저것을 mysql database에 연동하겠다는 것을 선언해주는 것으로
$conn안에 아이디와 비번을저장하여 주고 mysqli_select_db에 자신이 들어갈 저장된 conn과 들어갈 데이터베이스를 입력해줍니다. 다음 검색할 테이블을 $result에 넣어줍니다
이렇게 선언하기전 주의사항으로 처음 비밀번호가 숫자로만 이루어져 있다면 ""를 입력하지 않아도 되지만 문자가 포함이 되어있다면 비밀번호 안에 ""를 꼭 넣어주셔야 오류가 뜨지 않습니다.
다음으로 쓰기로 넘어가는 링크를 달아줍니다
위의 사진처럼 쓰기 버튼이 있으며 보이지는 않지만 데이터 베이스가 연결되어있습니다.
다음 쓰기를 누를 시 표현이 되는 것을 해볼 것입니다.
<?php
$conn = mysqli_connect("localhost", "root", "jh1245");
mysqli_select_db($conn, "mytester");
$result = mysqli_query($conn, "SELECT * FROM topic2");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="http://localhost/style.css">
</head>
<body id="target">
<header>
<img src="https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" alt="생활코딩">
<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로 넘어갑니다.
<?php
$conn = mysqli_connect("localhost", "root", "jh1245");
mysqli_select_db($conn, "mytester");
$sql = "INSERT INTO topic2 (title,description,author,created) VALUES('".$_POST['title']."', '".$_POST['description']."', '".$_POST['author']."', now())";
$result = mysqli_query($conn, $sql);
header('Location: http://localhost/index.php');
?>
process.php로 넘어오면 데이터베이스의 insert 즉 데이터베이스에 입력을 해주는 기능을 해주기 위한 sql문을 php로 작 상하게 됩니다.
여기서 중요한 점은 칼럼과 입력할 값의 순서는 같아야 합니다 그렇지 않으면 오류 가뜨니 주의하셔야 합니다.
그리고 다시 http://localhost/index.php 로넘어가줍니다
<?php
$conn = mysqli_connect("localhost", "root", "jh1245");
mysqli_select_db($conn, "mytester");
$result = mysqli_query($conn, "SELECT * FROM topic2");
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="http://localhost/phpjs/style.css">
</head>
<body id="target">
<header>
<img src="https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" alt="생활코딩">
<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>
<?php
if(empty($_GET['id']) === false ) {
$sql = 'SELECT * FROM topic2 WHERE id='.$_GET['id'];
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
echo '<h2>'.$row['title'].'</h2>';
echo $row['description'];
}
?>
</article>
</body>
</html>
process.php에서 데이터 베이스에 값을 넣은 후 그것을 표현해줍니다 처음에는 제목을 입력한 것은 메뉴의 표시가 되며
메뉴를 클릭 시 이렇게 입력한 값들이 나오는 것을 볼 수 있으며 이 값들은
데이터베이스에도 들어가 있는 것을 볼 수 있습니다
참고한 수업 링크
'생활코딩 > 생활코딩웹' 카테고리의 다른 글
관계형 데이터베이스 실습 (0) | 2020.04.10 |
---|---|
관계형 데이터베이스 이론 (0) | 2020.04.10 |
데이터베이스(MySQL) 이론 (0) | 2020.04.09 |
PHP 실습 (0) | 2020.04.09 |
자바스크립트 실습 (0) | 2020.04.08 |