Jump to content

Recommended Posts

To sum it up, my goal is to save the name of an image in the DB and the image in the folder c: ... /uploads
of course along with this there is other data that we don't care about...

I did some searching + tutorials and i ended up with this code :

in HTML ->
[!--coloro:#FF6600--][span style=\"color:#FF6600\"][!--/coloro--]everything is inhtml except a tiny bit of php [!--colorc--][/span][!--/colorc--]
<form action="script.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<tr><td>Photo:</td> <td><input type="file" name="pic"></td></tr></form>

in PHP ->
$target_path = "\uploads";

$target_path = $target_path . basename( $HTTP_POST_FILES['pic']['name']);

if(move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'], $target_path)) {
echo "The file ". basename( $HTTP_POST_FILES['pic']['name']). " has been uploaded<br>";
}
else {
echo "There was an error uploading the file, please try again!";
}

// connection to the DB (works)

$req ="INSERT INTO object VALUES ('', '$region',...., '$pic', '$date_aj' )";

I click "Submit " there is no errors but there is nothing goin in the DB on in the folder /uploads does anybody have an idea ?

Christos
Link to comment
https://forums.phpfreaks.com/topic/8291-file-upload-phpmysql/
Share on other sites

Is that all your code? you dont appear to be executing the query.

Try the code below, dont forget to put in your connection data. Try echoing the target path where indicated to make sure that it is all sweet. Also, make sure the directory you are uploading to has the correct permissions set etc...

[code]$target_path = "\uploads";

if(is_dir($target_path){
    $target_path = $target_path . basename( $HTTP_POST_FILES['pic']['name']);
    
    //echo "$target_path<br><br>"; // Uncomment this line to test that the correct path is being used
    
    if(move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'], $target_path)) {
        echo "The file ". basename( $HTTP_POST_FILES['pic']['name']). " has been uploaded<br>";
        
        // Really your insert should go here, only gets done if the file is successfully uploaded
        
        // Connection to the db
        $req ="INSERT INTO object VALUES ('', '$region',...., '$pic', '$date_aj' )";
        $result = mysql_query($req) or die(mysql_error());
    }
    else {
        echo "There was an error uploading the file, please try again!";
    }
}
else{
    echo "ERROR: Directory does not exist<br><br>";
    @unlink($HTTP_POST_FILES['pic']['tmp_name']);
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/8291-file-upload-phpmysql/#findComment-30330
Share on other sites

No, that's not all the code, i didn't want you to read endless lines (well its not too many :-) )
so its [b]script 1.php[/b] (contains html forms and php only to show the date) so i wont show all the code here unless you really want to (takes space..)
[code]<form action="script.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
<tr><td>Photo:</td> <td><input type="file" name="pic"></td></tr></form>[/code]

Then [b]script2.php[/b] its only php and receives the files from script 1 and stores them in the DB
[code]<?
$region       = $HTTP_POST_VARS['region'];
$state        = $HTTP_POST_VARS['state'];
$city         = $HTTP_POST_VARS['city'];
$district     = $HTTP_POST_VARS['district'];
$accomodation = $HTTP_POST_VARS['accomodation'];
$transaction  = $HTTP_POST_VARS['transaction'];
$nb_rooms     = $HTTP_POST_VARS['nb_rooms'];
$floor        = $HTTP_POST_VARS['floor'];
$price        = $HTTP_POST_VARS['price'];
$garage = (isset($HTTP_POST_VARS['garage']) ? $HTTP_POST_VARS['garage'] : '');
$cellar = (isset($HTTP_POST_VARS['cellar']) ? $HTTP_POST_VARS['cellar'] : '');
$garden = (isset($HTTP_POST_VARS['garden']) ? $HTTP_POST_VARS['garden'] : '');
$pool   = (isset($HTTP_POST_VARS['pool'])   ? $HTTP_POST_VARS['pool'] : '');
$descr        = $HTTP_POST_VARS['descr'];
$age_par      = $HTTP_POST_VARS['age_par'];
#$pic          = $HTTP_POST_VARS['pic'];                     pic is the var we're intrested in

$date_aj=date('l dS \of F Y, \a  h:i:s A');

$target_path = "\uploads";

$target_path = $target_path . basename( $HTTP_POST_FILES['pic']['name']);

if(move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'], $target_path)) {
echo "The file ". basename( $HTTP_POST_FILES['pic']['name']). " has been uploaded<br>";
}
else {
echo "There was an error uploading the file, please try again!";
}

$host = "a host";
$user = "a user";
$pass = "a pass";
$base = "a db";

$db_conx = mysql_connect($host,$user,$pass) or print("error de connecting mysql");
mysql_select_db($base, $db_conx) or print("error connecting to base");

$req ="INSERT INTO object VALUES ('', '$region', '$state', '$city', '$district',
'$accomodation', '$transaction', '$nb_rooms', '$floor', '$price', '$garage', '$cellar',
'$garden', '$pool', '$descr', '$age_par', '$pic', '$date_aj' )"; #

    print ("The request is:<BR> $req<P>\n");
        if (mysql_db_query($base,$req,$db_conx)) {
             print ("Request succusfull.<BR>\n");
        }

  mysql_close ($db_conx);
[/code] And tha'ts all the php code. No errors but adds nothing to the DB !!!
Link to comment
https://forums.phpfreaks.com/topic/8291-file-upload-phpmysql/#findComment-30510
Share on other sites

Ok the new is, that the problem is not the picture upload...actually it doesn't write anything in the DB so maybe i should give you the code of the [b]script1.php[/b] just in case i'm missing something...
[code]<html>
    <head>
        <title> * New Object * </title>
    </head>

    <body>
            <div style="text-align: center;"> </div>
            <h2 style="text-align: center;"> * Ajoutez votre annonce ! * </h2>
        <table>

            <form action="ad_obj.php" method="POST" enctype="multipart/form-data">

                    <td>Region:</td><td><select name="region" >
                        <option value="" selected="selected">Toutes les  regions</option>
                        <option >Alsace</option>
                        <option >Aquitaine</option>
                        <option >Auvergne</option>
                        <option >Basse-Normandie</option>
                        <option >Bourgogne</option>
                        <option >Bretagne</option>
                        <option >Centre</option>
                        <option >Champagne-Ardenne</option>
                        <option >Corse</option>
                        <option >DOM-TOM</option>
                        <option >Franche-Comte</option>
                        <option >Guadeloupe</option>
                        <option >Guyane</option>
                        <option >Haute-Normandie</option>
                        <option >ile-de-France</option>
                        <option >Languedoc-Roussillon</option>
                        <option >Limousin</option>
                        <option >Lorraine</option>
                        <option >Martinique</option>
                        <option >Midi-Pyrenees</option>
                        <option >Nord-Pas-de-Calais</option>
                        <option >Pays de la Loire</option>
                        <option >Picardie</option>
                        <option >Poitou-Charentes</option>
                        <option >Provence-Alpes-C.d'Azur</option>
                        <option >Reunion</option>
                        <option >Rhone-Alpes</option></select>
                    </td>
                </tr>

                    <td>Departement:</td><td align="left"><select name="state" >
                        <option value="" selected="selected">Tous les departements</option>
                        <option >01 Ain</option>
                        <option >02 Aisne</option>
                        <option >03 Allier</option>
                        <option >04 Alpes de Haute Provence</option>
                        <option >05 Hautes Alpes</option>
                        <option >06 Alpes Maritimes</option>
                        <option >07 Ardeche</option>
                        <option >08 Ardennes</option>
                        <option >09 Ariege</option>
                        <option >10 Aube</option>
                        <option >11 Aude</option>
                        <option >12 Aveyron</option>
                        <option >13 Bouches du Rhone</option>
                        <option >14 Calvados</option>
                        <option >15 Cantal</option>
                        <option >16 Charente</option>
                        <option >17 Charente Maritime</option>
                        <option >18 Cher</option>
                        <option >19 Correze</option>
                        <option >20 Corse</option>
                        <option >21 Cote d'Or</option>
                        <option >22 Cotes d'Armor</option>
                        <option >23 Creuse</option>
                        <option >24 Dordogne</option>
                        <option >25 Doubs</option>
                        <option >26 Drome</option>
                        <option >27 Eure</option>
                        <option >28 Eure et Loir</option>
                        <option >29 Finistere</option>
                        <option >30 Gard</option>
                        <option >31 Haute Garonne</option>
                        <option >32 Gers</option>
                        <option >33 Gironde</option>
                        <option >34 Herault</option>
                        <option >35 Ille et Vilaine</option>
                        <option >36 Indre</option>
                        <option >37 Indre et Loire</option>
                        <option >38 Isere</option>
                        <option >39 Jura</option>
                        <option >40 Landes</option>
                        <option >41 Loir et Cher</option>
                        <option >42 Loire</option>
                        <option >43 Haute Loire</option>
                        <option >44 Loire Atlantique</option>
                        <option >45 Loiret</option>
                        <option >46 Lot</option>
                        <option >47 Lot et Garone</option>
                        <option >48 Lozere</option>
                        <option >49 Maine et Loire</option>
                        <option >50 Manche</option>
                        <option >51 Marne</option>
                        <option >52 Haute Marne</option>
                        <option >53 Mayenne</option>
                        <option >54 Meurthe et Moselle</option>
                        <option >55 Meuse</option>
                        <option >56 Morbihan</option>
                        <option >57 Moselle</option>
                        <option >58 Nievre</option>
                        <option >59 Nord</option>
                        <option >60 Oise</option>
                        <option >61 Orne</option>
                        <option >62 Pas de Calais</option>
                        <option >63 Puy de Dome</option>
                        <option >64 Pyrenees Atlantique</option>
                        <option >65 Hautes Pyrenees</option>
                        <option >66 Pyrenees Orientales</option>
                        <option >67 Bas-Rhin</option>
                        <option >68 Haut-Rhin</option>
                        <option >69 Rhone</option>
                        <option >70 Haute Saone</option>
                        <option >71 Saone et Loire</option>
                        <option >72 Sarthe</option>
                        <option >73 Savoie</option>
                        <option >74 Haute Savoie</option>
                        <option >75 Paris</option>
                        <option >76 Seine Maritime</option>
                        <option >77 Seine et Marne</option>
                        <option >78 Yvelines</option>
                        <option >79 Deux Sevres</option>
                        <option >80 Somme</option>
                        <option >81 Tarn</option>
                        <option >82 Tarn et Garonne</option>
                        <option >83 Var</option>
                        <option >84 Vaucluse</option>
                        <option >85 Vendee</option>
                        <option >86 Vienne</option>
                        <option >87 Haute Vienne</option>
                        <option >88 Vosges</option>
                        <option >89 Yonne</option>
                        <option >90 Territoire de Belfort</option>
                        <option >91 Essonne</option>
                        <option >92 Hauts de Seine</option>
                        <option >93 Seine Saint Denis</option>
                        <option >94 Val de Marne</option>
                        <option >95 Val d'Oise</option>
                        <option >99 DOM-TOM</option></select>
                    </td>
                </tr>

                <tr><td>Ville:</td> <td><input type="text" name="city"></td></tr>
                <tr><td>Quartier:</td> <td><input type="text" name="district"></td></tr>

                <td>Type de logement:</td><td><select name="accomodation" >
                        <option value="" selected="selected">Tous les types</option>
                        <option>Maison</option>
                        <option>Appartement</option>
                        <option>Chalet</option>
                        <option>Chateau</option>
                        <option>Local</option></select>

                <tr><td>Type de Transaction :&nbsp &nbsp</td>
                <td>Vente &nbsp &nbsp &nbsp &nbsp
                    <input type="radio" name="transaction"value="Vente" checked> </input></td>
                <td>Location &nbsp &nbsp &nbsp &nbsp &nbsp
                    <input type="radio" name="transaction"value="Location"unchecked> </input></td></tr>
                <td>Colocation &nbsp &nbsp &nbsp &nbsp &nbsp
                    <input type="radio" name="transaction"value="colocq"unchecked> </input></td></tr>


                <td>Nombre de Pieces :</td>    <td><select name="nb_rooms"><option> </option>
                <option>1</option><option>2</option><option>3</option><option>4</option>
                <option>5</option><option>6</option><option>7</option><option>8</option>
                <option>9</option><option>10</option></select>

                <tr><td>Etage :</td><td><select name="floor"><option> </option>
                <option>rdc</option><option>1</option><option>2</option>
                <option>3</option><option>4</option><option>5</option></select>

                <tr><td>Prix :</td> <td><input type="text" name="price" maxlength="7"></td></tr>

                <tr><td>&nbsp &nbsp Options :</td></tr><br>
                <td>Garage &nbsp <input type="checkbox" name="garage"value="Garage"unchecked> </input></td>
                <td>Cave &nbsp <input type="checkbox" name="cellar"value="Cellar"unchecked> </input></td>
                <td>Jardin &nbsp <input type="checkbox" name="garden"value="Garden"unchecked> </input></td>
                <td>Piscine &nbsp <input type="checkbox" name="pool"value="Pool"unchecked> </input></td>

                <tr><td>Description :</td> <td><input type="textarea" name="descr" rows="3"></td></tr>

                <td>Vendeur:</td>
                    <td>Agence &nbsp &nbsp &nbsp
                          <input type="radio" name="age_par" value="Agence" checked></input></td>
                    <td>Paticulier &nbsp &nbsp
                        <input type="radio" name="age_par" value="Particulier" unchecked></input></td>

                           <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
                <tr><td>Photo:</td> <td><input type="file" name="pic"></td></tr>

                <tr><td>La date est : <?echo date('l dS \of F Y, \a   h:i:s A');?>

                <tr><td><input type="submit" value="Envoyer"></input></td></tr>
                    <td><input type="reset" value="Retablir"></input></td>
                <br>
            </form>
        </table>
                <br>
                <p><a href="index.html"> Accueil</a></p>
    </body>
</html>
[/code]

At the moment i'm without any ideas...it doesn't make sence it should upload at least the rest infos.
Link to comment
https://forums.phpfreaks.com/topic/8291-file-upload-phpmysql/#findComment-30679
Share on other sites

in script1.php where you have

[code]print ("The request is:<BR> $req<P>\n");
        if (mysql_db_query($base,$req,$db_conx)) {
             print ("Request succusfull.<BR>\n");
        }[/code]

Replace it with this

[code]mysql_db_query($base,$req,$db_conx) or die("UNABLE TO WRITE TO DATABASE: . " mysql_error());

print ("The request is:<BR> $req<P>\n");
print ("Request succusfull.<BR>\n");[/code]

You should always use the general format of
[code]
mysql_query($query) or die(mysql_error())[/code]

to help identify any errors in querying the database
Link to comment
https://forums.phpfreaks.com/topic/8291-file-upload-phpmysql/#findComment-30684
Share on other sites

[b]sanfly[/b] first of all thanks for your reply, i will folow your advise but for the moment i'm trying to find whats' wrong and i don't think it's this because i have another page where i enter the infos of the user and it's working fine...so i don't think the problem is this.
But why not i'll give it a try !!!

christos
Link to comment
https://forums.phpfreaks.com/topic/8291-file-upload-phpmysql/#findComment-30700
Share on other sites

That's the new code you suggested sanfly[code]$region = $HTTP_POST_VARS['region'];
$state = $HTTP_POST_VARS['state'];
$city = $HTTP_POST_VARS['city'];
$district = $HTTP_POST_VARS['district'];
$accomodation = $HTTP_POST_VARS['accomodation'];
$transaction = $HTTP_POST_VARS['transaction'];
$nb_rooms = $HTTP_POST_VARS['nb_rooms'];
$floor = $HTTP_POST_VARS['floor'];
$price = $HTTP_POST_VARS['price'];
$garage = (isset($HTTP_POST_VARS['garage']) ? $HTTP_POST_VARS['garage'] : '');
$cellar = (isset($HTTP_POST_VARS['cellar']) ? $HTTP_POST_VARS['cellar'] : '');
$garden = (isset($HTTP_POST_VARS['garden']) ? $HTTP_POST_VARS['garden'] : '');
$pool = (isset($HTTP_POST_VARS['pool']) ? $HTTP_POST_VARS['pool'] : '');
$descr = $HTTP_POST_VARS['descr'];
$age_par = $HTTP_POST_VARS['age_par'];
$pic = $HTTP_POST_VARS['pic'];

$date_aj=date('l dS \of F Y, \a h:i:s A');


$host = "blah";
$user = "blah";
$pass = "blah";
$base = "blah";

$target_path = "\uploads";

if(is_dir($target_path){ #***************** Parse error on this line************************
$target_path = $target_path . basename( $HTTP_POST_FILES['pic']['name']);

echo "$target_path<br><br>"; // Uncomment this line to test that the correct path is being used

if(move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'], $target_path)) {
echo "The file ". basename( $HTTP_POST_FILES['pic']['name']). " has been uploaded<br>";

$db_conx = mysql_connect($host,$user,$pass) or print("erreur de connection mysql");
mysql_select_db($base, $db_conx) or print("erreur connection base");

$req ="INSERT INTO object VALUES ('', '$region', '$state', '$city', '$district',
'$accomodation', '$transaction', '$nb_rooms', '$floor', '$price', '$garage', '$cellar',
'$garden', '$pool', '$descr', '$age_par', '$pic', '$date_aj' )";
$result = mysql_query($req) or die(mysql_error());
}
else {
echo "There was an error uploading the file, please try again!";
}
}
else {
echo "ERROR: Directory does not exist<br><br>";
@unlink($HTTP_POST_FILES['pic']['tmp_name']);
}
Link to comment
https://forums.phpfreaks.com/topic/8291-file-upload-phpmysql/#findComment-31299
Share on other sites

If you post your problems here rather than PMing them to me, you will most likely get a quicker response. Im not the only one that would be able to help you solve this.

Anyway, the parse error was because of a missing ")". Try this instead:

[code]$region = $HTTP_POST_VARS['region'];
$state = $HTTP_POST_VARS['state'];
$city = $HTTP_POST_VARS['city'];
$district = $HTTP_POST_VARS['district'];
$accomodation = $HTTP_POST_VARS['accomodation'];
$transaction = $HTTP_POST_VARS['transaction'];
$nb_rooms = $HTTP_POST_VARS['nb_rooms'];
$floor = $HTTP_POST_VARS['floor'];
$price = $HTTP_POST_VARS['price'];
$garage = (isset($HTTP_POST_VARS['garage']) ? $HTTP_POST_VARS['garage'] : '');
$cellar = (isset($HTTP_POST_VARS['cellar']) ? $HTTP_POST_VARS['cellar'] : '');
$garden = (isset($HTTP_POST_VARS['garden']) ? $HTTP_POST_VARS['garden'] : '');
$pool = (isset($HTTP_POST_VARS['pool']) ? $HTTP_POST_VARS['pool'] : '');
$descr = $HTTP_POST_VARS['descr'];
$age_par = $HTTP_POST_VARS['age_par'];
$pic = $HTTP_POST_VARS['pic'];

$date_aj=date('l dS \of F Y, \a h:i:s A');


$host = "blah";
$user = "blah";
$pass = "blah";
$base = "blah";

$target_path = "\uploads";

if(is_dir($target_path)){ #***************** Parse error on this line************************
$target_path = $target_path . basename( $HTTP_POST_FILES['pic']['name']);

echo "$target_path<br><br>"; // Uncomment this line to test that the correct path is being used

if(move_uploaded_file($HTTP_POST_FILES['pic']['tmp_name'], $target_path)) {
echo "The file ". basename( $HTTP_POST_FILES['pic']['name']). " has been uploaded<br>";

$db_conx = mysql_connect($host,$user,$pass) or print("erreur de connection mysql");
mysql_select_db($base, $db_conx) or print("erreur connection base");

$req ="INSERT INTO object VALUES ('', '$region', '$state', '$city', '$district',
'$accomodation', '$transaction', '$nb_rooms', '$floor', '$price', '$garage', '$cellar',
'$garden', '$pool', '$descr', '$age_par', '$pic', '$date_aj' )";
$result = mysql_query($req) or die(mysql_error());
}
else {
echo "There was an error uploading the file, please try again!";
}
}
else {
echo "ERROR: Directory does not exist<br><br>";
@unlink($HTTP_POST_FILES['pic']['tmp_name']);
}[/code]
Link to comment
https://forums.phpfreaks.com/topic/8291-file-upload-phpmysql/#findComment-31401
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.