0

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:

enter image description here

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:

1264 - Out of range value for column 'number' at row 1

Community
  • 1
  • 1
matthew smart
  • 95
  • 1
  • 1
  • 11
  • Try to print the `$query` then execute it in phpmyadmin to see if there is any error that by something PHP can't catch. – Fenistil Jan 22 '15 at 09:52
  • `runs is when the contains 6 digits.` what do the digits refer to? – RST Jan 22 '15 at 10:03
  • the number that is input into the contact number input field. If they type a 6-7 digit number it works, but if they type in an 11 digit number(which is a phone number length) it doesnt run the query – matthew smart Jan 22 '15 at 10:05
  • @Fenistil please check my edit for the query info – matthew smart Jan 22 '15 at 10:06
  • When did this wall of code stop working? Or did you expect it to work first time? Code should be built incrementally,checking results at each increment. – david strachan Jan 22 '15 at 10:07
  • it was working fine yesterday, and i must be honest. I'm unsure what i have done for it to stop working. Its nothing to do with my front end validation which I did yesterday. – matthew smart Jan 22 '15 at 10:08
  • 1
    A contact/phone number is *not an integer*, it's a *numeric string*. You don't do math with it, so don't store it as a number. – DCoder Jan 22 '15 at 10:09
  • is this why it is not working? – matthew smart Jan 22 '15 at 10:10
  • Not exactly, it doesn't work because that "number" is too large for an `int` column ... http://dev.mysql.com/doc/refman/5.0/en/integer-types.html . Storing it as a number is bad design even if it did fit in an `int` column. – DCoder Jan 22 '15 at 10:13
  • ok, thanks for that @DCoder i have changed the field to Varchar(11) and it now works. :) – matthew smart Jan 22 '15 at 10:15
  • possible duplicate of [What is the size of column of int(11) in mysql in bytes?](http://stackoverflow.com/questions/5634104/what-is-the-size-of-column-of-int11-in-mysql-in-bytes) – LeGEC Jan 22 '15 at 16:23

1 Answers1

1

The 11 in Int(11) is misleading :
an Int column will always store a 32-bits integer, and its max value is 2 147 483 647 (less than 11 digits ...).
The 11 is only a display directive.

Phone numbers are often used as plain strings ("starts with", "international prefix", "format with initial + sign", etc...), it would make more sense to store them as regular strings : Varchar(11) (<- for Varchar or Char types, the number does indicate the length of the string ... )

If you really want to store a phone number in an integer column, you could change your type to BigInt (64-bits integer : max value allows you to store up to 18 digits).


AFAIK, the length directive is only useful if you specify Int(11) Zerofill. Values returned by your queries on such column would be padded with 0s for up to 11 digits.

Quoting the highly vague documentation :

M indicates the maximum display width for integer types. The maximum display width is 255. Display width is unrelated to the range of values a type can contain, as described in Section 11.2, “Numeric Types”. For floating-point and fixed-point types, M is the total number of digits that can be stored.

(M stands for "the number in parentheses")

In this SO question : What is the size of column of int(11) in mysql in bytes?
the third answer points to 11.2.5 Numeric Type Attributes :

MySQL supports an extension for optionally specifying the display width of integer data types in parentheses following the base keyword for the type. For example, INT(4) specifies an INT with a display width of four digits.

Community
  • 1
  • 1
LeGEC
  • 46,477
  • 5
  • 57
  • 104