Question

So i have an image gallery ATM and i`m trying to add the function to it where the admin gets to upload the pictures adding his own cattegorie to it. But the pictures open by cattegory.

My question is. How do i get the images category automaticly?

<a href="categorie.php?categorie=Auto`s"><img src="img/autos.png" alt="autos" class ="cat"/></a>
            <a href="categorie.php?categorie=Meubels"><img src="img/Meubels.png" alt="meubels" class ="cat"/></a>
            <a href="categorie.php?categorie=Bedrijven"><img src="img/reno.png" alt="renovatie" class ="cat"/></a>
            <a href="categorie.php?categorie=Zadels"><img src="img/zadels.png" alt="zadels" class ="cat"/></a>
            <a href="categorie.php?categorie=Boten"><img src="img/boten.png" alt="boten" class ="cat"/></a>
            <a href="categorie.php?categorie=Overig"><img src="img/overig.png" alt="overig" class ="cat"/></a>

This is my code atm

Was it helpful?

Solution

Including everything for uploading a file is going to take a lot of time. This could give you a start.

<?php
$link = mysqli_connect("myhost","myuser","mypassw","mybd") or die("Error " . mysqli_error($link));
if($_POST['submit']) {
    $category = $_POST['category'];

    $query_insert = "INSERT INTO image_gallery (id, category, image_location) VALUES ('', '$category', 'uploads/".$_FILES['file']['name']."');
    $result_insert = mysqli_query($link, $query_insert);

   move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
}



?>

<form action='' method='POST' enctype='multipart/form-data'>
<?php
    $query = "SELECT * FROM image_gallery";
    $result = mysqli_query($link, $query);
    while($row = $mysqli_fetch_assoc($link, $result)){
        echo "<select name = 'category'>
                  <option value='".$row['category']."'>".$row['category']."</option>
              </select>";
    }
    echo "Category: <input type='text' name='category'>";
?>
Image <input type='file' name='file'/>
<input type='submit' name='submit'>
</form>

Here I have assumed that you have a database table named 'image_gallery' in your database and you have a folder called 'uploads'. Go through the following link, it will give you some idea about file uploading.

http://www.w3schools.com/php/php_file_upload.asp

All the best.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top