将图像从 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();
?>
除图片外一切正常,这里是how it looks in phpmyadmin after i do a test
我是初学者,刚开始学习这个项目php,所以如果你能提供答案的解释。
已修复!
我将 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();
?>
我想将数据从 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();
?>
除图片外一切正常,这里是how it looks in phpmyadmin after i do a test
我是初学者,刚开始学习这个项目php,所以如果你能提供答案的解释。
已修复! 我将 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();
?>