gpt4 book ai didi

php - 如何在具有 1 个或多个关系的数据库中上传文件

转载 作者:行者123 更新时间:2023-11-29 11:41:14 24 4
gpt4 key购买 nike

我创建了一个表单,人们可以在其中上传个人信息和简历。现在我的数据库有三个表,分别称为人员、地址和简历。地址和简历都与人有关。当我填写表格并查看数据库时,人员>地址是正确的,但它根本不会将简历(路径)保存在数据库中。此外,cv_id 亲自显示为 NULL。谁能帮我解决这个问题吗?

我的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 = mysql_query($sql);
}
?>

这是我的 function.php,它仅上传人员和地址信息。我的表单有两个针对 function.php 和 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']);


// 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();
?>

我的表格:

<form method="post" action="action.php" enctype="multipart/form-data">
<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" name="cv">
</div>
<div class="col-sm-3">
<input type="submit" class="btn btn-default btn-form" name="submit" value="Solliciteer"/>
</div>
</form>

action.php:

<?php
include('functie.php');
include('upload.php');
?>

数据库: picture

也许有一种方法可以将我的文件上传到我的 function.php 中。

最佳答案

您的错误位于 upload.php 文件的第 24 行。

更改此:

$result = mysql_query($sql);

对此:

$result = mysqli_query($conn, $sql);

请记住 mysql 扩展已被弃用,并且在最近的 php 版本中已被删除。 7. 您不应该在代码中使用它。

编辑。你当前的数据库逻辑有缺陷,但我有什么资格评判你呢?如果您想按照现在的方式进行操作,则必须进行以下更改。

在 function.php 中修改 INSERT INTO 并从中删除 $cv。

$stmt = $conn->prepare("INSERT INTO person (person_firstname, person_lastname, person_email, person_phonenumber, person_address) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("ssssi", $firstname, $lastname, $email, $telephone, $last_id);

顺便说一句,在你的原始代码中它是 sssssi,这也是一个错误,因为你的 id 可能是一个整数,就像地址 id 一样。

在upload.php中关闭<?php之前标签添加以下行:

$last_id = $conn->insert_id;

现在更新表“person”,将该 ID 放入 person_cv 字段中。

编辑2。代码格式化一如既往地失败...

编辑3。我以为你会稍微移动一下并自己尝试一下。这是另一种解决方案,甚至更简单。

查看你的文件。在action.php中更改

include('function.php');
include('upload.php');

include('upload.php');  
include('functie.php');

现在 upload.php 首先被处理,这意味着您可以在 upload.php 文件的末尾添加 $cv = $conn->insert_id; (就在 php 结束标记之前)。

现在您在 function.php 中使用的变量 $cv 存在,因此您可以使用它,并且您的原始插入查询将在不将 NULL 值插入数据库的情况下工作:

$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);

关于php - 如何在具有 1 个或多个关系的数据库中上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35750018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com