简体   繁体   中英

Uploading image in the database using PHP:

It only shows Something went wrong! :(. I've got that code from one site. I don't know where is the problem. The database should be ok also and the code. Can anyone tell me what is wrong?

        <?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>

My database is that:

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 */;

Change this;

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

To this, this will output the real error;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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