Jump to content

[SOLVED] PHP MYSQL Image Upload


dewey_witt

Recommended Posts

OK Im on a deadline here. Last script for the site. Im stuck. Literaly. I have a form that i need to add an image upload element to. What i need is to upload the image to the server, and store the image name in mysql database. Im semi new to php. all my other scripts have worked I don't know what Im doing wrong. Well ok here if the php code. WARNING THIS IS A HUGE CODE! LOL But realy pleeeeeeeeease Helppppppp!

OK this is the php for the form without the image upload:

 

<?php

if (!isset($_SESSION)) {

  session_start();

}

 

//Check to see if all the data was entered and return them to the page if not.

if ((!$_POST['StrAddy']) || (!$_POST['City']) || (!$_POST['County']) || (!$_POST['State']) || (!$_POST['Suburb']) || (!$_POST['SubDiv']) || (!$_POST['Lst_Price']) || (!$_POST['Total_Rooms']) || (!$_POST['Bedrooms']) || (!$_POST['Baths']) || (!$_POST['FamilyRoom']) || (!$_POST['FormalDining']) || (!$_POST['First_Bedroom']) || (!$_POST['First_Bath']) || (!$_POST['Type']) || (!$_POST['Levels']) || (!$_POST['Arch']) || (!$_POST['Const']) || (!$_POST['Found']) || (!$_POST['Roof']) || (!$_POST['Windows']) || (!$_POST['Heat']) || (!$_POST['Cooling']) || (!$_POST['Age']) || (!$_POST['New']) || (!$_POST['Lot']) || (!$_POST['Acreage']) || (!$_POST['Basement']) || (!$_POST['Garage']) || (!$_POST['Parking']) || (!$_POST['Fireplace']) || (!$_POST['Gas']) || (!$_POST['Water']) || (!$_POST['Sewer']) || (!$_POST['Zoning']) || (!$_POST['SaTax']) || (!$_POST['TaxRate']) || (!$_POST['Asmnt']) || (!$_POST['Occupy']) || (!$_POST['Transp']) || (!$_POST['LvlCondo']) || (!$_POST['HOA']) || (!$_POST['SchoolDis']) || (!$_POST['SchoolPhone']) || (!$_POST['OhDate']) || (!$_POST['OhTime']) || (!$_POST['Auction']) || (!$_POST['TaxID']) || (!$_POST['SpecFin']) || (!$_POST['Comments']) || (!$_POST['FullName']) || (!$_POST['ListerComp']) || (!$_POST['CompCity']) || (!$_POST['CompState']) || (!$_POST['CompZip']) || (!$_POST['PrimPhone']) || (!$_POST['OfficePhone']) || (!$_POST['PrimFax']) || (!$_POST['Cell']) || (!$_POST['ListerEmail'])) {

// you will probably want to modify this - if you just send the user back to the page they were

// on without telling them they were missing some information, they won't know that their

// record wasn't saved. This is bad form!

header("Location: http://www.ezsellnow.com/Post_Listings.php");

echo "It seems you have missed a feild or more. Please Check your Information Carefully";

exit;

}

 

// only save the information if this page was accessed with POST information

if (isset($_POST)) {

//Create a variable to hold the database name.

$db_name="db_name";

//create a variable to hole the database name.

$table_name="Post";

//Add the conection information.

$connection= mysql_connect("Server", "username", "password")or die(mysql_error());

//Select the database

$db = mysql_select_db($db_name, $connection) or die(mysql_error());

//Create the sql statement.

$sql= "INSERT INTO $table_name(StrAddy, City, County, State, Suburb, SubDiv, Lst_Price, Total_Rooms, Bedrooms, Baths, FamilyRoom, FormalDining, First_Bedroom, First_Bath, Type, Levels, Arch, Const, Found, Roof, Windows, Heat, Cooling, Age, New, Lot, Acreage, Basement, Garage, Parking, Fireplace, Gas, Water, Sewer, Zoning, SaTax, TaxRate, Asmnt, Occupy, Transp, LvlCondo, HOA, SchoolDis, SchoolPhone, OhDate, OhTime, Auction, TaxID, SpecFin, Comments, FullName, ListerComp, CompAddy, CompCity, CompState, CompZip, PrimPhone, OfficePhone, PrimFax, Cell, ListerEmail) Values ('" . $_POST['StrAddy'] . "', '" . $_POST['City'] . "', '" . $_POST['County'] . "', '" . $_POST['State'] . "', '" . $_POST['Suburb'] . "', '" . $_POST['SubDiv'] . "', '" . $_POST['Lst_Price'] . "', '" . $_POST['Total_Rooms'] . "', '" . $_POST['Bedrooms'] . "', '" . $_POST['Baths'] . "', '" . $_POST['FamilyRoom'] . "', '" . $_POST['FormalDining'] . "', '" . $_POST['First_Bedroom'] . "', '" . $_POST['First_Bath'] . "', '" . $_POST['Type'] . "', '" . $_POST['Levels'] . "', '" . $_POST['Arch'] . "', '" . $_POST['Const'] . "', '" . $_POST['Found'] . "', '" . $_POST['Roof'] . "', '" . $_POST['Windows'] . "', '" . $_POST['Heat'] . "', '" . $_POST['Cooling'] . "', '" . $_POST['Age'] . "', '" . $_POST['New'] . "', '" . $_POST['Lot'] . "', '" . $_POST['Acreage'] . "', '" . $_POST['Basement'] . "', '" . $_POST['Garage'] . "', '" . $_POST['Parking'] . "', '" . $_POST['Fireplace'] . "', '" . $_POST['Gas'] . "', '" . $_POST['Water'] . "', '" . $_POST['Sewer'] . "', '" . $_POST['Zoning'] . "', '" . $_POST['SaTax'] . "', '" . $_POST['TaxRate'] . "', '" . $_POST['Asmnt'] . "', '" . $_POST['Occupy'] . "', '" . $_POST['Transp'] . "', '" . $_POST['LvlCondo'] . "', '" . $_POST['HOA'] . "', '" . $_POST['SchoolDis'] . "', '" . $_POST['SchoolPhone'] . "', '" . $_POST['OhDate'] . "', '" . $_POST['OhTime'] . "', '" . $_POST['Auction'] . "', '" . $_POST['TaxID'] . "', '" . $_POST['SpecFin'] . "', '" . $_POST['Comments'] . "', '" . $_POST['FullName'] . "', '" . $_POST['ListerComp'] . "', '" . $_POST['CompAddy'] . "', '" . $_POST['CompCity'] . "', '" . $_POST['CompState'] . "', '" . $_POST['CompZip'] . "', '" . $_POST['PrimPhone'] . "', '" . $_POST['OfficePhone'] . "', '" . $_POST['PrimFax'] . "', '" . $_POST['Cell'] . "', '" . $_POST['ListerEmail'] . "')";

 

//Create a variable to hold the result of the query.

$result = mysql_query($sql, $connection) or die(mysql_error());

//Show a success Message.

if ($result) {

  echo "SUCCESS - your record was saved.";

// you might just want to redirect them here and only warn them if there was an error

} else {

echo "OOPS - there has been an error. Please Try Again" . mysql_error();

}

}

 

//Close PHP block.

?>

OK Now again I need to upload a photo to server and record the pic name in this db. please help!

Link to comment
Share on other sites

hmm there is an awful lot of OR in your logic

 

<?php
$check = true;
foreach($_POST as $key => $val)
{
if (empty($val))
{
  $check = false;
  break;
}
}

if ((bool)$check == false)
{
echo "It seems you have missed a feild or more. Please Check your Information Carefully";
exit;
}
?>

 

as for the upload script - I can't really decipher what is going on in there as it is a mess and you didn't use the code tags on the post.

 

where exactly is the code to perform the upload??? or have I got the wrong end of the stick???

Link to comment
Share on other sites

The code to preform the upload isnt on here. this is the code for all the other feilds. I need to intergrate the other code into this. I have tried a gazzilion methods......... none worked. and sorry for not useing the color coding I'm New here :/

<?php
if (!isset($_SESSION)) {
  session_start();
}

//Check to see if all the data was entered and return them to the page if not.
if ((!$_POST['StrAddy']) || (!$_POST['City']) || (!$_POST['County']) || (!$_POST['State']) || (!$_POST['Suburb']) || (!$_POST['SubDiv']) || (!$_POST['Lst_Price']) || (!$_POST['Total_Rooms']) || (!$_POST['Bedrooms']) || (!$_POST['Baths']) || (!$_POST['FamilyRoom']) || (!$_POST['FormalDining']) || (!$_POST['First_Bedroom']) || (!$_POST['First_Bath']) || (!$_POST['Type']) || (!$_POST['Levels']) || (!$_POST['Arch']) || (!$_POST['Const']) || (!$_POST['Found']) || (!$_POST['Roof']) || (!$_POST['Windows']) || (!$_POST['Heat']) || (!$_POST['Cooling']) || (!$_POST['Age']) || (!$_POST['New']) || (!$_POST['Lot']) || (!$_POST['Acreage']) || (!$_POST['Basement']) || (!$_POST['Garage']) || (!$_POST['Parking']) || (!$_POST['Fireplace']) || (!$_POST['Gas']) || (!$_POST['Water']) || (!$_POST['Sewer']) || (!$_POST['Zoning']) || (!$_POST['SaTax']) || (!$_POST['TaxRate']) || (!$_POST['Asmnt']) || (!$_POST['Occupy']) || (!$_POST['Transp']) || (!$_POST['LvlCondo']) || (!$_POST['HOA']) || (!$_POST['SchoolDis']) || (!$_POST['SchoolPhone']) || (!$_POST['OhDate']) || (!$_POST['OhTime']) || (!$_POST['Auction']) || (!$_POST['TaxID']) || (!$_POST['SpecFin']) || (!$_POST['Comments']) || (!$_POST['FullName']) || (!$_POST['ListerComp']) || (!$_POST['CompCity']) || (!$_POST['CompState']) || (!$_POST['CompZip']) || (!$_POST['PrimPhone']) || (!$_POST['OfficePhone']) || (!$_POST['PrimFax']) || (!$_POST['Cell']) || (!$_POST['ListerEmail'])) {
   // you will probably want to modify this - if you just send the user back to the page they were
   // on without telling them they were missing some information, they won't know that their
   // record wasn't saved. This is bad form!
   header("Location: http://www.ezsellnow.com/Post_Listings.php");
   echo "It seems you have missed a feild or more. Please Check your Information Carefully";
    exit;
}

// only save the information if this page was accessed with POST information
if (isset($_POST)) {
   //Create a variable to hold the database name.
   $db_name="db_name";
   //create a variable to hole the database name.
   $table_name="Post";
   //Add the conection information.
   $connection= mysql_connect("Server", "username", "password")or die(mysql_error());
   //Select the database
   $db = mysql_select_db($db_name, $connection) or die(mysql_error());
   //Create the sql statement.
   $sql= "INSERT INTO $table_name(StrAddy, City, County, State, Suburb, SubDiv, Lst_Price, Total_Rooms, Bedrooms, Baths, FamilyRoom, FormalDining, First_Bedroom, First_Bath, Type, Levels, Arch, Const, Found, Roof, Windows, Heat, Cooling, Age, New, Lot, Acreage, Basement, Garage, Parking, Fireplace, Gas, Water, Sewer, Zoning, SaTax, TaxRate, Asmnt, Occupy, Transp, LvlCondo, HOA, SchoolDis, SchoolPhone, OhDate, OhTime, Auction, TaxID, SpecFin, Comments, FullName, ListerComp, CompAddy, CompCity, CompState, CompZip, PrimPhone, OfficePhone, PrimFax, Cell, ListerEmail) Values ('" . $_POST['StrAddy'] . "', '" . $_POST['City'] . "', '" . $_POST['County'] . "', '" . $_POST['State'] . "', '" . $_POST['Suburb'] . "', '" . $_POST['SubDiv'] . "', '" . $_POST['Lst_Price'] . "', '" . $_POST['Total_Rooms'] . "', '" . $_POST['Bedrooms'] . "', '" . $_POST['Baths'] . "', '" . $_POST['FamilyRoom'] . "', '" . $_POST['FormalDining'] . "', '" . $_POST['First_Bedroom'] . "', '" . $_POST['First_Bath'] . "', '" . $_POST['Type'] . "', '" . $_POST['Levels'] . "', '" . $_POST['Arch'] . "', '" . $_POST['Const'] . "', '" . $_POST['Found'] . "', '" . $_POST['Roof'] . "', '" . $_POST['Windows'] . "', '" . $_POST['Heat'] . "', '" . $_POST['Cooling'] . "', '" . $_POST['Age'] . "', '" . $_POST['New'] . "', '" . $_POST['Lot'] . "', '" . $_POST['Acreage'] . "', '" . $_POST['Basement'] . "', '" . $_POST['Garage'] . "', '" . $_POST['Parking'] . "', '" . $_POST['Fireplace'] . "', '" . $_POST['Gas'] . "', '" . $_POST['Water'] . "', '" . $_POST['Sewer'] . "', '" . $_POST['Zoning'] . "', '" . $_POST['SaTax'] . "', '" . $_POST['TaxRate'] . "', '" . $_POST['Asmnt'] . "', '" . $_POST['Occupy'] . "', '" . $_POST['Transp'] . "', '" . $_POST['LvlCondo'] . "', '" . $_POST['HOA'] . "', '" . $_POST['SchoolDis'] . "', '" . $_POST['SchoolPhone'] . "', '" . $_POST['OhDate'] . "', '" . $_POST['OhTime'] . "', '" . $_POST['Auction'] . "', '" . $_POST['TaxID'] . "', '" . $_POST['SpecFin'] . "', '" . $_POST['Comments'] . "', '" . $_POST['FullName'] . "', '" . $_POST['ListerComp'] . "', '" . $_POST['CompAddy'] . "', '" . $_POST['CompCity'] . "', '" . $_POST['CompState'] . "', '" . $_POST['CompZip'] . "', '" . $_POST['PrimPhone'] . "', '" . $_POST['OfficePhone'] . "', '" . $_POST['PrimFax'] . "', '" . $_POST['Cell'] . "', '" . $_POST['ListerEmail'] . "')";
  
   //Create a variable to hold the result of the query.
   $result = mysql_query($sql, $connection) or die(mysql_error());
   //Show a success Message.
   if ($result) {
         echo "SUCCESS - your record was saved.";
      // you might just want to redirect them here and only warn them if there was an error
   } else {
      echo "OOPS - there has been an error. Please Try Again" . mysql_error();
   }
}

//Close PHP block.
?> 

 

Did this help?

Link to comment
Share on other sites

well seeing the other code would help but at the end of the day all you will be using is move_uploaded_file().

 

anything else is useful for validation and what ever other checks/mods you wish to perform but other wise that is it.

 

make sure your form where you are uploading the file enctype="multipart/formdata" in the attributes.

Link to comment
Share on other sites

OK im still not seeing how to intergrate the two. If anyone knows I'll.... I dunno I was gonna say kiss you on the mouth but ewwwww lol I realy realy realy realy need a fix to this. BADLY. Wanna do my sis? Ten bux? I have candy :):P

Link to comment
Share on other sites

Sweeeeeeeeeeeeet. Your awsome........ what do i owe you? A date with granny? JP ;) For real I owe you one..... one last thing..... what do i have to modify to inert the file name into mysql database?

 

 

???

sql=" INSERT  Pic Into somedb VALUES (.\"$file[name].\"

???????

Link to comment
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.