Issue
Here is my form
It gets the url of the image, and save it in the database. But it can show only files saved in folder "uploads", so I want to allow user to add file from any place of the PC. Help please Maybe I can do smth in the store.php?
Create.php
<form style="width: 25em; text-align: center; align-items: center;" action="store.php" method="POST">
<div class="mb-2 form-group">
<label for="exampleInputEmail1" class="mb-2">Name</label>
<input type="text" name="name" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Name">
</div>
<div class="mb-2 form-group">
<label for="exampleInputPassword1" class="mb-2">Surname</label>
<input type="text" name="surname" class="form-control" id="exampleInputPassword1" placeholder="Surname">
</div>
<div class="mb-2 form-group">
<label for="inputPatronym" class="mb-2">Patronym:</label>
<input type="text" name="patronym" class="form-control"placeholder="Patronym">
</div>
<div class="mb-2 form-group">
<label for="inputPatronym" class="mb-2">Biography:</label>
<input type="text" name="description" class="form-control"placeholder="Biography">
</div>
<div class="mb-3">
<label for="formFile" class="form-label">Image</label>
<input class="form-control" name="userfile" type="file" id="formFile" accept=".img,.png,.jpeg,.jpg,.bmp">
</div>
<div class="mb-4 form-group">
<button type="submit" class="btn btn-success" >Save</button>
</div>
<a class="btn btn-primary btn-sm ml-3" href="/postati.php" role="button">Return</a>
</form>
</body>
</html>
Store .php
<?php
require_once __DIR__ . "/database/pdo.php";
$Name = $_POST['name'];
$Surname = $_POST['surname'];
$Patronym = $_POST['patronym'];
$Description = $_POST['description'];
$userfile = $_POST['userfile'];
$sql = "INSERT INTO `peoples` (`Name`, `Surname`, `Patronym`, `Description`, `userfile`) VALUES (:name, :surname, :patronym, :description, :userfile)";
$params = [
"name" => $Name,
"surname" => $Surname,
"patronym" => $Patronym,
"description" => $Description,
"userfile" => $userfile,
];
$prepare = $pdo->prepare($sql);
$prepare->execute($params);
$pdo->lastInsertId();
$lastInsertId=$pdo->lastInsertId();
header("Location: /postati.php")
?>
View
<div class="image text-center">
<img src="uploads/<?=$people['userfile']?>" width="250px" height="350px" class="center">
</div>
I tried to move the file with move_uploaded_file, but then the other variables are not saving.
Solution
You are missing the enctype="multipart/form-data"
declaration in your form. You need this if you want a file upload form.
<form enctype="multipart/form-data" action="store.php" method="POST">
Answered By - delboy1978uk
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.