jeudi 16 juillet 2020

PHP several checkboxes that upload an image

A checkbox is linked to the upload of the image chosen and moved to a document, only there are several checkboxes which also link to documents. The problem, several checboxes are checked but the download request does not work.

PHP

<?php
  // Create database connection
  $db = mysqli_connect("localhost", "root", "", "photos");
 // Initialize message variable
 $msg = "";

// If upload button is clicked ...
if (isset($_POST['upload'])) {
    // Get image name
    $image = $_FILES['image']['name'];
    //GET image nom
    $image_nom = mysqli_real_escape_string($db, $_POST['image_nom']);
    // Get text
    $image_text = mysqli_real_escape_string($db, $_POST['image_text']);
    // image file directory
    if (isset($_POST['combat'])) {
        $target = "combat/images/".basename($image);
    
        if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
            $msg = "Image uploaded successfully";
        }else{
            $msg = "Failed to upload image";
        }
        $sql = "INSERT INTO images (image, image_nom, image_text) VALUES ('$image', '$image_nom', '$image_text')";
    // execute query
    mysqli_query($db, $sql);
    }
    
    if (isset($_POST['personnage'])) {
        $targetp = "search/personnages/".basename($image);
    
        if (move_uploaded_file($_FILES['image']['tmp_name'], $targetp)) {
            $msg = "Image uploaded successfully";
        }else{
            $msg = "Failed to upload image";
        }
        $sql = "INSERT INTO personnage (image_personnage, image_nom_personnage, image_text_personnage) VALUES ('$image', '$image_nom', '$image_text')";
    // execute query
    mysqli_query($db, $sql);
    }
    }

HTML

    <div id="content">
<form method="POST" enctype="multipart/form-data">
    <input type="hidden" name="size" value="1000000">
    <div>
    <input type="file" name="image">
    </div>
    <div>
        <textarea 
            id="text" 
            cols="40" 
            rows="4" 
            name="image_nom" 
            placeholder="Nom"></textarea>
    </div>
    <div>
        <textarea 
            id="text" 
            cols="40" 
            rows="4" 
            name="image_text" 
            placeholder="Fiche"></textarea>
    </div>
    <div>
        <button type="submit" name="upload">OK</button>
    </div>
    <div>
    <input type="checkbox" id="combat" name="combat">
    <label for="combat">Combat</label></br>

    <input type="checkbox" id="personnage" name="personnage">
    <label for="personnage">Personnage</label>

When submit is triggered an image with a name and text is inserted into a database then the image with the name and uploaded to a file. But when several checkboxes are checked then the image with the name is downloaded only in the first folder. I would just like the checkboxes to download them in the requested files




Aucun commentaire:

Enregistrer un commentaire