글쓰기 페이지(create.php)

황인환·2024년 5월 12일

create.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
    
    </head>
    <body>
        <form action="create_p.php" method="POST">
            <table width="500" border="1">
                <tr>
                    <td colspan="6" >Create</td>
                </tr>
                <tr>
                    <th>Title</th>
                    <td><input type="text" name="title" placeholder="Title"></td>
                </tr>
                <tr>
                    <th>Content</th>
                    <td><textarea name="content" placeholder="content"></textarea></td>
                </tr>
                <tr>
                    <th>PassWord</th>
                    <td><input type="password" name="password" placeholder="PassWord"></td>
                </tr>
                <tr>
                    <td colspan="6">
                        <input type="submit" value="Create">
                        <input type="submit" value="Back" formaction="index.php">
                    </td>
                </tr>
            </table>
        </form>
    
    
    </body>
</html>

create.php

create_p.php

<?php
session_start();
include "db2.php";
if(isset($_POST['title']) && isset($_POST['content']) && isset($_POST['password'])){

    if(empty($_POST['title'])){
        echo "<script>
        alert(\"EmptyTitle\");
        history.back(1);
        </script>";
        exit();
    }elseif(empty($_POST['content'])){
        echo "<script>
        alert(\"EmptyContent\");
        history.back(1);
        </script>";
        exit();
    }elseif(empty($_POST['password'])){
        echo "<script>
        alert(\"EmptyPassWord\");
        history.back(1);
        </script>";
        exit();
    }

    $title=$_POST['title'];
    $content=$_POST['content'];
    $pwd=$_POST['password'];
    $author=$_SESSION['name'];
    $date=date("Y-m-d h:i:s");
    $ip=$_SERVER["REMOTE_ADDR"];
    $title=mysqli_escape_string($conn,$title);
    $content=mysqli_escape_string($conn,$content);
    $pwd=mysqli_escape_string($conn,$pwd);

    $sql="INSERT INTO board (title, content, pwd, date, author, ip) VALUES('$title','$content','$pwd','$date','$author','$ip')";
    $result=mysqli_query($conn,$sql);
    if($result){
        header("Location: index.php");
    }else{
        echo "<script>
        alert(\"fail\");
        history.back(1);
        </script>";
        exit();
    }
}else{
    header("Location: index.php");
}

?>

$sql="INSERT INTO board (title, content, pwd, date, author, ip) VALUES('$title','$content','$pwd','$date','$author','$ip')"; 코드는 DB에 데이터를 추가하는 코드로 INSERT INTO table (열 inDB) VAULES(추가할 데이터)이다. 이 코드는 추가하는 것이기에 WHERE조건은 필요없다.

---Normaltic 4주차 4일---

0개의 댓글