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
https://forums.phpfreaks.com/topic/58389-solved-php-mysql-image-upload/
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???

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?

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.

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].\"

???????

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.