Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Saving image to mysql, help needed

    Hello,

    I am trying to save image in mysql table using blob. I am show you my code [form page and backend page for insertion], please let me know where i am wrong ?

    Main form page
    Code:
    <!DOCTYPE html>
    <html>
    <head>
        <title>Forms</title>
        <link href="css/kendo.metro.min.css"rel="stylesheet"> 
            <link href="css/kendo.common.min.css" rel="stylesheet">
            <script src="js/jquery.min.js"></script>
            <script src="js/kendo.web.min.js"></script>
    		<script src="js/kendo.upload.min.js"></script>
    		
    		
    		<script type="text/javascript">
    	$(document).ready(function(){
    		$('#btnSubmit').click(function(){
    
    			$.ajax({
    				type: "POST",
    				url: 'data/create_new_property.php',
    				data: "buildername=" + $('#buildername').val() + "&projectname=" + $('#projectname').val() +  "&files=" + $('#files').val(),
    				success: function(data){
    					$('#status').html(data);
    					$('#buildername').val('');
    					$('#projectname').val('');
    					$('#files').val('');
    				}
    			});
    		});
    	});
    </script>
    </head>
    <body>
      
        <div id="example" class="k-content">
        <div>
            <ul class="forms">
    		
                <li>
                    <input type="text" class="k-textbox" id="buildername" value="Builder Name" 
    				onblur="if (this.value == '') {this.value = 'Builder Name';}"
     onfocus="if (this.value == 'Builder Name') {this.value = '';}"/>
                </li>
    			
    			<li>
                    <input type="text" class="k-textbox" id="projectname" value="Project Name" 
    				onblur="if (this.value == '') {this.value = 'Project Name';}"
     onfocus="if (this.value == 'Project Name') {this.value = '';}"/>
                </li>
    			
    			<li>
    			 <input type="text" class="k-textbox" id="location" value="Location" 
    				onblur="if (this.value == '') {this.value = 'Location';}"
     onfocus="if (this.value == 'Location') {this.value = '';}"/>
                </li>
    			
    			<li>
    			 <input type="text" class="k-textbox" id="budget" value="Budget" 
    				onblur="if (this.value == '') {this.value = 'Budget';}"
     onfocus="if (this.value == 'Budget') {this.value = '';}"/>
                </li>
    			
    			<li>
    			 <input type="text" class="k-textbox" id="propertytype" value="Property Type" 
    				onblur="if (this.value == '') {this.value = 'Property Type';}"
     onfocus="if (this.value == 'Property Type') {this.value = '';}"/>
                </li>
    			
    			 
    			<li>
    			<input name="files" id="files" type="file" />
    			</li>
    			
    			
    			<li>
    			 <input type="text" class="k-textbox" id="area" value="Area" 
    				onblur="if (this.value == '') {this.value = 'Area';}"
     onfocus="if (this.value == 'Area') {this.value = '';}"/>
                </li>
    			
    			
    			 <li>
                    <button class="k-button" id="btnSubmit">Save Project</button>
                </li>
    			
            </ul>
    		
            <style scoped>
                .forms {
                    float: left;
                }
    
                .forms li {
                    margin-bottom: 5px;
                    list-style: none;
                }
    
                .forms li > * {
                    width: 400px;
                }
            </style>
        </div>
    </div>
    
    </body>
    </html>
    php code to Insert data in mysql

    Code:
    <?php
    header('Content-Type: application/json');
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Methods: POST, GET");
    header("Access-Control-Allow-Headers: x-requested-with");
    
    
    mysql_connect("localhost", "root", "") or die("Connection Failed");
    mysql_select_db("property")or die("Connection Failed");
    
    
    $bname = $_POST['buildername'];
    $pname = $_POST['projectname'];
    $uploadfiles = addslashes (file_get_contents($_FILES['files']['tmp_name']));
    
    $image = getimagesize($_FILES['files']['tmp_name']);//to know about image type etc
    $imgtype = $image['mime'];
    //$query = "UPDATE test SET password = '$password' WHERE name = '$user'";
    
    
    
    $query = "INSERT INTO projects (buildername, projectname, img) VALUES('$bname','$pname','$uploadfiles')";
    if(mysql_query($query)){
    echo "updated";}
    else{
    echo "fail";}
    
    ?>

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    What's the actual error you are getting? Enable your error reporting with
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    Coding wise, you shouldn't use addslashes; use mysql_real_escape_string or upgrade to using mysqli (mysql library is deprecated and will be removed in the future) and prepared statements.

    Since this has such a heavy reliance on your use of javascript, you cannot rule that out as the problem. You should correct the HTML to work without javascript first, disable that, and verify it works on the server end first. If that checks out, your problem is the javascript.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Feb 2012
    Posts
    29
    Thanks
    1
    Thanked 1 Time in 1 Post
    Are you sure about keeping image in your database, I would rather keep only informations about image in DB, but image file on your http server, you can restrict specific access to your img by url.

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    There's pros and cons to both approaches.
    My personal recommendation would also be filesystem data with dbms information only. Then you get the best of both worlds; the control and search-ability of the dbms and the speed of the filesystem.
    Just DBMS has the advantage of when you move to a new server, especially if you have two separate machines for the webserver and dbms, than the images go with them.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,091
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Theres another slight advantage that felgall once came up with you know Fou (though I wasn't sure if it was a good idea to put so many images in a table mysef)..

    When you're doing a roll back. Deleted records will still have their images. If they're stored on the filesystem and you do a rollback you'll have a load of records with missing images. Again that was Felgalls point not mine, I've never been sure about storing many images in a DB.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,465
    Thanks
    0
    Thanked 634 Times in 624 Posts
    Quote Originally Posted by tangoforce View Post
    I've never been sure about storing many images in a DB.
    I think this is one of those instances where neither approach is always the beeter way. Only by considering all the pros and cons of both approaches can you decide which of the two uis the better for a given situation.

    Placing the images in the database means that they are
    1. synchronised with the resto fothe associated data - so that a rollback doesn't leave images behind.
    2. security where the image is only accessible to those who have access to that field
    3. automatic backup - where the team responsible for backing up will automatically back up all the images with the database (there have been instances where the application owners forgot to ask to have the image folder backed up and when the database needed to be restored after a drive failure they found that they'd lost all the images

    Keeping the images separate means that
    1. they take up less space
    2. they are accessible by people who don't have access to the database
    3. can be shared with other applications that don't use the database

    and if you think about it a bit you can come up with many more reasons for each having advantages over the other. Then you need to decide which of the advantages are the most important for your application so as to determine which approach to take.

    You can't just say that one approach is right and one is wrong because both can be right depending on the circumstances. Anyone who just states that you should never store images in the database has obvioulsy only ever worked on very small applications because as applications get bigger the advantages that storing the images in the database provide gradually gain in importance.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •