对于我所做的一个项目的一部分,我将让用户上传有关一双鞋子的信息,以及一对图片。他们填写表格填写品牌,型号,年份和尺寸等信息,然后上传图片。PHP表单将所有内容发送到数据库,除了图片
当我测试它,并检查数据库时,我会得到有关鞋子的所有信息,除了图像。我做了一些关于如何将图像上传到数据库的问题的一些研究,所以我知道我需要将表上的image属性设置为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