doulian4467 2015-03-22 19:40
浏览 43
已采纳

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

I have this form to upload pictures to my mysql database:

<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. The schema for my photos folder is: caption, image_url, date_taken, imageID

is there something I am doing wrong with the structure? mysqli code? any help will be very much appreciated! Thank you in advance!

  • 写回答

1条回答 默认 最新

  • doushao6874 2015-03-22 20:06
    关注

    As Fred -ii- mentioned, the problem is that you're using a "?" as the value for the column imageID, but you're not using prepared statements. You're not checking for errors, but if you did you'd get something like:

    You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '?)' at line xx

    Also, you're using addslashes to get user data into the query, which is unsafe (you should use mysqli_real_escape_string instead).

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类