[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>