수정전 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'>";
}
?>
'🔓데이터베이스 > SQL' 카테고리의 다른 글
게시판 여러 추가기능 만들어보기 (0) | 2023.11.26 |
---|---|
게시판리스트 스타일 수정 및 아이콘 추가 (0) | 2023.11.24 |
php 게시판만들기 관련 영상(1,2편) (0) | 2023.11.14 |
오늘은 다사다난하다(환경관리의 중요성) (0) | 2023.11.10 |
메모장 만들기 [primary key 선언하기] (0) | 2023.11.08 |