0

Hello and thank you for taking the time to read this. I am having trouble saving my image to the database. The input from the comment which is a string and the input from the rating will go in but the image will not save to my database. trying to create a site that will take images from someone who is rating their haircut. The code i have come up with is below.

<form method="post" id = "bodyContainer" enctype="multipart/form-data">
<img src="" name="image" id="image"/>
<br/>
<input style="float:right;"type="file" name="dataFile" id="fileChooser" onchange="return ValidateFileUpload()" />

<SCRIPT type="text/javascript">
    function ValidateFileUpload() {
        var fuData = document.getElementById('fileChooser');
        var FileUploadPath = fuData.value;

    //To check if user upload any file
        if (FileUploadPath == '') {
            alert("Please upload an image");

        }   else {
                var Extension = FileUploadPath.substring(FileUploadPath.lastIndexOf('.') + 1).toLowerCase();

                //The file uploaded is an image
                if (Extension == "gif" || Extension == "png" || Extension == "bmp" || Extension == "jpeg" || Extension == "jpg") {

                // To Display
                    if (fuData.files && fuData.files[0]) {
                        var reader = new FileReader();

                        reader.onload = function(e) {
                            $('#image').attr('src', e.target.result);
                        }

                        reader.readAsDataURL(fuData.files[0]);
                    }

                } 

                //The file upload is NOT an image
                else {
                    alert("Photo only allows file types of GIF, PNG, JPG, JPEG and BMP. ");

                }
            }
    }
</SCRIPT>   

<textarea id="comment" name="comment" placeholder="Your comment..."></textarea><br/>
<input type="range" name="rating" min="1" max="5" id="rating"/><br/>
<input type="submit" name="submit" value="Submit" id="submitButton"/><br/>

The main issue i believe arises from the php that adds the code to the database below.

<?php
    if (isset($_POST['submit'])) {
        if ($_POST['comment']) {

            $comment = addslashes($_POST['comment']);
            $rating = $_POST['rating'];

            $image = addslashes($_FILES['image']['tmp_name']);
            $name = addslashes($_FILES['image']['name']);
            $image = file_get_contents($image);
            $image = base64_encode($image) ;

            $link = mysqli_connect("localhost", "root", "root","DJP"); 
            $query = "INSERT INTO reviews (comment, image) VALUES ('$comment', '$image')";
            $result = mysqli_query($link, $query);    
            if ($result) {
                echo '<script type="text/javascript">alert("image uploaded");</script>';
            }else{
                echo '<script type="text/javascript">alert("image not uploaded");</script>';
            }
        }
    }
?>
0

2 Answers 2

1

The problem is because of your name attribute. Look at this statement here,

<input style="float:right;"type="file" name="dataFile" id="fileChooser" onchange="return ValidateFileUpload()" />
                                                ^ see here

It should be,

<input style="float:right;" type="file" name="image" id="fileChooser" onchange="return ValidateFileUpload()" />
Sign up to request clarification or add additional context in comments.

5 Comments

ok i understand i just tried that. I must have been under the assumption it would take it from the image. You were very helpful.
@MichaelVickers Glad I could help. :-)
@devpro <img> tag has nothing to do with <input> tag. The problem is, the name attribute in <input> tag is wrong.
thats fine, actually there r two solution for this, one u suggested and second one i suggested :) good work
@devpro Thanks buddy. ;-)
1

Your input type file name is dataFile not image. so you can get file information as

$image = addslashes($_FILES['dataFile']['tmp_name']);
$name = addslashes($_FILES['dataFile']['name']);
$image = file_get_contents($image);

Note that you have defined name='image' in <img> tag. you can't get this in $_FILES.

1 Comment

This could be the alternative approach. +10 from me. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.