Home > database >  Uploading an image from an html form input to database blob column
Uploading an image from an html form input to database blob column

Time:04-13

I want to upload data from a html form to a database, here's my html code:

<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" >
</form>

and the "events.php" that connects and sends data to the database (named "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();

?>

everything works fine except of the image, here's how it looks in phpmyadmin after i do a test

I am a beginner and just started learning php for this project, so please if you could provide an explanation with your answers.

CodePudding user response:

Fixed it! I changed the php code to this:

<?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();

?>
  • Related