-1

1 month, 5 hours daily I try to fix my problem. Asking here and there but so far seems unable to find exact solution.

<form action="connectionfile.php" method="POST" enctype="multipart/form-data">
Please Insert Image
<input type="file" name="myimage" id="myimage" accept="image/*"></input>
Enter your First Name
<input name="fname" id="fname" type="text">
</input> <input type="submit" name="upload">
</form>

Here is how I am i trying to do it using Php (connectionfile.php)

<?php
$host = "localhost";
$dbUserName = "root";
$password = "";
$dbname = "mydb";
// Variables coming from html form
$img     = file_get_contents($_FILES['myimage']['name']);
$fname   = $_POST['fname'];
// Error Check if Any
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Connection building
$conn = mysqli_connect( $host, $dbUserName, $password, $dbname );
// inserting incoming form data into Localhost MySql Database
$sql = $conn->prepare("INSERT INTO mytable ( image_soldier , fname_soldier ) VALUES ( ? , ? )");
// Using 'b' for BLOB type parameters and 's' for string type parameters and Binding
$sql -> bind_param("bs", $img, $fname); 
// Running the Sql statemnt
$sql -> execute();
?>

When i submit the form after entering name and selecting image from any directory on computer, image is not going into my database and showing in database although the name is successfully posting there.

This image shows that the name is posting in there in my Localhost but image is not posting. That shows my connection is successful to the database

This image shows that the name is posting in there in my Localhost but image is not posting. That shows my connection is successful to the database.

Any kind of help is appreciated.

3
  • file_get_contents($_FILES['myimage']['name']) makes no sense. there is no such file. Commented Dec 26, 2022 at 15:09
  • You need to enable error reporting and see what errors you get. Commented Dec 26, 2022 at 15:12
  • @YourCommonSense Sir can I have some minutes from your precious time please? These platform admin won't let me solve my problem I guess. I have been stuck for a long time and until today I haven't find a solution. Can you please help what's wrong and what to replace in my code?
    – Am Salman
    Commented Dec 26, 2022 at 15:39

1 Answer 1

0

Tested, it works like this - check if this works for you:

$img = file_get_contents($_FILES['myimage']['tmp_name']);
$fname   = $_POST['fname'];
// Error Check if Any
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
// Connection building
$conn = mysqli_connect( $host, $dbUserName, $password, $dbname );
var_dump($conn);
// inserting incoming form data into Localhost MySql Database
$sql = $conn->prepare("INSERT INTO mytable ( image_soldier , fname_soldier ) VALUES ( ? , ? )");
// Using 'b' for BLOB type parameters and 's' for string type parameters and Binding
$sql -> bind_param("ss", $img, $fname); 
// Running the Sql statemnt
$sql -> execute();

If you want to get content from temp file

Instead of $img = file_get_contents($_FILES['myimage']['name']);

try using $img = file_get_contents($_FILES['myimage']['tmp_name']);

if you want to get name of the file, then just use

$img = $_FILES['myimage']['name'];

bin 2 hex

$img = bin2hex(file_get_contents($_FILES['myimage']['tmp_name']));

7
  • Thanks for the response. Still its not posting image into my Localhost. I have used this $img = file_get_contents($_FILES['myimage']['tmp_name']) It's only posting name into my database but not the image I uploaded through form. Any comments?
    – Am Salman
    Commented Dec 26, 2022 at 13:31
  • Show us the structure of image_soldier row - is it a blob?
    – Peter
    Commented Dec 26, 2022 at 13:34
  • Yes it is longblob type.
    – Am Salman
    Commented Dec 26, 2022 at 13:43
  • then does this help? $img = bin2hex($img);
    – Peter
    Commented Dec 26, 2022 at 13:43
  • i have used $img = bin2hex(file_get_contents($_FILES['myimage']['tmp_name'])) But it's posting name and not image into Localhost server.
    – Am Salman
    Commented Dec 26, 2022 at 13:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.