简体   繁体   English

使用PHP表单将图像上传到mysql

[英]Uploading images to mysql using php form

I have this form to upload pictures to my mysql database: 我有这种形式将图片上传到我的mysql数据库:

<h4>Add Photo</h4>

<form enctype="multipart/form-data" method="post">
    <?php
    require_once 'config.php';
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

    if (isset($_POST['upload'])){
        $caption   = $_POST['caption'];
        $albumID   = $_POST['album'];
        $file      = $_FILES ['file']['name'];
        $file_type = $_FILES ['file']['type'];
        $file_size = $_FILES ['file']['size'];
        $file_tmp  = $_FILES ['file']['tmp_name'];
        $random_name = rand();

        if (empty($file)){
            echo "Please enter a file <br>";
        } else {
            move_uploaded_file($file_tmp, 'uploads/'.$random_name.'.jpg');
            mysqli_query(
                $mysqli,
                "INSERT INTO photos (caption, image_url, date_taken, imageID) "
                . "VALUES('"
                . addslashes($caption) . "', '"
                . $random_name . ".jpeg', NOW(), ?)"
            );
            echo "Photo successfully uploaded!<br>";
        }
    }
    ?>

    Caption: <br>
    <input type="text" name="caption">
    <br><br>

    Select Album: <br>
    <select name="album">
    <?php
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
    $result = $mysqli->query("SELECT * FROM albums");
    while ($row = $result->fetch_assoc()) {
        $albumID = $row['albumID'];
        $title   = $row['title'];
        echo "<option value='$albumID'>$title</option>";
    }
    ?>
    </select>
    <br><br>

    Select Photo: <br>
    <input type="file" name="file">
    <br><br>

    <input type="submit" name="upload" value="Upload">
</form>

I can successfully upload pictures to the 'uploads' folder on my sever, however nothing is added to the 'photos' table on my database. 我可以将图片成功上传到服务器上的“ uploads”文件夹中,但是不会对数据库的“ photos”表添加任何内容。 The schema for my photos folder is: caption, image_url, date_taken, imageID 我的照片文件夹的架构是:标题,image_url,date_taken,imageID

is there something I am doing wrong with the structure? 我的结构有问题吗? mysqli code? mysqli代码? any help will be very much appreciated! 任何帮助将不胜感激! Thank you in advance! 先感谢您!

As Fred -ii- mentioned, the problem is that you're using a "?" 正如Fred -ii-所述,问题在于您使用的是“?” as the value for the column imageID, but you're not using prepared statements. 作为imageID列的值,但您没有使用准备好的语句。 You're not checking for errors, but if you did you'd get something like: 您不是要检查错误,但如果这样做,则会得到类似以下内容的信息:

You have an error in your SQL syntax; 您的SQL语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near '?)' at line xx 检查与您的MySQL服务器版本相对应的手册,以在第XX行的'?)'附近使用正确的语法

Also, you're using addslashes to get user data into the query, which is unsafe (you should use mysqli_real_escape_string instead ). 另外,您还使用Addslashes将用户数据获取到查询中,这是不安全的(您应该使用mysqli_real_escape_string代替 )。

A good solution to both problems would be to use prepared statements. 解决这两个问题的好方法是使用准备好的语句。 You'd do something like this instead: 您将改为执行以下操作:

    move_uploaded_file($file_tmp, 'uploads/'.$random_name.'.jpg');
    $ret = mysqli_prepare($mysqli, "INSERT INTO photos (caption, image_url, date_taken)
    VALUES(?, ?, NOW())");
    $filename = $random_name + ".jpeg";
    mysqli_stmt_bind_param($ret, "ss", $caption, $filename);
    mysqli_stmt_execute($ret);
    echo "Photo successfully uploaded!<br>";

Update : As the id is autogenerated, I removed the column from the query entirely. 更新 :由于id是自动生成的,因此我从查询中完全删除了该列。

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

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