0

I'm building a photography website where I have one page that displays the pictures (bootstrapped and all) and then another page so I can upload new photos (consists of an html page with form and php that runs download code). In this php page I want to make an SQL addition to my column name in database photos. I want to add the file name into the SQL database name so I can later access it in my page displaying the photos and I can get the file name so I can for loop it there for all the pictures in the directory. The issue I'm having is adding the target file into the name and then getting it. Here's my code for adding the filename:

        $link = mysql_connect('meadowebcom.ipagemysql.com', 'lphotos', 'gut2*EMI_12'); 
        if (!$link) { 
            die('Could not connect: ' . mysql_error()); 
        } 
        echo 'Connected successfully'; 
        mysql_select_db(photos); 
        $sql = 'INSERT INTO `name` (`target_file`) VALUES ('.basename( $_FILES["fileToUpload"]["name"]).');';
        $retval = mysql_query( $sql, $conn );
        if(! $retval ) {
           die('Could not enter data: ' . mysql_error());
        }

        echo "Entered data successfully\n";
        mysql_close($conn);

I have it display that I am connected but can't add anything. I haven't even started on getting the file in the other file. I don't usually do PHP outside of contact and registration forms so any help or any points in the right direction would help.

  • In your SQL string, enclose your target_file field value in `''` – Karlo Kokkak Mar 31 '18 at 04:47
  • Also for the argument you passed in mysql_select_db(...); – Karlo Kokkak Mar 31 '18 at 04:49
  • If you `var_dump($retval)` what do you get? And is the table named `name` ? – Omar Tanti Mar 31 '18 at 04:50
  • Use $link variable all the time instead instead of $conn – Karlo Kokkak Mar 31 '18 at 04:50
  • 1
    Also `mysql_* functions are deprecated in PHP 5.5.0, and it is removed in PHP 7.0.0.` - You may wanna use mysqli_* next time or PDO. – Karlo Kokkak Mar 31 '18 at 04:54
  • I never use the "real" filenames, I guess if it's just you doing the uploads it will be ok. Just watch out for things like ` ` space, capitalization mistakes like `.JPG` instead of `.jpg`. What I like to do is hash the file using `sha1` then I save the hash, and the original name. I display the original name, but I use the hash as the actual filename on disk. This has the added benefit of telling if a file already exists as you can compare the hashes. – ArtisticPhoenix Mar 31 '18 at 05:28

0 Answers0