수업소개
mysqli API를 활용해서 MySQL에 글을 생성하는 방법을 알아봅니다.
강의
예제
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <!doctype html> <html> <head> <meta charset= "utf-8" > <title>WEB</title> </head> <body> <h1>WEB</h1> <ol> <li>HTML</li> </ol> <a href= "create.php" >create</a> <h2>Welcome</h2> Lorem ipsum dolor sit amet, consectetur adipisicing elit </body> </html> |
create.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <!doctype html> <html> <head> <meta charset= "utf-8" > <title>WEB</title> </head> <body> <h1>WEB</h1> <ol> <li>HTML</li> </ol> <form action= "process_create.php" method= "POST" > <p><input type= "text" name= "title" placeholder= "title" ></p> <p><textarea name= "description" placeholder= "description" ></textarea></p> <p><input type= "submit" ></p> </form> </body> </html> |
process_create.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 $conn = mysqli_connect( 'localhost' , 'root' , '111111' , 'opentutorials' ); $sql = " INSERT INTO topic (title, description, created) VALUES( '{$_POST[' title ']}' , '{$_POST[' description ']}' , NOW() ) "; $result = mysqli_query( $conn , $sql ); if ( $result === false){ echo '저장하는 과정에서 문제가 생겼습니다. 관리자에게 문의해주세요' ; error_log (mysqli_error( $conn )); } else { echo '성공했습니다. <a href="index.php">돌아가기</a>' ; } ?> |