标签云

微信群

扫码加入我们

WeChat QR Code

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:<?phpinclude('functie.php');include('upload.php');?>Database:Maybe there is a way my file upload can be in my function.php.


Pretty obvious thing to ask but could you paste your html form code as well?

2019年04月22日56分56秒

added my form. Action.php goes to a file which contains the double action to upload.php and function.php

2019年04月22日56分56秒

And what's the action.php? I mean the content. There is no point to make it complicated, you could do it all inside one .php file (action.php for example).

2019年04月22日56分56秒

added my action.php, My function.php is working like a charm so I do not want to change anything on this. However adding the file upload would be great. But if the file upload can be in upload.php that would just be perfect for me.

2019年04月22日56分56秒

Did you find it? As I mentioned you don't define $cv and you try to call it, that's why you get errors and null value is inserted in db. There's also problem with your logic, you fill in details for person and you try to fill it with id that doesn't exist at the time of executing that query.

2019年04月22日56分56秒

This is not for long term use... I tried the code and it is uploading the file to the database. However the cv_id which has a relation with person_cv in my table still shows NULL and when I go to the detail-page it is not showing the file/path. I added a picture of my database above.

2019年04月22日56分56秒

That is solution to your problem, it uploads the file and correctly fills up the corresponding database fields which didn't work earlier. What are you trying to put in person_cv field? Look at your code and echo it, it's null.

2019年04月22日56分56秒

You haven't defined $cv anywhere in the code.

2019年04月22日56分56秒

I could be wrong... But is there a way to insert my file as a statement like the table address?

2019年04月22日56分56秒

You mean in the same file? Or you want to upload it to database instead of storing it on server? You can do both but saving files in db is not a good idea, hurts performance.

2019年04月22日56分56秒