I am busy with a small project where people can fill in a form which will save the values into a database. Now everything is working fine exept for file upload path. In my database I got three tables person, address and cv (with relations) see picture:
Now I want the path to be www.test.nl/directory/filename so I can make a button with the link so when you press the button it will download the file. How ever on my detail page as you can see it is showing the cv_id and not the path. My file upload is in a seperate file as is my person and address upload see below:
address and person upload:
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// CREATE A CONNECTION WITH THE DATABASE
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// ADDRESS APPEND - PREPARE SQL STATEMENT AND BIND PARAMS
// ADRES TOEVOEGEN - BEREID SQL STATEMENT EN BIND PARAMS
$stmt = $conn->prepare("INSERT INTO address (address_street, address_housenumber,
address_zipcode, address_city, address_state)
VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $straat, $huisnummer, $postcode, $stad, $provincie);
$straat = htmlspecialchars($_POST['straat']);
$huisnummer = htmlspecialchars($_POST['huisnummer']);
$postcode = htmlspecialchars($_POST['postcode']);
$stad = htmlspecialchars($_POST['stad']);
$provincie = htmlspecialchars($_POST['provincie']);
// EXECUTE STATEMENT
// STATEMENT UITVOEREN
$result = $stmt->execute();
if ($result === FALSE) {
die("Error: " . $stmt->error);
}
// CAPTURE LAST INSERTED address_id
// PAK DE LAATST INGEVOERDE address_id
$last_id = $conn->insert_id;
// PERSON APPEND - PREPARE SQL STATEMENT AND BIND PARAMS
// PERSOON TOEVOEGEN - BEREID SQL STATEMENT EN BIND PARAMS
$stmt = $conn->prepare("INSERT INTO person (person_firstname, person_lastname,
person_email, person_phonenumber,
person_cv, person_address)
VALUES (?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssi", $firstname, $lastname, $email, $telephone, $cv, $last_id);
$firstname = htmlspecialchars($_POST['firstname']);
$lastname = htmlspecialchars($_POST['lastname']);
$email = htmlspecialchars($_POST['email']);
$telephone = htmlspecialchars($_POST['telephone']);
// EXECUTE STATEMENT
// STATEMENT UITVOEREN
$result = $stmt->execute();
if ($result === TRUE) {
$URL="http://localhost:8080/Website/bedankt.php";
header ("Location: $URL");
} else {
echo "Error: " . $stmt->error;
}
// CLOSE CONNECTION AND STATEMENT
// SLUIT CONNECTIE EN STATEMENT
$stmt->close();
$conn->close();
?>
file upload.php
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// CREATE A CONNECTION WITH THE DATABASE
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit']))
{
$filetmp = $_FILES["cv"]["tmp_name"];
$filename = $_FILES["cv"]["name"];
$filetype = $_FILES["cv"]["type"];
$filepath = "files/".$filename;
move_uploaded_file($filetmp,$filepath);
$sql = "INSERT INTO cv (cv_name,cv_path,cv_type) VALUES ('$filename','$filepath','$filetype')";
$result = mysqli_query($conn, $sql);
}
$cv = $conn->insert_id;
?>
My detail page:
<?php
$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT person_firstname, person_lastname,
person_email, person_phonenumber, person_cv,
address_street,address_housenumber,
address_city,address_state,address_zipcode
FROM person
inner join address on address.address_id = person.person_address";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border=1>
<tr>
<th>Voornaam</th>
<th>Achternaam</th>
<th>Straat</th>
<th>Huisnummer</th>
<th>Postcode</th>
<th>Stad</th>
<th>Provincie</th>
<th>Email</th>
<th>Mobiel</th>
<th>CV</th>
</tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["person_firstname"] . "</td>";
echo "<td>" . $row["person_lastname"] . "</td>";
echo "<td>" . $row["address_street"] . "</td>";
echo "<td>" . $row["address_housenumber"] . "</td>";
echo "<td>" . $row["address_zipcode"] . "</td>";
echo "<td>" . $row["address_city"] . "</td>";
echo "<td>" . $row["address_state"] . "</td>";
echo "<td>" . $row["person_email"] . "</td>";
echo "<td>" . $row["person_phonenumber"] . "</td>";
echo "<td>" . $row["person_cv"] . "</td>";
echo "</tr>";
}
} else {
echo "Er is niks in het database gevonden";
}
$conn->close();
?>
I hope you guys can help me with this I am new to PHP so I am still learning this all. Note that everything is working well... exept the upload www.test.nl/directory/filename and saving this into my table cv_path... As showing the path (maybe as a button) in the detail page. If there is a way using almost the same statement as address to upload a file to cv than that would be great. But because this is working now (for so far) I keep my file upload and person and address upload seperated. And if it's possible to add a download button into my detail page under CV which will download the file that would be perfect.