在线相册 - 上传的图像不会显示在mysql数据库中

时间:2015-03-17 16:15:48

标签: mysql mysqli phpmyadmin image-uploading

我正在使用mysql和php创建一个在线相册。我使用phpmyadmin来管理我的数据库。到目前为止,我有以下代码:

    <form method='post'>
        Album Name: <input type="text" name="title" /> 
        <input type="submit" name="submit" value="create" />
    </form>
<h4>Add Photo</h4>
<form enctype="multipart/form-data" method="post">
        <?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($caption, $random_name.jpeg, NOW(), $albumID)");
            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>

使用此代码,我可以成功将照片上传到“上传”状态。我服务器上的文件夹。但是,当我查看phpmyadmin时,“照片”中没有添加任何照片。表。 我应该补充一下我的照片的模式&#39;和专辑&#39;表是: 照片(标题,image_url,date_taken,imageID,userID) ALBUMS(title,date_created,date_modified,albumID)

我的代码出了什么问题,导致上传的图片没有显示在数据库中?

谢谢!

1 个答案:

答案 0 :(得分:0)

SQL语法错误。更好的引用.... 并且不要忘记削减:)

mysqli_query($mysqli, "INSERT INTO photos (caption, image_url, date_taken, imageID)
VALUES('" . addslashes($caption) . "', '" . $random_name . ".jpeg', NOW(), " . $albumID . ")");