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

brombeer
  • 8,716
  • 5
  • 21
  • 27
Am Salman
  • 9
  • 3
  • file_get_contents($_FILES['myimage']['name']) makes no sense. there is no such file. – Your Common Sense Dec 26 '22 at 15:09
  • You need to enable error reporting and see what errors you get. – Your Common Sense Dec 26 '22 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 Dec 26 '22 at 15:39

1 Answers1

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']));

Peter
  • 221
  • 1
  • 2
  • 12