0

I'm trying with one friend to make a simple upload form, but i'm stuck at the moment. Here's my trouble :

I made this form:

screenshot

This is the code :

<form method="post" class="form-horizontal" role="form" action="../i/insert.php" enctype="multipart/form-data">

    <div class="form-group">
        <label class="col-lg-2 control-label">Title</label>
        <div class="col-lg-10">
            <input name="v" id="title" type="text" class="form-control" placeholder="Banana nation">
        </div>
    </div>

    <div class="form-group">
        <label class="col-lg-2 control-label">Uploader's name</label>
        <div class="col-lg-10">
            <input name="name" id="upname" type="text" class="form-control" placeholder="Onision">
        </div>
    </div>

    <div class="form-group">
        <label class="col-lg-2 control-label">Description</label>
        <div class="col-lg-10">
            <textarea name="description" id="description" class="form-control" rows="3" placeholder="We are not robots, we are not slaves, we are banana nation."></textarea>
        </div>
    </div>

    <div class="form-group">
        <label for="exampleInputFile" class="col-lg-2 control-label">Video thumbnail</label>
        <div class="col-lg-10">
            <input type="file" id="thumbnail" name="thumbnail">
            <p class="help-block">Click to upload your file.</p>
        </div>
    </div>

    <div class="form-group">
        <div class="col-lg-offset-2 col-lg-10">
            <button type="submit" class="btn btn-default">Submit</button>
        </div>
    </div>
</form>

This form have an action to ../i/insert.php, here's the code (insert.php):

<?php
require('db.php');


    $dossier = '/var/www/vids/m/';
    $file = basename($_FILES['thumbnail']['name']);
    $thmb_name = basename($_FILES['thumbnail']['name']);
    $taille_maxi = 3145728;
    $taille = filesize($_FILES['thumbnail']['tmp_name']);
    $extensions = array('.png', '.jpg', '.jpeg');
    $extension = strrchr($_FILES['thumbnail']['name'], '.'); 

    if(!in_array($extension, $extensions)) 
    {
         $erreur = 'You can only upload a png, jpg or a jpeg file.';
    }
    if($taille>$taille_maxi)
    {
         $erreur = 'Hng.. it\'s too big sen-senpai.., it must be smaller than 3 mb...';
    }
    if(!isset($erreur))
    {

         $file = strtr($file, 
              'ÀÁÂÃÄÅÇÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÝàáâãäåçèéêëìíîïðòóôõöùúûüýÿ', 
              'AAAAAACEEEEIIIIOOOOOUUUUYaaaaaaceeeeiiiioooooouuuuyy');
         $file = preg_replace('/([^.a-z0-9]+)/i', '-', $file);
         if(move_uploaded_file($_FILES['thumbnail']['tmp_name'], $dossier . $file))
         {
              $thmb_result = 'Upload succesfull !';
         }
         else
         {
              $thmb_result = 'Upload failed !';
         }
    }
ajout($_POST['v'], $_POST['name'], $_POST['description'], $_POST['description'], $file);
function ajout($v, $name, $description, $fichier, $file)
    {
        global $bdd;
        $req = $bdd->prepare('INSERT INTO `vids`.`vidinfos` (`v`, `name`, `date`, `description`, `fichier`, `file`, `id`) VALUES (:v, :name, CURRENT_DATE(), :description, :fichier, :file, NULL);');
        $req->execute(array(
        ':v' => $v,
        ':name' => $name,
        ':description' => $description,
        ':fichier' => $fichier,
        ':thmb_name' => $file
        ));
    }
$bdd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

And here's database structure: enter image description here

So I fill up the form, and the picture successfully uploads in the directory I requested, but nothing shows up in my database, no new entry. I checked that every variable is correctly defined using var_dump.

Plus, I got this error in my PHP error log:

[Wed Jul 09 15:57:52 2014] [error] [client 10.0.0.1] PHP Warning:  PDOStatement::execute(): SQLSTATE[HY093]: Invalid parameter number: parameter was not defined on line 48
2
  • Just paste the actual code. The editor shows you how to do this – highlight the code and click on the {} icon. Also when pasting links, include the protocol (http://) so that they are converted into links that we don't have to copy-and-paste. Commented Jul 9, 2014 at 15:05
  • @CharlieS I couldn't post more than 4 links and pictures, sorry. Commented Jul 9, 2014 at 15:10

2 Answers 2

3

You're specifying a parameter file but are binding it as thmb_name. They should be the same parameter.

Sign up to request clarification or add additional context in comments.

Comments

0

Your parameters do not match -> :file != :thmb_name

$req = $bdd->prepare('INSERT INTO `vids`.`vidinfos` (`v`, `name`, `date`, `description`, `fichier`, `file`, `id`) VALUES (:v, :name, CURRENT_DATE(), :description, :fichier, **:file**, NULL);');

$req->execute(array(
    ':v' => $v,
    ':name' => $name,
    ':description' => $description,
    ':fichier' => $fichier,
    '**:thmb_name**' => $file
    ));

Should be

$req->execute(array(
    ':v' => $v,
    ':name' => $name,
    ':description' => $description,
    ':fichier' => $fichier,
    ':file' => $file
    ));

1 Comment

@IbrahimAshShohail, true, I added them for emphasis, but for clarity removed it from the correct parameter.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.