【问题标题】:Why image is not saved to the database?为什么图像没有保存到数据库中?
【发布时间】:2019-03-26 22:34:12
【问题描述】:

我正在尝试创建一个用户必须填写其详细信息并上传其选择的图片的注册页面。当我按下注册按钮时,出现以下错误:“(!)注意:未定义索引:/strath-cis/2018/SmartCommute/php/register.php 中的图像”和“(!)警告:file_get_contents(): /strath-cis/2018/SmartCommute/php/register.php 中的文件名不能为空" 在我的数据库中,我确实有一个名为“图像”的 BLOB 类型的字段。 我确信我连接数据库的详细信息是正确的。 有谁知道为什么会这样?

下面是我的代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
    <meta name="mobile-web-app-capable" content="yes"/>
    <meta name="apple-mobile-web-app-capable" content="yes"/>

    <link rel="stylesheet" type="text/css" href="../css/normalize.css"/>
    <link rel="stylesheet" type="text/css" href="../css/register_css.css"/>
    <title>Smart Commute</title>
</head>
<body>
<header id="headerBar">Smart Commute - Register</header>
<form class="needs-validation" novalidate action="" method="post">
    <p><input type="file"  accept="image/*" name="image" id="file"  onchange="loadFile(event)" style="display: none;"></p>
    <p><label for="file" style="cursor: pointer;">Upload Image</label></p>
    <p><img id="output" width="200" /></p>

    <script>
        var loadFile = function(event) {
            var image = document.getElementById('output');
            image.src = URL.createObjectURL(event.target.files[0]);
        };
    </script>

    <p>Name: <input type="text" class="form-control" name="name" id="name" placeholder="John Doe" autocomplete="off" value="<?php if(isset($_POST['Register'])){echo $_POST['name'];} ?>" required></p>
    <p>Email: <input type="text" class="form-control" name="email" id="email" placeholder="fake@mail.com" autocomplete="off" value="<?php if(isset($_POST['Register'])){echo $_POST['email'];} ?>" required></p>
    <p>Password: <input type="password" class="form-control" name="password" id="password" placeholder="Enter password" autocomplete="off" value="<?php if(isset($_POST['Register'])){echo $_POST['password'];} ?>" required></p>
    <br><br>
    <!--    <input type="submit" value="Submit" name="Register">-->
    <button name="Register">Register</button>
</form>
</body>
</html>


<?php

session_start();

    if (isset($_POST['Register'])  && isset($_POST["image"]) ) {


        if (empty($_POST['password']) || preg_match('/\s/', $_POST['password'])) {
            echo "No spaces allowed in new password!";
        } else {
            include("../include/db_config.php");
            include("../include/db_connect.php");



            // image file directory
            $image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL I


            $name = mysqli_real_escape_string($db, $_POST['name']);
            $email = mysqli_real_escape_string($db, $_POST['email']);
            $password = mysqli_real_escape_string($db, $_POST['password']);
            $encryptedPassword = md5($password);


            $sql = "SELECT * FROM `cs317madt`.`useracc` WHERE `email` = '$email'";
            if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $result = $db->query($sql);
                if (mysqli_num_rows($result) == 1) {
                    echo "User already exists";
                } else {
                    $sql2 = "INSERT INTO `cs317madt`.`useracc` (`id`,`name`,`password`,`email`, `image`) VALUES (NULL, '$name', '$encryptedPassword', '$email','$image');";
                    if ($result2 = $db->query($sql2)) {
                        header("location: ../php/home.php");
                        $_SESSION['logged-in'] = true;
                        $_SESSION['name'] = $name;
                    } else {
                        echo "Something went wrong with creating your account!";
                    }
                }
            } else {
                echo "Invalid email! Please enter a valid email";
            }
        }
    }
?>

【问题讨论】:

  • 切勿以明文形式或使用 MD5/SHA1 存储密码! 仅存储密码哈希值。使用 PHP 的 password_hash()password_verify() 。如果您运行的 PHP 版本低于 5.5(我真的希望您不是),您可以使用 password_compat 库来获得相同的功能。
  • 警告:您对SQL Injections 持开放态度,应该真正使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何类型的输入,尤其是来自客户端的输入。即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your data

标签: javascript php mysqli


【解决方案1】:

上传文件时,您需要在&lt;form&gt; 标记中包含enctype="multipart/form-data" 属性。 Have a quick read on the php's manual about file uploads.。您会注意到有一个 Note 部分明确提到了这一点。

所以,例如:

<form enctype="multipart/form-data" class="needs-validation" novalidate action="" method="post">

【讨论】:

  • 还是不行!基本上现在它甚至没有在数据库上存储任何东西
  • 好的,问题是在数据库中我需要将图像类型更改为 longblob 而不是 blob。谢谢!!!
猜你喜欢
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
  • 2019-11-29
  • 2012-02-10
  • 2019-09-04
  • 2014-10-02
  • 2020-05-26
相关资源
最近更新 更多