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:
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:
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