I'm totally new using databases, so maybe my problem is something obvious. My variables are in danish, I hope you'll understand anyway. I have already searched in the forum, but because this is new to me, I find it very hard to understand.
A user should create a profile with a profile picture, but an error happens when I try to save it. My own defined error pops up because $billednavn == false
.
The code:
<?php
// 1. Gem modtagne formulardata i variabler:
$navn = $_POST['navn'];
$alder = $_POST['alder'];
$postnr = $_POST['postnr'];
$mail = $_POST['mail'];
$billede = $_FILES['profilbillede'];
$password = $_POST['password'];
// 2. Forbind til databasen:
$databaselink = mysqli_connect("localhost","mmddk3e12m8b18", "****", "mmddk3e12m8b18") or die ("Fejl: Kan ikke etablere forbindelse til database");
$billednavn = billedupload($billede);
// besked til brugeren om at der er sket en fejl
if($billednavn == false){
die("Der skete en fejl under upload af billedet");
}
// 3. Udform insert SQL streng med de modtagne data, og udfør SQL strengen på databasen vha mysqli_query:
$query = "INSERT INTO brugere (navn, alder, postnr, mail, password, profilbillede) VALUES ('$navn', '$alder', '$postnr', '$mail', '$password', '$billednavn')";
$result = mysqli_query($databaselink, $query) or die( "Forespørgslen kunne ikke udføres: " . mysqli_error($databaselink));
// 4. luk databasen:
mysqli_close($databaselink);
function billedupload($fil){
if($fil['type']=='image/jpeg' or $fil['type']=='image/png'){
$tmp_navn = $fil['tmp_name'];
$filnavn = $fil['name'];
$target = 'images/' . time() . $filnavn;
move_uploaded_file($tmp_navn,$target);
return $target;
}
else{
return false;
}
}
?>
If it returns false, then - by your definition, either the file type isn't image/jpeg or it isn't image/png, or the type isn't set, or the entire $_FILES isn't set.
In your HTML form make sure you have <form enctype="multipart/form-data" method="POST">
and try var_dump($_FILES);
in your PHP file to validate your POST data.
EDIT: In the condition where tmp_name is empty there are two possible situations i can think of:
<form enctype="multipart/form-data">
(more likely, please validate that)file_uploads
are set to On
The first option is more probable, Of course.