Can't save image name into database

I have a code to upload an image, it works just fine. The problem I am having is that I can’t seem to get the file name inserted into the database. When I execute the script without the code to insert into mysql db it runs fine, but with it, the page goes blank. Can someone help me out? This is the code.

$con=mysqli_connect("localhost","user","***","db");
// Check connectionif (mysqli_connect_errno())  {  echo "Failed to connect to MySQL: " . mysqli_connect_error();  }
mysqli_query($con,"INSERT INTO image (image) VALUES ('$_FILES['image']['name']')");
mysqli_close($con);

Thanks in advance!

The first thing I would try is putting the file name into a variable and echoing it out to see it contains what you expect and then use that variable in your insert.
I always try and keep things simple and you can always make it more streamlined when its working.


$photo = $_FILES['image']['name'];
echo $photo;
mysqli_query($con,"INSERT INTO image (image) VALUES ($photo)");

Well I did what you said, no luck. I’ve been scouring the web and altering the code a little here and there, but no luck. I wonder if it is the placement within the code? I’ve included the full page.

<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");

$temp = explode(".", $_FILES["image"]["name"]);

$extension = end($temp);

if ((($_FILES["image"]["type"] == "image/gif")

|| ($_FILES["image"]["type"] == "image/jpeg")

|| ($_FILES["image"]["type"] == "image/jpg")

|| ($_FILES["image"]["type"] == "image/pjpeg")

|| ($_FILES["image"]["type"] == "image/x-png")

|| ($_FILES["image"]["type"] == "image/png"))

&& ($_FILES["image"]["size"] < 200000)

&& in_array($extension, $allowedExts))

  {

  if ($_FILES["file"]["error"] > 0)

    {

    echo "Return Code: " . $_FILES["image"]["error"] . "<br>";
    }


  else

    {

    echo "Upload: " . $_FILES["image"]["name"] . "<br>";

    echo "Type: " . $_FILES["image"]["type"] . "<br>";

    echo "Size: " . ($_FILES["image"]["size"] / 1024) . " kB<br>";

    echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br>";


    if (file_exists("upload/" . $_FILES["image"]["name"]))

      {

      echo $_FILES["image"]["name"] . " already exists. ";

      }

    else

      {

      move_uploaded_file($_FILES["image"]["tmp_name"],

      "upload/" . $_FILES["image"]["name"]);

      echo "Stored in: " . "upload/" . $_FILES["image"]["name"];
       if(move_uploaded_file($_FILES["image"]["tmp_name"],"upload/" . $_FILES["image"]["name"]);

         {
        $con=mysqli_connect("localhost","uname","***","dbname");
         if (mysqli_connect_errno())

           {

           echo "Failed to connect to MySQL: " . mysqli_connect_error();

            }

         $photo = $_FILES['image']['name'];

         mysqli_query($con,"INSERT INTO image (image) VALUES ('$photo')");

         mysqli_close($con);
          else

          {

          echo 'File name not stored in database';

          }

        }

      }

    }

  }

else  {  echo "Invalid file";  }

?>

‘$_FILES[‘image’][‘name’]’

is treated as ‘$_FILES[’ as a string then image as invalid and so stopping the query running then ‘][’ as a string then name as invalid which would also stop the script running then ‘]’ as a string

if you change it to:

‘“.$_FILES[‘image’][‘name’].”’

(that’s ’ then " at the start and " then ’ at the end) then the quotes will match up properly.

Well I’ve changed the code around a bit, and it uploads the image, but it will not add the image name to the db. Don’t know why it doesn’t work.

<?php 
$allowedExts = array("gif", "jpeg", "jpg", "png");

$temp = explode(".", $_FILES["image"]["name"]);

$extension = end($temp);

$photo = $_FILES['image']['name'];

$con=mysqli_connect("localhost","ac","zzz","ac");
// Check connection

if (mysqli_connect_errno())

  {

  echo "Failed to connect to MySQL: " . mysqli_connect_error();  }
mysqli_query($con,"INSERT INTO image (image) VALUES ($photo)");

mysqli_close($con);
if ((($_FILES["image"]["type"] == "image/gif")

|| ($_FILES["image"]["type"] == "image/jpeg")

|| ($_FILES["image"]["type"] == "image/jpg")

|| ($_FILES["image"]["type"] == "image/pjpeg")

|| ($_FILES["image"]["type"] == "image/x-png")

|| ($_FILES["image"]["type"] == "image/png"))

&& ($_FILES["image"]["size"] < 200000)

&& in_array($extension, $allowedExts))

  {

  if ($_FILES["file"]["error"] > 0)

    {

    echo "Return Code: " . $_FILES["image"]["error"] . "<br>";

    }

  else

    {

    echo "Upload: " . $_FILES["image"]["name"] . "<br>";

    echo "Type: " . $_FILES["image"]["type"] . "<br>";

    echo "Size: " . ($_FILES["image"]["size"] / 1024) . " kB<br>";

    echo "Temp file: " . $_FILES["image"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["image"]["name"]))

      {

      echo $_FILES["image"]["name"] . " already exists. ";

      }

    else

      {

      move_uploaded_file($_FILES["image"]["tmp_name"],

      "upload/" . $_FILES["image"]["name"]);

      echo "Stored in: " . "upload/" . $_FILES["image"]["name"];
      }    }  }else  {  echo "Invalid file";  }

?>

Change the line



mysqli_query($con,"INSERT INTO image (image) VALUES ($photo)");

to



mysqli_query($con,"INSERT INTO image (image) VALUES ('". $photo . "')") or die(mysqli_error($con));

That worked! Thank you so so so very much!

You are welcome, remember that you need to quote variables in the sql query string :slight_smile: