上传文本&图像与一个形式,并将其存储在路径&数据库


upload text & image with a form and storing it in a path & database

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

当我点击提交时,它什么也不做,只是刷新同一个页面。如果我不使用图像代码,我可以将文本存储在数据库中。

我做错了什么?请帮助

这是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>

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

代码中的一些行更改如下:

<?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 CODE

<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>