私が行っているプロジェクトの一環として、ユーザーはそのペアの画像と共に、一対の靴に関する情報をアップロードします。フォームには、ブランド、モデル、年、サイズなどの情報を記入し、写真をアップロードするフォームがあります。PHPフォームは、画像以外のすべてをデータベースに送信します
私がテストしてデータベースをチェックすると、イメージ以外のすべての靴の情報が得られます。私はデータベースに画像をアップロードする方法について、this oneのようないくつかの質問を調べたので、テーブルの画像属性をBLOBに設定する必要があることを知っています。SQLコマンドもかなり簡単です。私がアップロードしたくないのはimage_nameだけです。なぜなら、私はそれが私のプロジェクトに関連しているか適切ではないからです。
私は、データベースに画像をアップロードしないと言う人もいるかもしれませんが、私のプロジェクトに関しては、フォルダに保存して移動するのと比べて、データベースにアップロードする方が少し簡単ですそれ。
以下は、このフォームのコードです。
Shoe.php:
<?php
session_start();
require 'connect.php';
if (! isset ($_SESSION ['user']) || ! isset ($_SESSION ['logged_in'])) {
header ("Location: login.php");
}
$sql = "SELECT * FROM users WHERE Username = :username";
$stmt = $pdo->prepare ($sql);
// Bind value.
$stmt->bindValue (':username', $_SESSION ['user']);
// Execute.
$stmt->execute();
// Fetch row.
$user = $stmt->fetch (PDO::FETCH_ASSOC);
$image = addslashes (file_get_contents ($_FILES ['image'] ['tmp_name']));
if (isset ($_POST ["post"])) {
$brand = ! empty ($_POST ['brand']) ? trim ($_POST ['brand']) : null;
$model = ! empty ($_POST ['model']) ? trim ($_POST ['model']) : null;
$year = ! empty ($_POST ['year']) ? trim ($_POST ['year']) : null;
$size = ! empty ($_POST ['size']) ? trim ($_POST ['size']) : null;
$sql = "INSERT INTO shoe (Brand, Model, Year, Size, UserID, Image) VALUES (:brand, :model, :year, :size, :userID, :image)";
$stmt = $pdo->prepare ($sql);
// Bind our variables.
$stmt->bindValue (':brand', $brand);
$stmt->bindValue (':model', $model);
$stmt->bindValue (':year', $year);
$stmt->bindValue (':size', $size);
$stmt->bindValue (':image', $image);
$stmt->bindValue (':userID', $user ['UserID']);
// Execute the statement and insert the new shoe information.
$result = $stmt->execute();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Post Shoes</title>
<!-- Bootstrap core CSS -->
<link href="css/bootstrap.min.css" rel="stylesheet">
<!-- Index Custom CSS -->
<link href="css/profile.css" rel="stylesheet">
<!-- Animate.css -->
<link href="css/animate.css" rel="stylesheet">
<!-- Custom styles for this website -->
<link href="css/custom.css" rel="stylesheet">
<link href='https://fonts.googleapis.com/css?family=Fugaz+One'
rel='stylesheet' type='text/css'>
<link href='https://fonts.googleapis.com/css?family=Fjalla+One'
rel='stylesheet' type='text/css'>
</head>
<body>
<nav class="navbar navbar-inverse navbar-fixed-top">
<div class="container-fluid">
<div class="navbar-header">
<a href="profile.php" class="navbar-brand animated fadeInLeft"><?php echo $user['firstName'], " ", $user['lastName'];?></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav navbar-right animated fadeInRight">
<li><a href="home.php">CLOSET</a></li>
<li><a href="shoe.php">POST</a></li>
<li><a href="search.php">SEARCH</a></li>
<li><a href="settings.php">SETTINGS</a></li>
<li><a href="#">HELP</a></li>
<li><a class="logout" href="logout.php">LOGOUT</a></li>
</ul>
</div>
</div>
</nav>
<div class="container">
<div class="col-md-8 col-md-offset-2 profile">
<h1 class="profile-header">Post Shoes</h1>
<h3 class="">Post information about the shoes you wish to trade.</h3>
</div>
</div>
<form class="form-horizontal" role="form" method="post"
action="shoe.php">
<div class="form-group">
<label for="inputBrand"
class="col-md-2 col-md-offset-2 control-label">Brand</label>
<div class="col-md-4">
<input type="text" class="form-control" id="inputBrand" name="brand"
placeholder="Adidas">
</div>
</div>
<div class="form-group">
<label for="inputModel"
class="col-md-2 col-md-offset-2 control-label">Model</label>
<div class="col-md-4">
<input type="text" class="form-control" id="inputModel" name="model"
placeholder="Superstar II">
</div>
</div>
<div class="form-group">
<label for="inputYear" class="col-md-2 col-md-offset-2 control-label">Year</label>
<div class="col-md-4">
<input type="text" class="form-control" id="inputYear" name="year"
placeholder="2015">
</div>
</div>
<div class="form-group">
<label for="inputSize" class="col-md-2 col-md-offset-2 control-label">Size</label>
<div class="col-md-4">
<input type="text" class="form-control" id="inputSize" name="size"
placeholder="13">
</div>
</div>
<div class="form-group">
<label for="uploadImage"
class="col-md-2 col-md-offset-2 control-label">Picture</label>
<div class="col-md-4">
<input type="file" name="image" id="image">
</div>
</div>
<div class="form-group">
<div class="col-md-offset-4 col-md-2">
<button type="submit" name="post" class="btn btn-default">Post</button>
</div>
</div>
</form>
<div class="container">
<div class="col-md-8 col-md-offset-2">
<h3 id="signUpMessage"></h3>
</div>
</div>
</body>
</html>
また、私は、フォームをテストするとき、私は事前にこのメッセージを得ることに留意すべきである:
Warning: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\SneakerTrade\shoe.php on line 26
を私が行方不明、または正しくやっていないですここに?どんな助けもありがとうございます。
var_dump($ image)と結果を確認してください – IamFaysal