简体   繁体   中英

Query will not insert into Database. (Correct Credentials)

I am having a strange problem where my query to Insert values into a database are not being added. I am using MySQL through terminal and the file I am doing these queries through are on my AWS ubuntu instance.

Here is the upload.php file located on my AWS instance:

<?php

    if(isset($_POST["submit"])){
        $check = getimagesize($_FILES["image"]["tmp_name"]);
        if($check !== false){
            $image = $_FILES['image']['tmp_name'];
            $imgContent = addslashes(file_get_contents($image));

            $servername = "localhost";
            $username = "NOTREAL";
            $password = "NOTREAL";
            $dbname = "CS4830";

            //connect
            $db = new mysqli($servername, $username, $password, $dbname); 

            // Check connection
            if($db->connect_error){
                die("Connection failed: " . $db->connect_error);
            }
            else{
                echo "Successful";
            }

            $dataTime = date("Y-m-d H:i:s");    

            //Insert image content into database
            $insert = $db->query("INSERT into images (image, created) VALUES ('$imgContent', '$dataTime')");
            if($insert){
                echo "<br/>";
                echo "File uploaded successfully.";
            }else{
                echo "<br/>";
                echo "File upload failed, please try again. ";
            } 
        }else{
            echo "Please select an image file to upload.";
        }
    }

?>

When I upload an image and the php runs, I end up getting "File upload failed, please try again." printed out on my screen. - The table is named images. Can Anyone identify the possible issue? I thought maybe it had something to do with permissions but the credentials are correct and I can't imagine what permissions would be lacking. Other than that I'm lost at this point. Thank you.

(A simple demo site) http://ec2-54-158-61-31.compute-1.amazonaws.com/images/

$file_name = $_FILES['image']['name']; 
$tmpfilename = $_FILES["image"]["tmp_name"]; 
move_uploaded_file($tmpfilename,"images/".$file_name);  

There is multiple way to store image

if you want to store image into database then you need blob datatype (column data type must be BLOB) then you can used below code

$imagename=$_FILES["myimage"]["name"]; 

//Get the content of the image and then add slashes to it 
$imagetmp=addslashes (file_get_contents($_FILES['myimage']['tmp_name']));

//Insert the image name and image content in image_table
$insert_image="INSERT INTO image_table VALUES('$imagetmp','$imagename')";

mysql_query($insert_image);

Code reference

if you want to add image into directory and store name into database then

$uploaddir = '/var/www/uploads/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

    echo "<p>";

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
      echo "File is valid, and was successfully uploaded.\n";
    } else {
       echo "Upload failed";
    }
//and then execute insert query with image name instead of image

code reference

You can't store the image content in this way. So there are some issue in "$imgContent = addslashes(file_get_contents($image));"

If you want to save image data into database then convert the image content into base64 encode and then try to save the encoded data.

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