Jump to content

INPUT FORM THAT ALLOWS USER TO UPLOAD IMAGE AND STORES PATH IN DATABASE


kamaral9696

Recommended Posts

I created a site using Dreamweaver.  I do know HTML and some php.  What I am really stuck on now it the following:

 

I have a site where users can upload a listing with all the relevant information to a database.  I would like the users to also be able to upload an image with the other information the the form.  I added the upload feature to the form and it works, but now I don't know what to do with it.  I was trying to insert it as a blob in the database but it wasn't retrieving it correctly and I have read so much about why I shouldn't do that.

 

So now I am thinking I should store the image in a directory and store the image information in a database?  Does anyone know how I would go about doing this?  It sounds pretty basic but my head is so clogged right now I don't even know where to start...

 

Thanks for any help you can give me!

 

This is the code with the form to add the listing:

<?php require_once('../Connections/admin.php'); ?>
<?php
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 != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "insertCommRent")) {
  $insertSQL = sprintf("INSERT INTO commercial_rent (state_id, type_id, city, title, price, terms, description, photo, contact, email) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['state_id'], "int"),
                       GetSQLValueString($_POST['type_id'], "int"),
                       GetSQLValueString($_POST['city'], "text"),
                       GetSQLValueString($_POST['title'], "text"),
                       GetSQLValueString($_POST['price'], "text"),
                       GetSQLValueString($_POST['terms'], "text"),
                       GetSQLValueString($_POST['description'], "text"),
                       GetSQLValueString($_POST['photo'], "text"),
                       GetSQLValueString($_POST['contact'], "text"),
                       GetSQLValueString($_POST['email'], "text"));

  mysql_select_db($database_admin, $admin);
  $Result1 = mysql_query($insertSQL, $admin) or die(mysql_error());

  $insertGoTo = "../thankyou.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

mysql_select_db($database_admin, $admin);
$query_listStates = "SELECT * FROM `state` ORDER BY `state`.`state`";
$listStates = mysql_query($query_listStates, $admin) or die(mysql_error());
$row_listStates = mysql_fetch_assoc($listStates);
$totalRows_listStates = mysql_num_rows($listStates);

mysql_select_db($database_admin, $admin);
$query_listTypes = "SELECT * FROM type_comm_rent ORDER BY type ASC";
$listTypes = mysql_query($query_listTypes, $admin) or die(mysql_error());
$row_listTypes = mysql_fetch_assoc($listTypes);
$totalRows_listTypes = mysql_num_rows($listTypes);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/adminList_template.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Castletgate3</title>
<!-- InstanceEndEditable -->
<link href="styles/basic.css" rel="stylesheet" type="text/css" media="screen" />

<style type="text/css">
<!--
.style1 {color: #003366}
.style12 {font-size: 24px}
-->
</style>

<link href="/styles/basic.css" rel="stylesheet" type="text/css" />
<!-- InstanceBeginEditable name="head" -->
<style type="text/css">
<!--
.style14 {
font-size: 9px;
color: #FF0000;
}
-->
</style>
<!-- InstanceEndEditable -->
</head>

<body>


<div id="wrapper">
<!--wrapper-->

<div id="header">
<!--header-->
 <div id="header_image"><img src="../images/castlegate.jpg" alt="Castlegate3 Logo" width="650" height="75" /></div>	
 <div id="header_ad"><img src="/images/header_ad.gif" width="200" height="75" /></div>  
<div id="home_link"><a href="index.php" class="style12"></a></div>
<div id="time"> 
	<?php 
	echo date ('F j, Y')
	?> 
</div>

</div>
<!--end header-->
<div class="colmask threecol">
  <div class="colmid">
    <div class="colleft"><!-- InstanceBeginEditable name="Form" -->
      <div class="col1">
        <!-- middle -->
        <h1 align="center">Add a Commercial Rental Listing</h1>

        <form id="insertCommRent" name="insertCommRent" style="margin:10px; font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px; width:400px;" enctype="multipart/form-data" method="POST" action="<?php echo $editFormAction; ?>">
          <p>
            <label for="state">State:</label>
            <br />
            <select name="state_id" id="state_id">
              <option value="">Choose One</option>
              <?php
do {  
?>
              <option value="<?php echo $row_listStates['state_id']?>"><?php echo $row_listStates['state']?></option>
              <?php
} while ($row_listStates = mysql_fetch_assoc($listStates));
  $rows = mysql_num_rows($listStates);
  if($rows > 0) {
      mysql_data_seek($listStates, 0);
  $row_listStates = mysql_fetch_assoc($listStates);
  }
?>
            </select>
          </p>
          <p>
            <label for="city">City:</label>
            <br />
            <input type="text" name="city" id="city" />
          </p>
          <p>
            <label for="title">Listing Title:</label>
            <span class="style14">(Please type a short description for your listing)            </span><br />
            <input type="text" name="title" id="title" />
          </p>
          <p>
            <label for="type">Type:</label>
            <br />
            <select name="type_id" id="type_id">
              <option value="">Choose One</option>
              <?php
do {  
?>
              <option value="<?php echo $row_listTypes['type_id']?>"><?php echo $row_listTypes['type']?></option>
              <?php
} while ($row_listTypes = mysql_fetch_assoc($listTypes));
  $rows = mysql_num_rows($listTypes);
  if($rows > 0) {
      mysql_data_seek($listTypes, 0);
  $row_listTypes = mysql_fetch_assoc($listTypes);
  }
?>
            </select>
          </p>
          <p>
            <label for="price">Rental Fee:</label>
            <br />
            <input type="text" name="price" id="price" />
          </p>
          <p>
            <label for="terms">Terms:</label>
            <br />
            <textarea name="terms" cols="50" rows="3" id="terms">
      </textarea>
          </p>
          <p>
            <label for="description">Description:</label>
            <br />
            <textarea name="description" cols="50" rows="10" id="description">
      </textarea>
          </p>
         <input type="hidden" name="MAX_FILE_SIZE" value="100000">
          <p>
            <label for="photo">Upload photo</label>
            <br />
            <input type="file" name="photo" id="photo" size="40" />
          </p>
          <p>
            <label for="contact">Contact Name:</label>
            <br />
            <input type="text" name="contact" id="contact" />
          </p>
          <p>
            <label for="price"></label>
            <label for="email">Email Address:</label>
            <br />
            <input type="text" name="email" id="email" />
          </p>
          <input name="insert" type="submit" id="insert" value="Submit Listing" />
          <input type="hidden" name="MM_insert" value="insertCommRent" />
        </form>
      </div>
    <!-- InstanceEndEditable -->
      <!-- end middle -->
<div class="col2">
        <!-- left -->
	<div id="nav_home">
	   <p align="center" class="style9"><a href="/index.php" class="style12">Home</a></p>
	 </div>
        <div id="property_listing">
          <h3 align="center" class="style1"><strong> Add a Property Listing </strong></h3>
          <hr />
          <div id=commbg><li><a href="../commercial_add.php">Commercial </a></li>
          </div>
          <div id=resbg><li><a href="../residential_add.php">Residential </a></li>
          </div>
          <div id=vacbg><li><a href="../vacation_add.php"> Vacation </a></li>
          </div>
        </div>
        <div id="tips">
          <h4 align="center" class="style1">Tips for marketing your property</h4>
          <p align="center"><a href="marketing.php">Click Here</a></p>
        </div>
        <div id="info">
          <h4 align="center" class="style1">Information for first time buyers</h4>
          <p align="center"><a href="info.php">Click Here</a></p>
        </div>
        <div id="left_ad">
          <p><img src="../images/ad_left.jpg" alt="Advertisement" width="230" height="75" /></p>
        </div>
      </div>
      <!-- end left -->
<div class="col3">
        <!-- right -->
        <div id="right_ads">
	 <img src="../images/ad_right_top.jpg" alt="Advertisment" width="125" height="250" />           	       
	 <img src="../images/ad_right_bottom.jpg" alt="Advertisment" width="125" height="250" />		 </div>
      </div>
      <!-- end right -->
    </div>
  </div>
</div>
<div id="bottom_adspace">
 <div id="left">
   	  <img src="../images/ad_bottom_left.jpg" alt="Advertisment" width="400" height="100" />     </div>
 <div id="right">	   
     <img src="../images/ad_bottom_right.jpg" alt="Adverstisment" width="400" height="100" />     </div>
</div>

<div id="footer">
<!-- footer -->
<div class="innerbox_footer">
<ul>
   <li><a href="about.php"> About Us </a></li>
   <li><a href="terms.php"> Terms of Use </a></li>
   <li><a href="privacy.php"> Privacy Policy </a></li>
   <li><a href="faq.php"> FAQ's </a></li>
   <li> <a href="disclaimer.php">Disclaimer</a> </li>
   <li> <a href="contact.php">Contact Us </a></li>	
</ul>      
</div>
</div>
<!-- end footer -->	 

</div>
<!-- end wrapper -->


</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result($listStates);

mysql_free_result($listTypes);
?>



This is the code with the page that retrieves the results:

[code]
<?php require_once('../Connections/admin.php'); ?>
<?php
$currentPage = $_SERVER["PHP_SELF"];

$maxRows_result_commRent = 10;
$pageNum_result_commRent = 0;
if (isset($_GET['pageNum_result_commRent'])) {
  $pageNum_result_commRent = $_GET['pageNum_result_commRent'];
}
$startRow_result_commRent = $pageNum_result_commRent * $maxRows_result_commRent;

$var1_result_commRent = "-1";
if (isset($_GET['comm_id'])) {
  $var1_result_commRent = (get_magic_quotes_gpc()) ? $_GET['comm_id'] : addslashes($_GET['comm_id']);
}
mysql_select_db($database_admin, $admin);
$query_result_commRent = sprintf("SELECT type_comm_rent.type, commercial_rent.city, `state`.`state`, commercial_rent.price, commercial_rent.terms, commercial_rent.description, commercial_rent.contact, commercial_rent.email FROM type_comm_rent, commercial_rent, `state` WHERE commercial_rent.comm_id = %s AND commercial_rent.state_id = state.state_id AND commercial_rent.type_id = type_comm_rent.type_id", $var1_result_commRent);
$query_limit_result_commRent = sprintf("%s LIMIT %d, %d", $query_result_commRent, $startRow_result_commRent, $maxRows_result_commRent);
$result_commRent = mysql_query($query_limit_result_commRent, $admin) or die(mysql_error());
$row_result_commRent = mysql_fetch_assoc($result_commRent);

if (isset($_GET['totalRows_result_commRent'])) {
  $totalRows_result_commRent = $_GET['totalRows_result_commRent'];
} else {
  $all_result_commRent = mysql_query($query_result_commRent);
  $totalRows_result_commRent = mysql_num_rows($all_result_commRent);
}
$totalPages_result_commRent = ceil($totalRows_result_commRent/$maxRows_result_commRent)-1;

$queryString_result_commRent = "";
if (!empty($_SERVER['QUERY_STRING'])) {
  $params = explode("&", $_SERVER['QUERY_STRING']);
  $newParams = array();
  foreach ($params as $param) {
    if (stristr($param, "pageNum_result_commRent") == false && 
        stristr($param, "totalRows_result_commRent") == false) {
      array_push($newParams, $param);
    }
  }
  if (count($newParams) != 0) {
    $queryString_result_commRent = "&" . htmlentities(implode("&", $newParams));
  }
}
$queryString_result_commRent = sprintf("&totalRows_result_commRent=%d%s", $totalRows_result_commRent, $queryString_result_commRent);
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/mainPage_template.dwt.php" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Castletgate3</title>
<!-- InstanceEndEditable -->
<script type="text/javascript">
function goBack()
  {
  window.history.go(-1)
  }
</script>

<link href="../styles/basic.css" rel="stylesheet" type="text/css" media="screen" />

<style type="text/css">
<!--
.style1 {color: #003366}
.style11 {font-size: 18px}
.style2 {
background-color: #ACB0CA;
}
.style3 {
background-color: #B3D78A;
}	
.style4 {
background-color: #DBA399;
} 
.style12 {font-size: 24px}
-->
</style>

<!-- InstanceBeginEditable name="head" -->
<style type="text/css">
<!--
.style13 {font-size: 16px}
-->
</style>
<!-- InstanceEndEditable -->
</head>

<body>


<div id="wrapper">
<!--wrapper-->

<div id="header">
<!--header-->
 <div id="header_image"><img src="../images/castlegate.jpg" alt="Castlegate3 Logo" width="645" height="75" /></div>	
 <div id="header_ad"><img src="../images/header_ad.gif" width="200" height="75" /></div>  
 <div id="time"> 
	<p align="center"> 
	<script type="text/javascript"> 
	var d = new Date() 
	var time = d.getHours() 

	if (time < 12)  
	{ 
	document.write("Good Morning!") 
	} 
	else if (time>=12 && time<18)  
	{ 
	document.write("Good Afternoon!") 
	} 
	else 
	{ 
	document.write("<Good Evening!") 
	} 
	</script>  
	<?php 
	$date = date('l F j, Y');
	echo $date; 
	?>
	at
	<?php 
	putenv("TZ=America/New_York"); 
	$time = date('g:ia'); 
	echo $time;
	?>
	</p>
</div>

</div>
<!--end header-->
<div class="colmask threecol">
  <div class="colmid">
    <div class="colleft"><!-- InstanceBeginEditable name="Content" -->
      <div class="col1">
        <!-- middle -->
          <p align="center"><?php echo $row_result_commRent['title']; ?></p>
        <table width="400" border="2" align="center" cellpadding="4">
		<tr>
            <td>Property Type: </td>
		<td><?php echo $row_result_commRent['type']; ?></td>
           </tr>
          <tr>
            <td>City:</td>
            <td><?php echo $row_result_commRent['city']; ?>, <?php echo $row_result_commRent['state']; 				?></td>
          </tr>
          <tr>
            <td>Price:</td>
            <td><?php echo $row_result_commRent['price']; ?></td>
          </tr>
          <tr>
            <td>Terms:</td>
            <td><?php echo $row_result_commRent['terms']; ?></td>
          </tr>
          <tr>
            <td>Description:</td>
            <td><?php echo $row_result_commRent['description']; ?></td>
          </tr>
          <tr>
            <td>Contact #: </td>
            <td><?php echo $row_result_commRent['contact']; ?></td>
          </tr>
          <tr>
            <td>Email:</td>
            <td><?php echo $row_result_commRent['email']; ?></td>
          </tr>
          
          <tr>
            <td colspan="2"><div align="center">Photo should be inserted here </div></td>
          </tr>
        </table>
	   <p> </p>
	  <input type="button" value="Back" onclick="goBack()" />
	   <p> </p>
      </div>
    <!-- InstanceEndEditable -->
      <!-- end middle -->
<div class="col2">
        <!-- left -->
	<div id="nav_home">
	   <p align="center" class="style9"><a href="/index.php" class="style12">Home</a></p>
	 </div>
        <div id="property_listing">
          <h3 align="center" class="style1"><strong> Add a Property Listing </strong></h3>
          <hr />
          <div id=commbg><li><a href="../commercial_add.php">Commercial </a></li></div>
          <div id=resbg><li><a href="../residential_add.php">Residential </a></li></div>
          <div id=vacbg><li><a href="../vacation_add.php"> Vacation </a></li></div>
        </div>
        <div id="tips">
          <h4 align="center" class="style1">Tips for marketing your property</h4>
          <p align="center"><a href="../marketing.php">Click Here</a></p>
        </div>
        <div id="info">
          <h4 align="center" class="style1">Information for first time buyers</h4>
          <p align="center"><a href="../info.php">Click Here</a></p>
        </div>
        <div id="left_ad">
          <p><img src="../images/ad_left.jpg" alt="Advertisement" width="230" height="75" /></p>
        </div>
      </div>
      <!-- end left -->
<div class="col3">
        <!-- right -->
        <div id="right_ads">
	 <img src="../images/ad_right_top.jpg" alt="Advertisment" width="125" height="250" />           	       
	 <img src="../images/ad_right_bottom.jpg" alt="Advertisment" width="125" height="250" /> 
	 </div>
      </div>
      <!-- end right -->
    </div>
  </div>
</div>
<div id="bottom_adspace">
 <div id="left">
   	  <img src="../images/ad_bottom_left.jpg" alt="Advertisment" width="400" height="100" />
     </div>
 <div id="right">	   
     <img src="../images/ad_bottom_right.jpg" alt="Adverstisment" width="400" height="100" />     </div>
</div>

<div id="footer">
<!-- footer -->
<div class="innerbox_footer">
<ul>
   <li><a href="../about.php"> About Us </a></li>
   <li><a href="../terms.php"> Terms of Use </a></li>
   <li><a href="../privacy.php"> Privacy Policy </a></li>
   <li><a href="../faq.php"> FAQ's </a></li>
   <li> <a href="../disclaimer.php">Disclaimer</a> </li>
   <li> <a href="../contact.php">Contact Us </a></li>	
</ul>      
</div>
</div>
<!-- end footer -->	 

</div>
<!-- end wrapper -->


</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result($result_commRent);
?>

[/code]

in order to save the path of the image in the database you need to do the following:

 

1st. if the path(folder) is static assign the photo variable a value like: $photo = "path/foldername/". $imagename;

2nd. store that variable in your database.

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.