Jump to content

Can't add image name to database


bbg5000

Recommended Posts

I'm trying to add an image name from an uploaded image to MySQL database. The code for the upload works, however, when I inserted the code to add the image name to SB, it now just shows a blank page and no image is uploaded or added to the database. Can someone have a look at my code and tell me what is wrong. Thanks.

<?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";  }

?>
Edited by bbg5000
Link to comment
Share on other sites

I've changed the code up a little bit. It now uploads the image, yet it still wont add it to the database. Any ideas?

<?php 

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["image"]["name"]);
$extension = end($temp);
$photo = $_FILES['image']['name'];
$con=mysqli_connect("localhost","acro","2313canadian","acropm");

// 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";
  }


?>
Edited by bbg5000
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.