Recently I have tried to insert images into database via prepared statements. Sadly, in the tutorials there is not information how to include other informations than strings. I would like to make my prepared statements pass images into database Here is my code which does not work.
$connect = mysqli_connect($hostname, $username, $password, $databaseName);
$fname = mysqli_real_escape_string($connect, $_POST['yname']);
$lname = mysqli_real_escape_string($connect, $_POST['email']);
$filename = $_FILES['uploadfile']['name'];
$filetmpname = $_FILES['uploadfile']['tmp_name'];
$folder = 'imagesuploadedf/';
// edited and added below code. it will check if folder exists and create if not exists.....
$foldername = 'imagesuploadedf';
if ( ! is_dir($foldername)) {
mkdir($foldername);
}
// end of edited and added code
move_uploaded_file($filetmpname, $folder.$filename);
// $sql = "INSERT INTO `uploadedimage` (`imagename`) VALUES ('$filename')";
// connect to mysql database using mysqli
$sql = "INSERT INTO `tabela`(`name`, `email`, `imagename`) VALUES (?, ?, ?)";
$stmt = mysqli_stmt_init($connect);
if(!mysqli_stmt_prepare($stmt, $sql)){
echo "Error";
} else{
mysqli_stmt_bind_param($stmt, "sss", $fname, $lname, $filename);
mysqli_stmt_execute($stmt);
}
mysqli_close($connect);
}