MySQL 실습 1
실습환경으로 codeanywhere를 쓰고 계신 분은 아래 영상의 13:30 즈음의 영상의 코드를 다음와 같이 바꿔서 사용하시면 됩니다. 비밀번호를 지정하지 않으면 됩니다. $conn = mysqli_connect('localhost', 'root', '');
MySQL 실습 2
MySQL 실습 3
MySQL 실습 4
MySQL 실습 5
index.php
<?php $conn = mysqli_connect("localhost", "root", 111111); mysqli_select_db($conn, "opentutorials"); $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> <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 topic 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>
write.php
<?php $conn = mysqli_connect("localhost", "root", 111111); mysqli_select_db($conn, "opentutorials"); $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> <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", 111111); mysqli_select_db($conn, "opentutorials"); $sql = "INSERT INTO topic (title,description,author,created) VALUES('".$_POST['title']."', '".$_POST['description']."', '".$_POST['author']."', now())"; $result = mysqli_query($conn, $sql); header('Location: http://localhost/index.php'); ?>
style.css
body.white{ background-color:white; color:black; } body.black{ background-color:black; color:white; } header{ border-bottom:1px solid gray; padding:20px; } nav { border-right:1px solid gray; width:200px; height:600px; float:left; } nav ol{ list-style:none; padding:0; } article{ float:left; padding:20px; width:500px; } #control{ float:right; } header img{ float:right; height:100px; }