使用php更新数据库中的图像名称



[用解决方案编辑]Noob错误,它只缺少形式的enctype="multipart/form-data"

我不知道为什么我的代码突然停止工作。我用php和mySQL创建了一个CRUD应用程序。现在,当我尝试更新数据库中的图像名称时,没有得到任何结果。我很确定我使用的代码是正确的,但也许我遗漏了一些

DB表有一个名为"image"的列,我在其中存储图像名称及其扩展名(例如徽标.jpg(

这是update.php,它让我可以更改一些值。如果我尝试每隔一个字段更新一次,我不会出错,一切都正常,但我无法更改图像。我错过了什么?我对php真的很陌生,我不知道是不是不小心写错了什么,提前谢谢!(如果需要更多代码,请告诉我(

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

您的表单无法发布图像,因为您没有设置enctype属性,默认情况下HTML表单以纯文本形式发布,并且不包括任何二进制数据,如图像上传。要更正此问题,需要将enctype设置为multipart/form-data

例如:

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

请参阅:什么是enctype=';多部分/表单数据';意思是了解更多信息。

最新更新