The Form
with text and file input fields.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="legg_til_staff_funksjoner.php" method="post" enctype="multipart/form-data">
<input type="text" name="name">
<select name="position" id="">
<option value="bartender">Bartender</option>
<option value="bouncer">Bouncer</option>
<option value="entertainer">Entertainer</option>
<option value="greeter">Greeter</option>
<option value="manager">Manager</option>
</select>
<input type="text" name="ic_beskrivelse">
<input type="text" name="ooc_beskrivelse">
<input type="file" name="link_name">
<input type="submit">
</form>
</body>
</html>
The form function
where i gather the info submitted in form and it’s supposed to send it to database.
<?php
$target_dir = "staffImages/";
$target_file = $target_dir . basename($_FILES["link_name"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));
$name = $_POST['name'];
$position = $_POST['position'];
$ic_beskrivelse = $_POST['ic_beskrivelse'];
$ooc_beskrivelse = $_POST['ooc_beskrivelse'];
$link_name = ($_FILES['link_name']['name']);
include "funksjoner.php";
$db = kobleTilTest();
$sql = "INSERT INTO staff (name, position, ic_beskrivelse, ooc_beskrivelse, link_name)
VALUES ('$name', '$position','$ic_beskrivelse', '$ooc_beskrivelse', '$link_name' )";
if (isset($_POST["submit"])) {
$check = getimagesize($_FILES["link_name"]["tmp_name"]);
if ($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["link_name"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if (
$imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif"
) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["link_name"]["tmp_name"], $target_file)) {
echo "The file " . htmlspecialchars(basename($_FILES["link_name"]["name"])) . " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
echo "$target_file";
}
}
I tried to set up a form which takes staff input with text and image then post it to the database but can’t seem to make it work. it only upload image somtimes but the text isn’t stored in the database.
New contributor
Thor is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.