使用PHP在数据库中上传图像:

时间:2014-03-25 11:02:54

标签: php mysql image

它只显示出错了! :(。我从一个站点获得了该代码。我不知道问题出在哪里。数据库也应该没问题和代码。谁能告诉我什么是错的?

        <?php
        include 'config.php';
        session_start();
        if(isset($_POST['button_upload_image'])){
        $username=$_SESSION['username'];
        $image = addslashes(file_get_contents($_FILES['image']['tmp_name'])); //SQL Injection defence!
        $image_name = addslashes($_FILES['image']['name']);
        $sql = "INSERT INTO `images` (`image`, `image_name`,`username`)VALUES('$image', '$image_name','$username')";
        if (!mysql_query($sql)) { // Error handling
            echo "Something went wrong! :("; 
        }
        }
        ?>
        <!DOCTYPE html>
        <html>
            <head>
                <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            </head>
            <body>
                <?php
                    include $main_directory.'parts/header_admin.php';
                ?>
                <?php
                    include $main_directory.'parts/slider.php';
                ?>
                <?php
                    include $main_directory.'parts/bottom_menu_hidden.php';
                ?>
        <div class="center_all">
        <div class="all_content">
        <div class="images_gallery">
        <form method="POST" enctype="multipart/form-data">
            <label>File: </label>
            <input type="file" name="image" />
            <input type="submit" name="button_upload_image" />
        </form>
        </div>
        </div>
        </div>
                <?php
                    include $main_directory.'parts/footer.php';
                ?>
            </body>
        </html>

我的数据库是:

CREATE TABLE IF NOT EXISTS `images` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `image` blob NOT NULL,
  `image_name` text NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

1 个答案:

答案 0 :(得分:0)

改变这一点;

if (!mysql_query($sql)) { // Error handling
    echo "Something went wrong! :("; 
}

对此,这将输出实际错误;

if (!mysql_query($sql)) { // Error handling
    var_dump(mysql_error());
    die;
}