라이브러리 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" > </head> <body id= "target" > <header> <img src= "https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" alt= "생활코딩" > </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'" /> </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" > </head> <body id= "target" > <header> <img src= "https://s3.ap-northeast-2.amazonaws.com/opentutorials-user-file/course/94.png" alt= "생활코딩" > </header> <nav> <ol> <?php while ( $row = mysqli_fetch_assoc( $result )){ } ?> </ 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'" /> </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 ); ?> |