我创建了一个将信息上传到数据库的表单。现在我处于人们可以上传PDF,DOC或DOCX文件的地步。但是,如何使用我的代码执行此操作?
我的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);
}
// ADDRESS APPEND - PREPARE SQL STATEMENT AND 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
$result = $stmt->execute();
if ($result === FALSE) {
die("Error: " . $stmt->error);
}
// CAPTURE LAST INSERTED address_id
$last_id = $conn->insert_id;
// PERSON APPEND - PREPARE SQL STATEMENT AND 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]);
$cv = htmlspecialchars($_POST[cv]);
// EXECUTE STATEMENT
$result = $stmt->execute();
if ($result === TRUE) {
$URL="http://localhost:8080/Website/bedankt.php";
header ("Location: $URL");
} else {
echo "Error: " . $stmt->error;
}
$stmt->close();
$conn->close();
?>
字段person_CV仅用于存储文件附件PDF,DOC和DOCX。在我的数据库中,我使用了varchar(45)类型,因为我想将文件上传到我服务器上的目录。但如果您有任何建议,我建议我使用那个。
我的表格
<form method="post" action="functie.php">
<div class="col-sm-3">
<input name="firstname" id="name" type="text" class="form-control" placeholder="Voornaam" required>
</div>
<div class="col-sm-3">
<input name="lastname" id="name" type="text" class="form-control" placeholder="Achternaam" required>
</div>
<div class="col-sm-3">
<input name="straat" id="name" type="text" class="form-control" placeholder="Straat" required>
</div>
<div class="col-sm-3">
<input name="huisnummer" id="name" type="text" class="form-control" placeholder="Huisnummer" required>
</div>
<div class="col-sm-3">
<input name="postcode" id="name" type="text" class="form-control" placeholder="Postcode" required>
</div>
<div class="col-sm-3">
<input name="stad" id="name" type="text" class="form-control" placeholder="Stad" required>
</div>
<div class="col-sm-3">
<select name="provincie" id="name" type="text" class="form-control" placeholder="Provincie" required>
<option value="Drenthe">Drenthe</option>
<option value="Flevoland">Flevoland</option>
<option value="Friesland">Friesland</option>
<option value="Gelderland">Gelderland</option>
<option value="Groningen">Groningen</option>
<option value="Limburg">Limburg</option>
<option value="Noord-Brabant">Noord-Brabant</option>
<option value="Noord-Holland">Noord-Holland</option>
<option value="Overijssel">Overijssel</option>
<option value="Utrecht">Utrecht</option>
<option value="Zeeland">Zeeland</option>
<option value="Zuid-Holland">Zuid-Holland</option>
</select>
</div>
<div class="col-sm-3">
<input name="telephone" id="telephone" class="form-control" type="tel" placeholder="Telefoonnummer">
</div>
<div class="col-sm-3">
<input name="email" id="email" class="form-control" type="email" placeholder="Email" required>
</div>
<div class="col-sm-3">
<input name="cv" id="cv" class="form-control" type="file" placeholder="CV">
</div>
<div class="col-sm-3">
<input type="submit" class="btn btn-default btn-form" name="submit" value="Solliciteer"/>
</div>
<input type="hidden" value="Nieuwe sollicitatie slimme-meters-maken.nl" name="subject" id="subject">
</form>