为什么图像不保存到数据库?

时间:2019-03-26 22:34:12

标签: javascript php mysqli

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

以下是我的代码:

<!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";
            }
        }
    }
?>

1 个答案:

答案 0 :(得分:0)

上载文件时,需要在enctype="multipart/form-data"标签中包含<form>属性。 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">