简体   繁体   English

如何通过php将图像上传到sql数据库

[英]How to upload images to a sql database via php

I am trying to create an AdLister site - and so far everything a user posts into the database displays properly on the site except an image. 我正在尝试创建AdLister网站-到目前为止,用户发布到数据库中的所有内容都可以在该网站上正确显示,除了图像。 Below is my code - Please let me know if I left anything out: 以下是我的代码-如果我遗漏了任何东西,请告诉我:

    $insert_table = "INSERT INTO posts (userid, post_date, title, price, description, email, location, image) VALUES (:userid, :post_date, :title, :price, :description, :email, :location, :image)";
    $stmt = $dbc->prepare($insert_table);
    $stmt->bindValue(':userid', 1, PDO::PARAM_STR);
    $stmt->bindValue(':post_date', $date, PDO::PARAM_STR);
    $stmt->bindValue(':title', $title, PDO::PARAM_STR);
    $stmt->bindValue(':price', $price, PDO::PARAM_STR);
    $stmt->bindValue(':description', $description, PDO::PARAM_STR);
    $stmt->bindValue(':email', $email, PDO::PARAM_STR);
    $stmt->bindValue(':location', $location, PDO::PARAM_STR);
    $stmt->bindValue(':image', $image, PDO::PARAM_STR);

    $stmt->execute();

    return $errors;
    }

    if (!empty($_POST)) {
        if (checkValues()) {

        $errors = insertPost($dbc);         
    } else {
        $message = "Invalid format. Please try again.";
        $javascript = "<script type='text/javascript'>alert('$message');</script>";
        echo $javascript;
    }
}

    if(Input::has('title')){
        if($_FILES) {
            $uploads_directory = '/img';
            $filename = $uploads_directory . basename($_FILES['image']['name']);
            if (move_uploaded_file($_FILES['image']['tmp_name'], $filename)) {
                // echo '<p>The file '. basename( $_FILES['image']['name']). ' has been uploaded.</p>';
            } else {
                //alert("Sorry, there was an error uploading your file.");
            }
        }
       }
        <table class="table table-hover table-bordered table-striped">
        <tr class='table-hover'>
            <th class="header">Photo</th>
            <th class="header col-md-1">Date Posted</th>
            <th class="header">Title</th>
            <th class="header col-md-1">Price</th>
            <th class="header col-md-6">Description</th>
            <th class="header col-md-6">Image</th>
        </tr>

            <?php
            foreach ($posts as $post):?>
                <tr class='table table-hover table-bordered body'>
                    <td>Photo</td>
                    <td><?= $post['post_date'] ?></td>
                    <td><?= $post['title']?></td> 
                    <td><?= $post['price']?></td>
                    <td><?= $post['description']?></td>
                    <td><?= $post['image']?></td>
            <?php endforeach ?>
            </tr>
</table>

I'm guessing your trying to insert the image as a posted string value (like the other inputs) except data received from file inputs is stored as an array called $_FILES 我猜您正在尝试将图像作为发布的字符串值(如其他输入)插入,除了从文件输入接收的数据存储为名为$ _FILES的数组

So... 所以...

$_POST['image']

Will be empty but 将为空,但

$_FILES['image']['name']

Will contain the actual filename. 将包含实际的文件名。

Rearrange your code to upload the file and get the name, then use that value to insert. 重新排列代码以上传文件并获取名称,然后使用该值插入。

Oh and don't forget to include: 哦,别忘了包括:

enctype="multipart/form-data"

on your html form tag! 在您的html表单标签上!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM