hi i have followed a online tutorial to replace photo system on my site and all went well till i attempt to upload photo i get no file selected even if i have just gone on the page but after pressing submit i get a success message and the photo goes to the database and in th euploads folder but it doesnt show on the album page can someone check this out as ive been through tutorial twice and there are no comments about this problem my upload code is

<form enctype="multipart/form-data" method="post"> <?php
    if (isset($_POST['upload'])) {
       $name = $_POST['name'];
       $album_id = $_POST['album'];
       $file = $_FILES['file']['name'];
       $file_type = $_FILES['file']['type'];
       $file_size = $_FILES['file']['size'];
       $file_tmp = $_FILES['file']['tmp_name'];
           $random_name = rand();

       if (empty($name) or empty($file)) {
            echo "  please Fill in all fields ! <br/><br/>";
    } else {

            move_uploaded_file($file_tmp, 'uploads/'.$random_name.'.jpg');
            mysqli_query($mysqli,"INSERT INTO photos VALUES('', '$name', '$album_id', '$random_name.jpg')");

            echo "Photo Uploaded ! <br/><br/>";

}
}
?>
Select Photo</td><td><input type="file" accept="image/jpeg" name="file"/> <input type="submit" name="upload" value="Upload Photo"/>

and my album code is

<?php
    $query = mysqli_query($mysqli,"SELECT `id`, `name` FROM `albums`");
    while($run = mysqli_fetch_array($query)) {
        $album_id = $run['id'];
        $album_name = $run['name'];

        $query1 = mysqli_query($mysqli,"SELECT `url` FROM `photos` WHERE `album_id`='$album'");
        $run1 = mysqli_fetch_array($query1);
        $pic = $run1['url'];
   ?> <a href='view.php?id=<?php echo $album_id; ?>'> <div id='view_box'> <img src='uploads/<?php echo $pic; ?>'/> <br/> <b><?php echo $album_name ?></b> </div> </a> <?php

    }
?>

any help would be much appreicated ty all x

Recommended Answers

All 4 Replies

Member Avatar for diafol

Have you heard of punctuation? I had to take a few mental breaths while reading that first "story".

Try using NULL instead of '' if the first field in photos is the primary key.

You are also open to SQL injection by placing the input variables directly into the query without sanitizing nor using a prepared statement.

In addition, you are running loads of queries - no need - just run one JOINed query / statement. If you are retrieving every photo (as it seems you are from the code), then do this:

SELECT a.`name`, p.`url` FROM `photos` AS p INNER JOIN `albums` AS a ON p.`album_id`= a.`id`

Just one query and one loop.

Sorted it now i tried doing that with both queries and didnt do any difference. So i redone the tutoiral and its now working :)

Member Avatar for diafol

Care to share your code? It may help other people with a similar issue. Thanks.

yes sure ill post it in code snippet section because its several long files hun

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.