🔓데이터베이스/SQL

sql input 이슈관련 수정전후코드.

하얀성 2023. 11. 16. 20:10

수정전 p-process.php

내가 뭘 그리 잘못했니?

<?

if (!$wname){
      echo("
          <script>
          window.alert('등록자명을 입력해주세요')
          history.go(-1)
          </script>
         ");
      exit;
}

if (!$summary){
      echo("
          <script>
          window.alert('사진 설명을 간단히 적어주세요')
          history.go(-1)
          </script>
         ");
      exit;
}

if ($userfile == "none"){
      echo("
          <script>
          window.alert('업로드 할 사진을 골라주세요')
          history.go(-1)
          </script>
        ");
      exit;
}

$wdate = date("y-m-d");

if ($userfile) {
      $savedir = "./photo";
      $temp = $userfile_name;
      if (file_exists("$savedir/$temp")) {
         echo (" 
                <script>
                window.alert('서버에 동일한 화일이 이미 존재합니다')
                history.go(-1)
                </script>
               ");
         exit;
      } else {
         copy($userfile,   "$savedir/$temp");
         unlink($userfile);
      }
}

//mysql 데이타베이스에 연결
$con = mysql_connect("localhost","root","apmsetup");
mysql_select_db("comma",$con);

//데이터베이스에 글에 대한 정보 저장
$result = mysql_query("insert into photo(wname, summary, wdate, userfile, passwd) values('$wname', '$summary', '$wdate', '$userfile_name', '$passwd')", $con);

mysql_close($con);	//데이터베이스 연결해제

if (!$result) {
     echo("
        <script>
        window.alert('사진 등록에 실패했습니다.')
        history.go(-1)
        </script>
     ");
     exit;
} else {
     echo("
        <script>
        window.alert('사진 등록이 완료되었습니다')
        </script>
     ");
     echo ("<meta http-equiv='Refresh' content='0; url=p-show.php'>");
}

?>

수정 후 p-process.php

<?php

// $_POST와 $_FILES 배열에서 값을 안전하게 가져옵니다.
$wname = isset($_POST['wname']) ? $_POST['wname'] : '';
$summary = isset($_POST['summary']) ? $_POST['summary'] : '';
$passwd = isset($_POST['passwd']) ? $_POST['passwd'] : '';
$userfile = isset($_FILES['userfile']['tmp_name']) ? $_FILES['userfile']['tmp_name'] : '';
$userfile_name = isset($_FILES['userfile']['name']) ? $_FILES['userfile']['name'] : '';

if (!$wname) {
    echo "<script>window.alert('등록자명을 입력해주세요'); history.go(-1);</script>";
    exit;
}

if (!$summary) {
    echo "<script>window.alert('사진 설명을 간단히 적어주세요'); history.go(-1);</script>";
    exit;
}

if ($userfile == "none") {
    echo "<script>window.alert('업로드 할 사진을 골라주세요'); history.go(-1);</script>";
    exit;
}

$wdate = date("y-m-d");

if ($userfile) {
    $savedir = "./photo";
    $temp = $userfile_name;
    if (file_exists("$savedir/$temp")) {
        echo "<script>window.alert('서버에 동일한 화일이 이미 존재합니다'); history.go(-1);</script>";
        exit;
    } else {
        move_uploaded_file($userfile, "$savedir/$temp");
    }
}

// mysqli 데이터베이스에 연결
$con = mysqli_connect("localhost", "root", "apmsetup", "comma");

// 연결 확인
if (!$con) {
    echo "<script>window.alert('데이터베이스 연결에 실패했습니다.'); history.go(-1);</script>";
    exit;
}

// SQL 쿼리 실행
$query = "INSERT INTO photo (wname, summary, wdate, userfile, passwd) VALUES (?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($con, $query);

// 바인딩 및 실행
mysqli_stmt_bind_param($stmt, "sssss", $wname, $summary, $wdate, $userfile_name, $passwd);
$result = mysqli_stmt_execute($stmt);

mysqli_stmt_close($stmt);
mysqli_close($con); // 데이터베이스 연결 해제

if (!$result) {
    echo "<script>window.alert('사진 등록에 실패했습니다.'); history.go(-1);</script>";
    exit;
} else {
    echo "<script>window.alert('사진 등록이 완료되었습니다');</script>";
    echo "<meta http-equiv='Refresh' content='0; url=p-show.php'>";
}

?>