I am trying to upload a file path into MySQL database. I have this html form:
<form action="" method="post" name="add_maps" enctype="multipart/form-data">
<table border="1" cellpadding="2" cellspacing="1" align="center" dir="rtl">
<tr>
<th>
name of map</th>
<td>
<input type="text" name="name_of_map"/>
</td>
<th>
select map</th>
<td>
<input type="file" name="file" id="file"/>
</td>
</tr>
<tr>
<input type="submit" name="submit_map" value="upload"/>
</tr>
</table>
</form>
And in the same page, I have the PHP code to upload:
<?php
require_once('../include/inner_global.php');
$hostdb = "localhost";
$namedb = "architect";
$userdb = "root";
$passdb = "root";
$id = $_REQUEST['id'];
$name='';
$conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn->exec("SET CHARACTER SET utf8mb4");
$path = "../uploads/".$_FILES['file']['name'];
if(isset($_POST['submit_map'])){
try{
$name = isset($_POST['name']) ? $_POST['name'] : '';
$ext = pathinfo($path, PATHINFO_EXTENSION);
if(move_uploaded_file($_FILES["file"]["tmp_name"], $path)){
$path = "./uploads/".$path;
$sql = "INSERT INTO maps(name_of_maps, projects_id, map) VALUES (:name, :id, :file)";
$stmt = $conn->prepare($sql);
$stmt->bindValue(":name", $name);
$stmt->bindValue(":id", $id);
$stmt->bindValue(":file", $path);
$count = $stmt->execute();
}
}
catch(PDOException $e) {
echo $e->getMessage();
header("location: insert_map_false.php?id=".$id);
}
}
?>
When I go to the page where this html form is, I get directly this error in the header:
P.S
Files are uploaded correctly to their path, when i click on upload, but not added to database. EDIT
This is my table datatypes