J-Play J-Play - 1 year ago 37
SQL Question

PHP form sends everything to database except image

For part of a project I am doing, I will have users upload information about a pair of shoes, along with a picture of said pair. A form has them fill out information such as the brand, model, year, and size, and then to upload a picture.

When I test it out, and I check the database, I get all the information about the shoe except for the image. I did some researching on a few questions like this one on how to upload an image to the database, so I know I need to set the image attribute on the table to blob, which I did, and the SQL command seems pretty straightforward as well. The only thing I don't want to upload is the image_name, because I don't find it relevant or pertinent to my project.

I know some people will say not to upload an image to the database, but in terms of my project, it is a little easier for me to upload it to the database as compared to storing it in a folder and then moving it.

Here is my code for the form below.


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">
<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'>
<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 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>
<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>
<form class="form-horizontal" role="form" method="post"
<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"
<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 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"
<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"
<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 class="form-group">
<div class="col-md-offset-4 col-md-2">
<button type="submit" name="post" class="btn btn-default">Post</button>
<div class="container">
<div class="col-md-8 col-md-offset-2">
<h3 id="signUpMessage"></h3>

It should also be noted that when I test out the form, I get this message beforehand:

Warning: file_get_contents(): Filename cannot be empty in C:\xampp\htdocs\SneakerTrade\shoe.php on line 26

What am I missing, or not doing correctly here? Any help is appreciated.

Answer Source

I also think you should store the image in a file and link it to the register somehow (for example, storing the filename in the db).

But if you want to make your current code work, it's likely that the problem is in the form. Add the following attribute to the form tag: