Je vais expliquer en 2 mots mon probleme et je vais y mettre tout le code derriere.
Donc, je voudrais faire une page d'insertion de donnée vers une table. Mon probleme est que je voudrais en même temps uploader une image et enregistrer son nom dans la table.
J'ai ecrit le code qui me semble correcte, mais seulement, quand il doit faire l'envoie, rien ne se passe.
Mon code est le suivant :
<?php require_once('Connections/connexion.php');?>
<?php //Fonctions
//Fonction permettant de formé valeur parfaite pour les requete SQL
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = (!get_magic_quotes_gpc())? addslashes($theValue) : $theValue;
switch($theType)
{
case "text":
$theValue = ($theValue != "") ? "'".$theValue."'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'".$theValue."'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'".theValue."'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
//Fonction permettant de formé les date au format SQL
function form_to_db($date, $delimiter = ".")
{
if (ereg("([0-9]{1,2})$delimiter([0-9]{1,2})$delimiter([0-9]{2,4})",$date, $regs))
{
if (strlen($regs[1]) <2) $regs[1] = "0$regs[1]";
if (strlen($regs[2]) <2) $regs[2] = "0$regs[2]";
if (strlen($regs[3]) <4) $regs[3] = "20$regs[3]";
return "$regs[3]$regs[2]$regs[1]";
}
else
{
return false;
}
}
?>
<?php //Programme d'insert.
$infos = '';
//Initialisation des variables.
empty($_POST['Nom']) ? $Nom = '' : $Nom = $_POST['Nom'];
empty($_POST['Duree']) ? $Duree = '' : $Duree = $_POST['Duree'];
empty($_POST['Realisateur']) ? $Realisateur = '' : $Realisateur = $_POST['Realisateur'];
empty($_POST['Date_de_sortie']) ? $Date_de_sortie = '' : $Date_de_sortie = $_POST['Date_de_sortie'];
empty($_POST['Titre_original']) ? $Titre_original = '' : $Titre_original = $_POST['Titre_original'];
empty($_POST['Resume']) ? $Resume = '' : $Resume = $_POST['Resume'];
empty($_POST['idcategorie']) ? $Idcategorie = '' : $Idcategorie = $_POST['idcategorie'];
//Verification des informations.
if ($Nom == '') $infos .= "- Vous devez entrer un nom !<br>\n";
if ($Duree == '') $infos .= "- Vous devez entrer une durée en minutes !<br>\n";
if ($Realisateur == '') $infos .= "- Vous devez entrer un nom de réalisateur !<br>\n";
if ($Date_de_sortie == '') $infos .= "- Vous devez entrer une date de sortie !<br>\n";
if ($Titre_original == '') $infos .= "- Vous devez entrer le titre original !<br>\n";
if ($Resume == '') $infos .= "- Vous devez entrer un résumé !<br>\n";
if ($Idcategorie == '') $infos .= "- Vous devez sélectionner une catégorie !<br>\n";
$editFormAction = $HTTP_SERVER_VARS['PHP_SELF'];
if (isset($HTTP_SERVER_VARS['QUERY_STRING']))
{
$editFormAction .= "?" . $HTTP_SERVER_VARS['QUERY_STRING'];
}
if (!empty($_FILES['image']['size']))
{
$Image_size = $_FILES['Image']['size'];
$Image_name = $_FILES['Image']['name'];
$Image_tmpname = $_FILES['Image']['tmp_name'];
$Ext = strtolower(substr($Image_name, strrpos($Image_name, ".")+1));
$Valides = array("jpg","gif");
if (!in_array($Ext, $Valides)) $infos .= "- Ce type de fichier n'est pas accepté !<br>\n";
if ($infos == '')
{
move_uploaded_file($Image_tmpname, "./images/{$Image_name}") or $infos .= "- Impossible d'enregistrer le fichier, contacter le webmaster !<br>\n";
if ($infos == '')
{
$date_to_db = form_to_db($Date_de_sortie,'-');
if ((isset($HTTP_POST_VARS["MM_insert"])) && ($HTTP_POST_VARS["MM_insert"] == "form1"))
{
$insertSQL = sprintf("INSERT INTO film (idfilm, image, nom, duree, Realisateur, 'Date de sortie', 'Titre original', Resume, idcategorie) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s);",
GetSQLValueString($HTTP_POST_VARS['idfilm'], "int"),
GetSQLValueString($Image_name, "text"),
GetSQLValueString($Nom, "text"),
GetSQLValueString($Duree, "int"),
GetSQLValueString($Realisateur, "text"),
GetSQLValueString($date_to_db, "date"),
GetSQLValueString($Titre_original, "text"),
GetSQLValueString($Resume, "text"),
GetSQLValueString($Idcategorie, "text"));
mysql_select_db($database_connexion, $connexion);
$add = mysql_query($insertSQL, $connexion) or die(mysql_error());
$insertGoTo = "index.php";
if (isset($HTTP_SERVER_VARS['QUERY_STRING']))
{
$insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
$insertGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
}
header(sprintf("Location: %s", $insertGoTo));
}
}
}
}
?>
<?php //Programme de selection de categorie.
mysql_select_db($database_connexion, $connexion);
$query_Select = "SELECT * FROM categ";
$Select = mysql_query($query_Select, $connexion) or die (mysql_error());
$row_Select = mysql_fetch_assoc($Select);
$totalRows_Select = mysql_num_rows($Select);
?>
<html>
<head>
<title>Document sans titre</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form method="post" name="form1" action="insert3.php" enctype = "multipart/form-data" >
<table align = "center">
<tr valign = "baseline">
<td nowrap align = "right">Image :</td>
<td><input type = "hidden" name = "MAX_FILE_SIZE" value = "1000000"><input type = "file" name = "Image" value = "" size = "32"></td>
</tr>
<tr valign = "baseline">
<td nowrap align = "right">Nom :</td>
<td><input type = "text" name = "Nom" value ="" size = "32"></td>
</tr>
<tr valign = "baseline">
<td nowrap align = "right">Duree :</td>
<td><input type = "text" name = "Duree" value ="" size = "32"></td>
</tr>
<tr valign = "baseline">
<td nowrap align = "right">Realisateur :</td>
<td><input type = "text" name = "Realisateur" value ="" size = "32"></td>
</tr>
<tr valign = "baseline">
<td nowrap align = "right">Date de sortie :</td>
<td><input type = "text" name = "Date_de_sortie" value ="" size = "32"></td>
</tr>
<tr valign = "baseline">
<td nowrap align = "right">Titre original :</td>
<td><input type = "text" name = "Titre_original" value ="" size = "32"></td>
</tr>
<tr valign = "baseline">
<td nowrap align = "right">Resume :</td>
<td><textarea name = "Resume" cols = "50" rows = "5"></textarea></td>
</tr>
<tr valign = "baseline">
<td nowrap align = "right">Catégorie :</td>
<td>
<select name = "idcategorie">
<?php
do
{
?>
<option value = "<?php echo $row_Select['idcategorie']?>">
<?php echo $row_Select['nomcategorie']?>
</option>
<?php
}
while ($row_Select = mysql_fetch_assoc($Select));
?>
</select>
</td>
</tr>
<tr valign = "baseline">
<td nowrap align = "right"></td>
<td><input type="submit" value = "Insérer l'enregistrement"></td>
</tr>
</table>
<input type = "hidden" name = "idfilm" value = "">
<input type = "hidden" name = "MM_insert" value = "form1">
</form>
</body>
</html>
<?php
mysql_free_result($Select);
?>
Donc si quelqu'un pourrait me dire pourquoi aucun insert ne se fais, se serait gentils. Merci d'avance.