PHP表单将所有内容发送到数据库,但图像

时间:2016-04-23 18:05:06

标签: php sql image

对于我正在进行的项目的一部分,我将让用户上传有关一双鞋的信息,以及所述对的图片。表单会让他们填写品牌,型号,年份和尺寸等信息,然后上传图片。

当我测试它并检查数据库时,除了图像外,我得到了关于鞋子的所有信息。我做了一些关于如何将图像上传到数据库的问题,如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

我错过了什么,或者在这里做得不好?任何帮助表示赞赏。

2 个答案:

答案 0 :(得分:1)

我还认为您应该将图像存储在文件中并以某种方式将其链接到寄存器(例如,将文件名存储在数据库中)。

但是如果你想让你当前的代码工作,问题就在于表格。将以下属性添加到表单标记:

enctype="multipart/form-data"

答案 1 :(得分:0)

最好将图像上传到服务器,并将其保存为行ID.jpg

的ID