无法将图像文件上传到服务器

时间:2020-01-12 18:15:51

标签: php mysql mysqli blob prepared-statement

我试图将带有准备好的语句的BLOB上载到MYSQL数据库,但是我找不到它不起作用的原因。有人可以解释一下原因吗?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Registrieren</title>
</head>
<body>
<header>

    <?php
    if (isset($_POST['submit'])) {
        require_once "db_connect.php";

        //Arbeit mit prepared Statements, da diese gegen SQL-Injections sicher sind im Gegensatz zum Escapen
        $stmt = $mysqli->prepare("INSERT INTO Bild (Bild_ID, Inhalt, Info) VALUES (?, ?, ?)");
        $stmt->bind_param("ibs", $picID, $image, $info);
        // set parameters and execute
        $picID = NULL;
        $image = file_get_contents($_FILES['userPic']['name']);
        echo($image);
        $info = "just an example";
        $stmt->execute();

        echo "New records created successfully";
        $stmt->close();
        $mysqli->close();
    }
    ?>
</header>
<main>
    <form action="" method="post" enctype="multipart/form-data">
        <input id="UserPicUpload" type="file" name="userPic" accept="image/*">
        <button id="send" type="submit" name="submit" >Bild Hochladen</button>
    </form>
</main>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这里有几个错误...

  1. 绑定参数时,甚至都没有定义它们-您定义 绑定后ContentResolverimagepicID
  2. 使用info键可以上传文件的内容。

关于picID:根据使用情况,您可以可以在数据库中将其设置为tmp_name。由于类型是整数,因此我怀疑您实际上是要手动设置这些数字。

这将为我们提供以下代码:

AUTO_INCREMENT

另一个字:用德语或英语命名您的内容,并在全球范围内使用,但不要混淆语言。将 $image = file_get_contents($_FILES['userPic']['tmp_name']); $info = "just an example"; $stmt = $mysqli->prepare("INSERT INTO Bild (Inhalt, Info) VALUES (?, ?)"); $stmt->bind_param("bs", $image, $info); $stmt->execute(); echo "New records created successfully"; 命名为Bild_ID真是令人困惑。