0

[EDITED WITH SOLUTION] Noob error, it was only missing the enctype="multipart/form-data" in the form

I don't know why my code suddenly stop to work. I created a CRUD app with php and mySQL. Now when I try to update the image name in the database I get no result. I'm pretty sure the code I'm using is correct but maybe I'm missing something

The DB table has a column named 'image' where I store the image name with its extension (ex. logo.jpg)

This is the update.php that let me change some values. If I try to update every other field I get no error and everything works, but I'm not able to change the image. What am I missing? I'm really new with php and I don't know if I accidentally miswrote something, thanks in advance! (Let me know if more code is needed)

<?php
include 'functions.php';

$msg = '';

if(isset($_GET['id'])){
    if(isset($_POST['update'])){

        // Posted Values
        $title=$_POST['title'];
        $category=$_POST['category'];
        $preview=$_POST['preview'];
        $main=$_POST['main'];
        $created = $_POST['created'];

        $imageName=$_FILES['immagine']['name'];

        // Query for Insertion
        $data = [
            'title' => $title,
            'category' => $category,
            'preview' => $preview,
            'main' => $main,
            'id' => $_GET['id'],
            'created' => $created,
            'immagine' => $imageName,
        ];

        $sql="UPDATE post SET title=:title, category=:category, preview=:preview, main=:main, created=:created, image=:immagine WHERE id=:id";

        //Prepare Query for Execution
        $stmt = $pdo->prepare($sql);

        // Query Execution
        $stmt->execute($data);

    

        header('Location: preview.php?id=' . $_GET['id']);
  }

    $stmt = $pdo->prepare('SELECT * FROM post WHERE id = ?');
    $stmt->execute([$_GET['id']]);
    $contact = $stmt->fetch(PDO::FETCH_ASSOC);

}
?>

<!DOCTYPE html>

<html>
    <body>
        <div class="container_create">
            <div class="topnav">
                <a href="preview.php?id=<?php echo $_GET['id']; ?>"><img src="IMG/Back_icon.svg" alt="menu" id="back_icon"></a>
                <img src="IMG/Logo_orizontale.svg" alt="menu" id="hyperink_logo">
            </div>

            <p id="titolo_create">Impostazione Pagina</p>

            <div class="box_create">
                <form action="update.php?id=<?php echo $_GET['id']; ?>" method="post" id="form_create">

                    <label class="labels_create">Data</label>
                    <input type="datetime-local" name="created" class="inputs_create" id="myDatetimeField" value="<?=$contact['created']?>">

                    <label class="labels_create">Titolo del Articolo</label>
                    <input type="text" name="title" class="inputs_create" id="title" value="<?=$contact['title']?>">

                    <label class="labels_create">Categoria</label>
                    <input type="text" name="category" class="inputs_create" id="cat" value="<?=$contact['category']?>">

                    <label class="labels_create">Descrizione</label>
                    <textarea type="text" name="preview" id="preview" class="inputs_create" row="5" col="50" onkeyup ="limite_caratteri()"><?=$contact['preview']?></textarea>


                    <label class="labels_create">Contenuto del Articolo</label>
                    <div class="text_editor">
                        <textarea id="main" name="main" rows="5" cols="50"><?=$contact['main']?></textarea>
                    </div>

                    <input type="file" id="immagine" name="immagine" class="inputs_create_file" accept="image/*"><span><?php echo $contact['image']?></span>

                    <input type="submit" name="update" value="Salva Modifiche" class="btn_create">
                </form>
            </div>
  • 2
    It was working before but now it's not... so, what did you change? – ADyson Jun 05 '21 at 12:55
  • I really don't know, because I made some test without the image field and when I tried to re-write the code it didn't work. I'm really confused @ADyson – Arianna parisi Jun 05 '21 at 12:58
  • Not the solution to your issue, but note that `header` does not terminate the script, you should add a `die()` after the header is set to prevent the rest of the script from executing. – Geoffrey Jun 05 '21 at 12:58
  • Just noticed the issue, your `form` doesn't have the attribute `enctype="multipart/form-data"`, the file will not be posted without this. See: https://stackoverflow.com/questions/4526273/what-does-enctype-multipart-form-data-mean – Geoffrey Jun 05 '21 at 12:59
  • 1
    AH! Sorry, I'm working only on the backend of this code and I totally forgot to check if the enctype was there -.-' Thanks a lot @Geoffrey I would have probably spent a day after this – Arianna parisi Jun 05 '21 at 13:03

1 Answers1

0

Your form is failing to post the image because you have not set the enctype attribute, by default HTML forms post as plain text and will not include any binary data such as an image upload. To correct this you need to set the enctype to multipart/form-data.

For example:

<form action="update.php?id=<?php echo $_GET['id']; ?>" method="post" id="form_create" enctype="multipart/form-data">

See: What does enctype='multipart/form-data' mean? for more information.

Geoffrey
  • 10,843
  • 3
  • 33
  • 46