提问人:KALIL 提问时间:4/10/2022 更新时间:4/10/2022 访问量:1278
将图像从 html 表单输入上传到数据库 blob 列
Uploading an image from an html form input to database blob column
问:
我想将数据从 html 表单上传到数据库,这是我的 html 代码:
<form method="post" action="events.php" id="formhh" enctype="multipart/form-data">
<input type="text" name="Titre" required>
<input type="file" accept="image/jpeg" name="Image" required>
<textarea name="Description" form="formhh" required></textarea>
<div>
<input type="radio" name="style" value="1" required>
<input type="radio" name="style" value="2" required>
<input type="radio" name="style" value="3" required>
<input type="radio" name="style" value="4" required>
<input type="radio" name="style" value="5" required>
</div>
<input type="submit" class="btn btn-outline-primary me-5">
</form>
以及连接数据并将其发送到数据库的“events.php”(名为“isticg”):
<?php
$conn = new mysqli("localhost", "root", "", "isticg");
$Titre = $_POST['Titre'];
$Img = mysqli_real_escape_string($conn ,file_get_contents($_FILES['Image']['tmp_name']));
$Desc = $_POST['Description'];
$Style = $_POST['style'];
$stmt = $conn->prepare("insert into evenements(Titre, Image, Description,style) values(?,?,?,?)");
$stmt->bind_param("sbsi",$Titre,$Img,$Desc,$Style);
$stmt->execute();
$stmt->close();
$conn->close();
?>
除了图像之外,一切正常,这是我做测试后它在 phpMyadmin 中的样子
我是一个初学者,刚刚开始为这个项目学习php,所以如果你能提供你的答案的解释,请。
答:
-2赞
KALIL
4/10/2022
#1
修好了! 我将 php 代码更改为:
<?php
$dbh = new PDO("mysql:host=localhost;dbname=isticg", "root", "");
$Titre = $_POST['Titre'];
$Img = file_get_contents($_FILES['Image']['tmp_name']);
$Desc = $_POST['Description'];
$Style = $_POST['style'];
$stmt = $dbh->prepare("insert into evenements values('',?,?,?,?)");
$stmt->bindParam(1,$Titre);
$stmt->bindParam(2,$Img);
$stmt->bindParam(3,$Desc);
$stmt->bindParam(4,$Style);
$stmt->execute();
?>
评论
0赞
Zach Jensz
4/10/2022
可以解释代码
评论