Formulaire+base de donée

kissi -  
Scalpweb Messages postés 1483 Statut Membre -
Bonjour,
jai mon script d'upload image ki fonctionne a merveil. mon problem est que je veux pouvoir inserer le chemain pour acceder a l'image dans ma base de donnée, cee ke je n'arrive pas a faire.
voici mon script:
<?php

//load the config file
include("config.php");

//if the for has submittedd
if (isset($_POST['upForm'])){

$file_type = $_FILES['imgfile']['type'];
$file_name = $_FILES['imgfile']['name'];
$file_size = $_FILES['imgfile']['size'];
$file_tmp = $_FILES['imgfile']['tmp_name'];

//check if you have selected a file.
if(!is_uploaded_file($file_tmp)){
echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit(); //exit the script and don't do anything else.
}
//check file extension
$ext = strrchr($file_name,'.');
$ext = strtolower($ext);
if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
echo "Wrong file extension. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
exit();
}
//get the file extension.
$getExt = explode ('.', $file_name);
$file_ext = $getExt[count($getExt)-1];

//create a random file name
$rand_name = md5(time());
$rand_name= rand(0,999999999);
//get the new width variable.
$ThumbWidth = $img_thumb_width;

//keep image type
if($file_size){
if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
$new_img = imagecreatefromjpeg($file_tmp);
}elseif($file_type == "image/x-png" || $file_type == "image/png"){
$new_img = imagecreatefrompng($file_tmp);
}elseif($file_type == "image/gif"){
$new_img = imagecreatefromgif($file_tmp);
}
//list width and height and keep height ratio.
list($width, $height) = getimagesize($file_tmp);
$imgratio=$width/$height;
if ($imgratio>1){
$newwidth = $ThumbWidth;
$newheight = $ThumbWidth/$imgratio;
}else{
$newheight = $ThumbWidth;
$newwidth = $ThumbWidth*$imgratio;
}
//function for resize image.
if (function_exists('imagecreatetruecolor')){
$resized_img = imagecreatetruecolor($newwidth,$newheight);
}else{
die("Error: Please make sure you have GD library ver 2+");
}
imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
//save image
ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
ImageDestroy ($resized_img);
ImageDestroy ($new_img);
//print message
echo "<br>Image Thumb: <a href=\"$path_thumbs/$rand_name.$file_ext\">$path_thumbs/$rand_name.$file_ext</a>";
}

//upload the big image
move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");

echo "<br>Image Big: <a href=\"$path_big/$rand_name.$file_ext\">$path_big/$rand_name.$file_ext</a>";

echo "<br><br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

}else{ //if the form hasn't been submitted.

//print the form
echo "<script>
function view_img(img_name){
document[img_name].src = upForm.imgfile.value;
document[img_name].width = 150;
}
</script>\n\n
<br><h3>:: Browse an Image to Upload:</h3>\n
<form method=\"post\" name=\"upForm\" enctype=\"multipart/form-data\" action=\"$_SERVER[PHP_SELF]\">\n
<input type=\"file\" name=\"imgfile\" onchange=\"javascript:view_img('img_vv');\"> <img src='' name='img_vv' width='0'><br>\n
Image width will resize to <b>$img_thumb_width</b> with height ratio.
<br><input type=\"Submit\" name=\"upForm\" value=\"Upload & Resize\">\n
</form>
";

}

?>
A voir également:

3 réponses

Scalpweb Messages postés 1483 Statut Membre 43
 
Tu commences par te connecter à ta base de donnée :

mysql_connect($host,$user, $pass);
@mysql_select_db($bdd);

Evidemment, tu changes $host, $user, $pass et $bdd par les valeurs correspondantes à ton serveur.

Ensuite, tu envoies la commande MySql :

$commandeSQL = "INSERT INTO `ta_table` ( `champ1`,`champ2, `champ3...`)
VALUES ('Valeur1', 'Valeur2', 'Valeur3...');";
$requet=mysql_db_query($bdd,$commandeSQL);

La encore, il faut que tu modifies la requete en fonction de la structure de table MySQL
0
kissi
 
oui je l'ai fait comme suite mais je reçoi des message d'erreur:

116.<? mysql_connect("localhost","root","") or die(" Erreur de connection au serveur");
117.mysql_select_db("prod") or die("Erreur de connection a la base de donnees");//ouvre la BDD
118.mysql_query("INSERT INTO juste VALUES('','$rand_name.$file_ext','$rand_name','$file_ext','$path_big')")
119.or die(" Erreur d'insertion de donnees");
120.echo "Nous donnerons suite à votre enrégistrement dans les plus brefs delais";
121.mysql_close(); ?>

erreur:

Notice: Undefined variable: rand_name in c:\program files\easyphp1-8\www\upload_file\index.php on line 118

Notice: Undefined variable: file_ext in c:\program files\easyphp1-8\www\upload_file\index.php on line 118

Notice: Undefined variable: rand_name in c:\program files\easyphp1-8\www\upload_file\index.php on line 118

Notice: Undefined variable: file_ext in c:\program files\easyphp1-8\www\upload_file\index.php on line 118
Nous donnerons suite à votre enrégistrement dans les plus brefs delais
0
Scalpweb Messages postés 1483 Statut Membre 43
 
Il faut que tu places ce code juste à la fin de ta balise php, juste avant le ?>, pour que les variables soient bien reconnues.
0