如何在具有 1 个或多个关系的数据库中上传文件 [英] How do I upload a file within a database with 1 or more relations

查看:36
本文介绍了如何在具有 1 个或多个关系的数据库中上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我的upload.php(文件)

connect_error){die("连接失败:" . $conn->connect_error);}if(isset($_POST['提交'])){$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

connect_error){die("连接失败:" . $conn->connect_error);}//地址附加 - 准备 SQL 语句和绑定参数$stmt = $conn->prepare("INSERT INTO address (address_street, address_housenumber,address_zipcode, address_city, address_state)值 (?, ?, ?, ?, ?)");$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']);//执行语句$result = $stmt->execute();如果($result === FALSE){死(错误:".$stmt->错误);}//CAPTURE LAST INSERTED address_id$last_id = $conn->insert_id;//PERSON APPEND - 准备 SQL 语句和绑定参数$stmt = $conn->prepare("INSERT INTO person (person_firstname, person_lastname,person_email, person_phonenumber,person_cv, person_address)值 (?, ?, ?, ?, ?, ?)");$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']);//执行语句$result = $stmt->execute();如果($result === TRUE){$URL="http://localhost:8080/Website/bedankt.php";标头(位置:$URL");} 别的 {回声错误:".$stmt->错误;}$stmt->close();$conn->close();?>

我的表格:

<div class="col-sm-3"><input name="firstname" id="name" type="text" class="form-control" placeholder="Voornaam" required>

<div class="col-sm-3"><input name="lastname" id="name" type="text" class="form-control" placeholder="Achternaam" required>

<div class="col-sm-3"><input name="straat" id="name" type="text" class="form-control" placeholder="Straat" required>

<div class="col-sm-3"><input name="huisnummer" id="name" type="text" class="form-control" placeholder="Huisnummer" required>

<div class="col-sm-3"><input name="postcode" id="name" type="text" class="form-control" placeholder="Postcode" required>

<div class="col-sm-3"><input name="stad" id="name" type="text" class="form-control" placeholder="Stad" required>

<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="弗里斯兰">弗里斯兰</option><option value="海尔德兰">海尔德兰</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="乌得勒支">乌得勒支</option><option value="Zeeland">Zeeland</option><option value="Zuid-Holland">Zuid-Holland</option></选择>

<div class="col-sm-3"><input name="telephone" id="telephone" class="form-control" type="tel" placeholder="Telefoonnummer">

<div class="col-sm-3"><input name="email" id="email" class="form-control" type="email" placeholder="Email" required>

<div class="col-sm-3"><input name="cv" id="cv" class="form-control" type="file" placeholder="CV" name="cv">

<div class="col-sm-3"><input type="submit" class="btn btn-default btn-form" name="submit" value="Solliciteer"/>

</表单>

action.php:

数据库:

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

解决方案

您的错误出现在第 24 行的 upload.php 文件中.

改变这个:

$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');包括('上传.php');

include('upload.php');包括('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);

I created a form where people can upload their personal information and resume. Now my database has three tables called person, address and cv. address and cv both has a relation with person. When I fill in the form and look in my database the person > address is correct however it doesnt save the resume(path) in the database at all. Also the cv_id is shown in person as a NULL. Can anyone help me fix this problem?

My upload.php (file)

<?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);
}
?>

Here is my function.php which only uploads person and address information. My form has two actions to function.php and 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();
?>

My form:

<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');
?>

Database:

Maybe there is a way my file upload can be in my function.php.

解决方案

Your error is in upload.php file on line 24.

Change this:

$result = mysql_query($sql);

to this:

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

Keep in mind mysql extension is deprecated and it was removed in recent php ver. 7. You shouldn't be using it in your code.

EDIT. Your current db logic is flawed but who am I to judge you? If you want to do it as you have it now you have to do the following changes.

In function.php modify the INSERT INTO and remove $cv from it.

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

btw in your original code it's sssssi, this is also an error as your id is probably an integer just like the address id.

In upload.php before you close the <?php tag add following line:

$last_id = $conn->insert_id;

Now update the table "person" putting that id into person_cv field.

EDIT2. Code formatting fails as always...

EDIT3. I thought you will move it around a little bit and try it yourself. Here is another solution, even simpler.

Look at your files. In action.php change

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

to

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

Now upload.php gets processed first which means that at the end of the upload.php file you can add $cv = $conn->insert_id; (right before php closing tag).

Now the variable $cv which you use in function.php exists therefore you can use it and your original insert query will work without inserting NULL values into your db:

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

这篇关于如何在具有 1 个或多个关系的数据库中上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆