SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Member
    Join Date
    Dec 2004
    Location
    (AFK) Back Later
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    PHP 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!
    Kevin

  2. #2
    SitePoint Mentor silver trophy
    Rubble's Avatar
    Join Date
    Dec 2005
    Location
    Cambridge, England
    Posts
    2,435
    Mentioned
    82 Post(s)
    Tagged
    3 Thread(s)
    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.
    Code:
    $photo = $_FILES['image']['name'];
    echo $photo;
    mysqli_query($con,"INSERT INTO image (image) VALUES ($photo)");

  3. #3
    SitePoint Member
    Join Date
    Dec 2004
    Location
    (AFK) Back Later
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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";  }

    ?>
    Kevin

  4. #4
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,869
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    '$_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.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  5. #5
    SitePoint Member
    Join Date
    Dec 2004
    Location
    (AFK) Back Later
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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";  }

    ?>
    Kevin

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Change the line
    Code:
    mysqli_query($con,"INSERT INTO image (image) VALUES ($photo)");
    to
    Code:
    mysqli_query($con,"INSERT INTO image (image) VALUES ('". $photo . "')") or die(mysqli_error($con));
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Member
    Join Date
    Dec 2004
    Location
    (AFK) Back Later
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked! Thank you so so so very much!
    Kevin

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    You are welcome, remember that you need to quote variables in the sql query string
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

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