Right, I have an upload form. When submit it should store all the input fields into the database phpmyadmin. Also it should create a folder for the attachments to save into in a folder on the local server.
If there are no errors in the form validation it does the above. What is actually happening is that the folder is getting created in the correct folder holding the attachment documents. But the details are not saving into the database as when I look on phpmyadmin there is nothing there.
The only time the database query runs is when the contains 6 digits. If it contains 11 numbers it doesnt run. But heres the weird thing if i put in 7 digits the query does run but it saves in the database as 6 digits and deleted the first digit.
HTML:
<form action="upload.php" method="post" enctype="multipart/form-data">
Select your policy type:<br/>
<select name="pol_type">
<option selected="selected" value="Private Car">Private Car</option>
<option value="Commercial Vehicle">Commercial Vehicle</option>
<option value="Motorcycle">Motorcycle</option>
</select><br/><br/>
Customer reference<br/>
<input type="text" name="reference"/><br/><br/>
First Name<br/>
<input type="text" name="first_name"/><br/><br/>
last Name<br/>
<input type="text" name="last_name"/><br/><br/>
Vehicle registration<br/>
<input type="text" name="registration"/><br/><br/>
Contact Number<br/>
<input type="text" name="number"/><br/><br/>
Email Address<br/>
<input type="text" name="email"/><br/><br/>
Upload Your documents <br/>
<input type="file" name="pictures[]" /><br/><br/>
<input type="file" name="pictures[]" /><br/><br/>
<input type="file" name="pictures[]" /><br/><br/>
<input type="submit" value="Send" />
</form>
Upload.php:
<?php
session_start();
$_SESSION['errors'] = array();
require_once("includes/db_connection.php");
require_once("includes/functions.php");
$errors = array();
//Variables from $_POST
$ref = $_POST['reference'];
$type = $_POST['pol_type'];
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$reg = $_POST['registration'];
$number = $_POST['number'];
$email = $_POST['email'];
//Validations:
//1) There needs to be at least one upload
$total_files = count(array_filter($_FILES['pictures']['name']));
if($total_files < 1)
{
$_SESSION['errors'][] = "No files have been uploaded!";
}
//2) Reference needs to be certain length
if(strlen($ref) < 7)
{
$_SESSION['errors'][] = "client reference cannot be empty, and must be 8 characters long";
}
//3) All Fields need to be filled
if($type == "" || $first_name == "" || $last_name == "" || $reg == "" || $number == "" || $email == ""){
$_SESSION['errors'][] = "All Fields must have a value!";
}
//3) Files size limit to 20MB
$total_size = 0;
foreach ($_FILES["pictures"]["size"] as $size)
{
$total_size = $total_size + $size;
}
if($total_size > 1e+7)
{
$_SESSION['errors'][] = "File size limit is 20MB, your files are too big!";
}
// If there are no errors in the validation then continue with script
if(empty($_SESSION['errors']))
{
$img_count = 0;
$img_name1 = "";
$img_name2 = "";
$img_name3 = "";
//Foreach files uplaoded place the file name into variable to go into table for later reference.
foreach ($_FILES["pictures"]["name"] as $key => $Name)
{
$img_count++;
if($img_count == 1)
{
$img_name1 = $_FILES["pictures"]["name"][$key];
}elseif($img_count == 2)
{
$img_name2 = $_FILES["pictures"]["name"][$key];
}else
{
$img_name3 = $_FILES["pictures"]["name"][$key];
}
}
$query = "INSERT INTO ";
$query .= "customers";
$query .= "(`reference`, `policy_type`, `first_name`, `last_name`, `registration`, `number`, `email`, `doc1`, `doc2`, `doc3`) ";
$query .= "VALUES ('{$ref}', '{$type}', '{$first_name}', '{$last_name}' , '{$reg}' , '{$number}' , '{$email}' , '{$img_name1}' , '{$img_name2}' , '{$img_name3}')";
$result = mysqli_query($connection, $query);
if(!$result){
die('Invalid query: ' . mysql_error());
}
$target_dir = "docs".DIRECTORY_SEPARATOR;
//IF/ELSE the folder is already created...
if(!file_exists($target_dir . $ref . DIRECTORY_SEPARATOR))
{//Create folder named 1 inside the customer ref folder.
mkdir($target_dir . $ref . DIRECTORY_SEPARATOR . "1" . DIRECTORY_SEPARATOR, 0775, true);
$count = 0;
}else
{//Create new folder inside customer ref folder
//count the amount of folders inside docs/$ref/
$find_folders = glob($target_dir . $ref . DIRECTORY_SEPARATOR . "*",GLOB_ONLYDIR);
$count = count($find_folders);
//create new folder inside $ref/ using count+1 to make the folder increase by 1
$new_folder = $count +1;
mkdir($target_dir . $ref . DIRECTORY_SEPARATOR . $new_folder . DIRECTORY_SEPARATOR, 0775, true);
}
//IF count exists then the $target_file changes to the new folder...
if($count > 0)
{
$target_file = $target_dir . $ref . DIRECTORY_SEPARATOR . $new_folder . DIRECTORY_SEPARATOR;
}else
{//else use first directory
$target_file = $target_dir . $ref . DIRECTORY_SEPARATOR . "1" . DIRECTORY_SEPARATOR;
}
//Loop through files and place them into $target_file...
foreach ($_FILES["pictures"]["name"] as $key => $Name)
{
$tmp_name = $_FILES["pictures"]["tmp_name"][$key];
$name = $_FILES["pictures"]["name"][$key];
move_uploaded_file($tmp_name, $target_file . "$name");
}
redirect_to('thanks.php');
}else//if there are errors in validation then redirect to index and show them
{
redirect_to('index.php');
}//END OF empty session
?>
The db_connection is correct that I include at the top of upload.php
This is my table on phpmyadmin:
Does any one know why the data is not going into my phpmyadmin database table?
EDIT:
When i print the query i get this:
INSERT INTO customers(reference
, policy_type
, first_name
, last_name
, registration
, number
, email
, doc1
, doc2
, doc3
) VALUES ('8888888', 'Private Car', 'Matthew', 'Smart' , 'Kp09 JVL' , '07707783196' , 'mattysmart' , 'logo.jpg' , '' , '')
and now when i try to do it on phpmyadmin without running it through php i get the following error: