上传文字&带有表格的图像并将其存储在路径中。数据库

时间:2016-11-05 11:36:51

标签: php html mysql

我一直在尝试将图像存储在数据库中,但它不起作用。它适用于文本,但不适用于图像上传。

当我点击提交时,它只会刷新同一页面。如果我不使用图像代码(afbeelding),我可以在数据库中存储文本。

我做错了什么?请帮忙

这是PHP

<?php

include("connect.php");


if(isset($_POST['titel']) && isset($_POST['content']) && isset($_POST['afbeelding'])){

    $titel = $_POST['titel'];
    $content = $_POST['content'];
    $afbeelding = $_POST['afbeelding'];

    $tmp = $_FILES['afbeelding']['tmp_name'];
    $file_location = 'gallery/'.date("YmdHis").'_'.$_FILES['afbeelding']['name'];

    move_uploaded_file($tmp, $file_location);

    $sql = "INSERT INTO `portfolio_items` (titel, content, date, afbeelding, active) VALUES (:titel, :content, NOW(), :afbeelding, 1)";

    $query = $conn->prepare($sql);
    $query->bindParam(':titel', $titel, PDO::PARAM_STR);
    $query->bindParam(':content', $content, PDO::PARAM_STR);
    $query->bindParam(':afbeelding', $afbeelding, PDO::PARAM_STR);
    $query->execute();

    echo "Opgeslagen!";

}

这是表格

<form method="POST">
        <input type="hidden" name="verzonden" value="1" />
        <input type="text" name="titel" required placeholder="Titel" maxlength="20">
        <br>
        <textarea rows="13" cols="30" required name="content" placeholder="Content" id="tekst" maxlength="400"></textarea>
        <br>
        <input type="file" id="afbeelding" accept="image/jpeg, image/x-png, image/gif" required>
        <br> <br>
        <input type="submit" name="submit" value="Opslaan">
</form>

对不起,我的英文很乱,不是我的母语

1 个答案:

答案 0 :(得分:0)

代码中的一些行更改请参见下文:

<?php

include("connect.php");


if(isset($_POST['titel']) && isset($_POST['content']) && isset($_FILES['afbeelding'])){

$titel = $_POST['titel'];
$content = $_POST['content'];

$tmp = $_FILES['afbeelding']['tmp_name'];
$file_location = 'gallery/'.date("YmdHis").'_'.$_FILES['afbeelding']['name'];

    move_uploaded_file($tmp, $file_location);

$sql = "INSERT INTO `portfolio_items` (titel, content, date, afbeelding, active) VALUES (:titel, :content, NOW(), :afbeelding, 1)";


$query = $conn->prepare($sql);
$query->bindParam(':titel', $titel, PDO::PARAM_STR);
$query->bindParam(':content', $content, PDO::PARAM_STR);
$query->bindParam(':afbeelding', $_FILES['afbeelding']['name'], PDO::PARAM_STR);
$query->execute();

echo "Opgeslagen!";

}

表格代码

<form method="POST" enctype="multipart/form-data">
        <input type="hidden" name="verzonden" value="1" />
        <input type="text" name="titel" required placeholder="Titel" maxlength="20">
        <br>
        <textarea rows="13" cols="30" required name="content" placeholder="Content" id="tekst" maxlength="400"></textarea>
        <br>
        <input type="file" id="afbeelding" name="afbeelding" accept="image/jpeg, image/x-png, image/gif" required>
        <br> <br>
        <input type="submit" name="submit" value="Opslaan">
    </form>