저는 유니 스왑 사이트를 만들려고하는 학생입니다. 나는 문제에 부딪쳤다. 모든 게시물을 검색했는데 비슷한 코드가 있지만 모든 코드가 일치하도록 코드를 변경하려고 할 때. 적어도 친절하거나 적어도 시도해보십시오. 배우고 있습니다.이미지 및 기타 데이터를 MYSQL 데이터베이스에 삽입하십시오.
파일 업로드를 위해 코드를 완전히 변경하려고했지만 심각하게 손실되었습니다. 나는 잘 작동하는 로그인 및 사용자 데이터베이스를 가지고 있지만, 어떤 이유로 '항목'데이터베이스에 저장하기 위해 내 항목 데이터를 가져올 수 없습니다. 메시지 파일을 업로드하지 않았습니다.
게시중인 항목 및 이미지에 대한 정보를 저장하고 세션의 타임 스탬프와 사용자 이름을 입력하려고합니다.
이이 항목에 대한 데이터베이스 설정 :
CREATE TABLE `item` (
`itemid` int(10) NOT NULL PRIMARY KEY,
`iname` varchar(25) NOT NULL,
`idescription` varchar(25) NOT NULL,
`itype` varchar(25) NOT NULL,
`icolour` varchar(25) NOT NULL,
`icategory` varchar(25) NOT NULL,
`image` varchar(25) NOT NULL,
`idate` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`username` varchar(60) NOT NULL
);
그리고 이것은 PHP 페이지의 코드 :
<?php
session_start();
if (!isset($_SESSION['userSession']) || isset($_SESSION['userSession']) && empty($_SESSION['userSession'])) {
header("Location: index.php");
}
require_once 'dbconnect.php';
if(isset($_POST['btn-postitem'])) {
$iname = $_POST['iname'];
$idescription = $_POST['idescription'];
$itype = $_POST['itype'];
$icolour = $_POST['icolour'];
$icategory = $_POST['icategory'];
$uploadDir = "upload/";
$fileName = $_FILES['image']['name'];
$filePath = $uploadDir . $fileName;
$username = $_SESSION['userSession'];
if(move_uploaded_file($_FILES['image']['tmp_name'],"upload/".$_FILES['image']['name'])){
$query_image = "INSERT INTO item(iname,idescription,itype,icolour,icategory,image,idate,username) VALUES('$iname','$idescription','$itype','$icolour','$icategory','$filePath','curdate()','$username')";
// insert the image into db
if(mysql_query($query_image)) {
echo "Stored in: " . "upload/" . $_FILES['image']['name'];
}
else{
echo "File name not stored in database";
}
}
else{
echo 'File not uploaded';
}
}
$db1->close();
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Post Item</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="bootstrap/css/bootstrap-theme.min.css" rel="stylesheet" media="screen">
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="signin-form">
<div class="container">
<form class="form-signin" method="post" id="register-form" enctype="multipart/form-data">
<h2 class="form-signin-heading">Post Item</h2><hr />
<?php
if (isset($msg)) {
echo $msg;
}
?>
<div class="form-group">
<input type="text" class="form-control" placeholder="Item Name" name="iname" required />
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Description" name="idescription" required />
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Type of Item" name="itype" required />
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Item Colour" name="icolour" required />
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Category" name="icategory" required />
</div>
<div class="form-group">
<input type="file" class="form-control" placeholder="Picture" name="image" required />
</div>
<hr />
<div class="form-group">
<button type="submit" class="btn btn-default" name="btn-postitem">
<span class="glyphicon glyphicon-log-in"></span> Post Item
</button>
</div>
</form>
</div>
</div>
</body>
</html>
"업로드"디렉토리를 만들었습니까? 이 코드를'session_start()'줄 앞에 넣으십시오 : error_reporting (E_ALL); ini_set ('display_errors', 1);'. 이제 코드가 작동하지 않는 이유를 알아야합니다. mysql은 PHP 5.5.0에서 더 이상 사용되지 않으며 PHP 7.0.0부터 제거 되었기 때문에 mysqli 또는 PDO를 사용하는 것이 좋습니다. SQL 주입을 피하기 위해 준비된 문을 사용하십시오. –
'itemid' 필드를'unsigned'와'AUTO_INCREMENT'로 정의하십시오. 그렇지 않으면 삽입시 값 0이 수신됩니다. –
당신이'idate'를 정의한 것처럼'INSERT' 문에 필드와'curdate()'값을 포함해서는 안됩니다. 삽입시 자동으로 현재 날짜 - 시간을 얻습니다. 그렇지 않으면, 당신이'curdate()'를 sql 문에 사용하면,'idate' 필드는'0000-00-00 00 : 00 : 00' 값을받습니다. –