I am a nood forgive me if all my code is messy, and evrything I know is self taught so my knowledge is not that great.

I have an issue, I created an insert record form and in it there is title, price, 8 file upload fields and other fields.

every thing somewhat works fine, but the file upload has problems.

if I input images into all the 8 file fields and submit the form there is no problem, but if i input 7 or 6 or less, i.e leave one or more file field blank and then try to submit the form, I get an error message (Notice: Undefined variable: insertSQL ...... line 126, Query was empty) and line 126 is

$Result1 = mysql_query($insertSQL, $val_nija_estate) or die(mysql_error());

I am using dreamweaver

And I m not so keen on using the blob method of uploading, i want to save the images on the server.

this is the code for the whole thing

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

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_listing")) {
	
	
		$name = basename($_FILES['pro_image_1']['name']);
	$ext = substr(strrchr($name, "."), 1);
	$randName = md5(rand() * time());
	$finalfile1 = $randName . '.' . $ext;	
		$t_name = $_FILES['pro_image_1']['tmp_name'];
		$dir = '../../images/properties/images/';
	 	if(move_uploaded_file($t_name,$dir."/".$finalfile1))
		
		$name = basename($_FILES['pro_image_2']['name']);
	$ext = substr(strrchr($name, "."), 1);
	$randName = md5(rand() * time());
	$finalfile2 = $randName . '.' . $ext;	
		$t_name = $_FILES['pro_image_2']['tmp_name'];
		$dir = '../../images/properties/images/';
	 	if(move_uploaded_file($t_name,$dir."/".$finalfile2))
		
		$name = basename($_FILES['pro_image_3']['name']);
	$ext = substr(strrchr($name, "."), 1);
	$randName = md5(rand() * time());
	$finalfile3 = $randName . '.' . $ext;	
		$t_name = $_FILES['pro_image_3']['tmp_name'];
		$dir = '../../images/properties/images/';
	 	if(move_uploaded_file($t_name,$dir."/".$finalfile3))
		
		$name = basename($_FILES['pro_image_4']['name']);
	$ext = substr(strrchr($name, "."), 1);
	$randName = md5(rand() * time());
	$finalfile4 = $randName . '.' . $ext;	
		$t_name = $_FILES['pro_image_4']['tmp_name'];
		$dir = '../../images/properties/images/';
	 	if(move_uploaded_file($t_name,$dir."/".$finalfile4))
		
		$name = basename($_FILES['pro_image_5']['name']);
	$ext = substr(strrchr($name, "."), 1);
	$randName = md5(rand() * time());
	$finalfile5 = $randName . '.' . $ext;	
		$t_name = $_FILES['pro_image_5']['tmp_name'];
		$dir = '../../images/properties/images/';
	 	if(move_uploaded_file($t_name,$dir."/".$finalfile5))
		
		$name = basename($_FILES['pro_image_6']['name']);
	$ext = substr(strrchr($name, "."), 1);
	$randName = md5(rand() * time());
	$finalfile6 = $randName . '.' . $ext;	
		$t_name = $_FILES['pro_image_6']['tmp_name'];
		$dir = '../../images/properties/images/';
	 	if(move_uploaded_file($t_name,$dir."/".$finalfile6))
		
		$name = basename($_FILES['pro_image_7']['name']);
	$ext = substr(strrchr($name, "."), 1);
	$randName = md5(rand() * time());
	$finalfile7 = $randName . '.' . $ext;	
		$t_name = $_FILES['pro_image_7']['tmp_name'];
		$dir = '../../images/properties/images/';
	 	if(move_uploaded_file($t_name,$dir."/".$finalfile7))
		
		$name = basename($_FILES['pro_image_8']['name']);
	$ext = substr(strrchr($name, "."), 1);
	$randName = md5(rand() * time());
	$finalfile8 = $randName . '.' . $ext;	
		$t_name = $_FILES['pro_image_8']['tmp_name'];
		$dir = '../../images/properties/images/';
	 	if(move_uploaded_file($t_name,$dir."/".$finalfile8))
						
  $insertSQL = sprintf("INSERT INTO property_listings_db (property_listing_title, pro_image_1, pro_image_2, pro_image_3, pro_image_4, pro_image_5, pro_image_6, pro_image_7, pro_image_8, bathrooms, bedrooms, state_ID, city_ID, type_ID, property_listing_price, long_description, short_description) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['property_listing_title'], "text"),
                       GetSQLValueString($finalfile1, "text"),
                       GetSQLValueString($finalfile2, "text"),
                       GetSQLValueString($finalfile3, "text"),
                       GetSQLValueString($finalfile4, "text"),
                       GetSQLValueString($finalfile5, "text"),
                       GetSQLValueString($finalfile6, "text"),
                       GetSQLValueString($finalfile7, "text"),
                       GetSQLValueString($finalfile8, "text"),
                       GetSQLValueString($_POST['bathrooms'], "text"),
                       GetSQLValueString($_POST['bedrooms'], "text"),
                       GetSQLValueString($_POST['state_ID'], "int"),
                       GetSQLValueString($_POST['city_ID'], "int"),
                       GetSQLValueString($_POST['type_ID'], "int"),
                       GetSQLValueString($_POST['property_listing_price'], "double"),
                       GetSQLValueString($_POST['long_description'], "text"),
                       GetSQLValueString($_POST['short_description'], "text"));

  mysql_select_db($database_ireland_estate, $ireland_estate);
  $Result1 = mysql_query($insertSQL, $ireland_estate) or die(mysql_error());

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

and this is the form

<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="add_listing" id="add_listing">
          <table width="600" border="0" align="center" cellpadding="10" cellspacing="0">
            <tr>
              <td width="136" align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Property Title:</td>
              <td width="424"><span id="sprytextfield1">
                <label for="property_listing_title"></label>
                <input name="property_listing_title" type="text" id="property_listing_title" size="50" maxlength="100" />
                <span class="textfieldRequiredMsg">*</span></span></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Primary Image:</td>
              <td><label for="pro_image_1"></label>
                <input name="pro_image_1" type="file" id="pro_image_1" size="50" /></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Image 1:</td>
              <td><label for="pro_image_2"></label>
                <input name="pro_image_2" type="file" id="pro_image_2" size="50" /></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Image 2:</td>
              <td><label for="pro_image_3"></label>
                <input name="pro_image_3" type="file" id="pro_image_3" size="50" /></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Image 3:</td>
              <td><label for="pro_image_4"></label>
                <input name="pro_image_4" type="file" id="pro_image_4" size="50" /></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Image 4:</td>
              <td><label for="pro_image_5"></label>
                <input name="pro_image_5" type="file" id="pro_image_5" size="50" /></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Image 5:</td>
              <td><label for="pro_image_6"></label>
                <input name="pro_image_6" type="file" id="pro_image_6" size="50" /></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Image 6:</td>
              <td><label for="pro_image_7"></label>
                <input name="pro_image_7" type="file" id="pro_image_7" size="50" /></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Image 7:</td>
              <td><label for="pro_image_8"></label>
                <input name="pro_image_8" type="file" id="pro_image_8" size="50" /></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>State:</td>
              <td><span id="spryselect1">
                <label for="state_ID"></label>
                <select name="state_ID" id="state_ID">
                  <option value="">Select a State</option>
                  <?php
do {  
?>
                  <option value="<?php echo $row_rs_States['state_ID']?>"><?php echo $row_rs_States['state_name']?></option>
                  <?php
} while ($row_rs_States = mysql_fetch_assoc($rs_States));
  $rows = mysql_num_rows($rs_States);
  if($rows > 0) {
      mysql_data_seek($rs_States, 0);
	  $row_rs_States = mysql_fetch_assoc($rs_States);
  }
?>
                </select>
                <span class="selectRequiredMsg">*</span></span></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>City:</td>
              <td><span id="spryselect2">
                <label for="city_ID"></label>
                <select name="city_ID" id="city_ID">
                  <option value="">Select a City</option>
                  <?php
do {  
?>
                  <option value="<?php echo $row_rs_city['city_ID']?>"><?php echo $row_rs_city['city_name']?></option>
                  <?php
} while ($row_rs_city = mysql_fetch_assoc($rs_city));
  $rows = mysql_num_rows($rs_city);
  if($rows > 0) {
      mysql_data_seek($rs_city, 0);
	  $row_rs_city = mysql_fetch_assoc($rs_city);
  }
?>
                </select>
                <span class="selectRequiredMsg">*</span></span></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Type:</td>
              <td><span id="spryselect3">
                <label for="type_ID"></label>
                <select name="type_ID" id="type_ID">
                  <option value="">Select a Type</option>
                  <?php
do {  
?>
                  <option value="<?php echo $row_rs_type['type_ID']?>"><?php echo $row_rs_type['type_name']?></option>
                  <?php
} while ($row_rs_type = mysql_fetch_assoc($rs_type));
  $rows = mysql_num_rows($rs_type);
  if($rows > 0) {
      mysql_data_seek($rs_type, 0);
	  $row_rs_type = mysql_fetch_assoc($rs_type);
  }
?>
                </select>
                <span class="selectRequiredMsg">*</span></span></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Price:</td>
              <td><span id="sprytextfield2">
                <label for="property_listing_price"></label>
                <input type="text" name="property_listing_price" id="property_listing_price" />
                <span class="textfieldRequiredMsg">*</span></span></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Number of Bedrooms:</td>
              <td><span id="sprytextfield3">
                <label for="bedrooms"></label>
                <input type="text" name="bedrooms" id="bedrooms" />
                <span class="textfieldRequiredMsg">*</span></span></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Number of Bathrooms:</td>
              <td><span id="sprytextfield4">
                <label for="bathrooms"></label>
                <input type="text" name="bathrooms" id="bathrooms" />
                <span class="textfieldRequiredMsg">*</span></span></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Long Description:</td>
              <td><label for="long_description"></label>
                <textarea name="long_description" id="long_description" cols="50" rows="10"></textarea></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap" class="p_for_index"><span class="contact_form_text">*</span>Short Description:</td>
              <td><label for="short_description"></label>
                <textarea name="short_description" id="short_description" cols="50" rows="5"></textarea></td>
              </tr>
            <tr>
              <td align="right" nowrap="nowrap">&nbsp;</td>
              <td><input type="submit" name="insert" id="insert" value="Add Listing" /></td>
              </tr>
            </table>
          <input type="hidden" name="MM_insert" value="add_listing" />
        </form>

Thank you for reading through it all, any help is appreciated,

Recommended Answers

All 3 Replies

Your insert SQL statement is always expecting there to be 8 images and so the undefined variables are the empty files being found. Your SQL needs to be more dynamic, counting the number of files you have prior to the SQL statement.

Here is a working example of a dynamic SQL statement which does what you require:

http://www.dynamicdrive.com/forums/showthread.php?t=34294

Hope that helps

He's right i would start spliting it out to be more flexible, thats way too much repeating yourself.

as a quick fix you could try changing these lines:

if(move_uploaded_file($t_name,$dir."/".$finalfile1))

to

@move_uploaded_file($t_name,$dir."/".$finalfile1)

for all 8 files, them if's are not enclosed like

if(){

}
so it is likely skipping the next line if it fails to pass, eg. if image 8 fails to move insertSQL doesnt get ran.


Heres how i would re-write your uploading file to be more manageable:

First off i would restructure your images out into a second table, so properties can have no images or 80 images doesnt matter and can be as scaleable as you want.

property_listings_db 

propertyid, property_listing_title, bathrooms, bedrooms, state_ID, city_ID, type_ID, property_listing_price, long_description, short_description
images

imageid,propertyid,imagetitle,imagedata
1,1,prop1img,rf34wferg434tr
2,1,prop1img2,dgrw45tgdrgw54
3,2,prop2img1,23423423545345
....

and the php

$editFormAction = $_SERVER['PHP_SELF'];
if(isset($_SERVER['QUERY_STRING'])){
	$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
function uploadImage($dbcon,$uploadName,$propid){
	$name = basename($_FILES[$uploadName]['name']);
	$ext = substr(strrchr($name, "."), 1);
	$randName = md5(rand() * time());
	$finalfile = $randName . '.' . $ext;
	$t_name = $_FILES[$uploadName]['tmp_name'];
	$dir = '../../images/properties/images/';
	$D['name'] = $name;
	if(move_uploaded_file($t_name,$dir."/".$finalfile)){
		$insertSQL = sprintf("INSERT INTO `images` (propertyid,imagetitle, imagedata) VALUES (%s, %s, %s)",
                       GetSQLValueString($propid, "text"),
                       GetSQLValueString($name, "text"),
                       GetSQLValueString($finalfile, "text"));
        if(mysql_query($insertSQL,$dbcon)){
        	$D['success'] = true;
        	$D['msg'] = '';
        }else{
        	$D['success'] = false;
        	$D['msg'] = 'Mysql Query failed: '.mysql_error($dbcon);
        }
	}else{
		$D['msg'] = 'Failed to move file: '.$t_name.' to '.$dir.'/'.$finalfile;
		$D['success'] = false;
	}
	return $D;
}
 
if((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "add_listing")){
	$insertSQL = sprintf("INSERT INTO property_listings_db (property_listing_title, bathrooms, bedrooms, state_ID, city_ID, type_ID, property_listing_price, long_description, short_description) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
	                       GetSQLValueString($_POST['property_listing_title'], "text"),
	                       GetSQLValueString($_POST['bathrooms'], "text"),
	                       GetSQLValueString($_POST['bedrooms'], "text"),
	                       GetSQLValueString($_POST['state_ID'], "int"),
	                       GetSQLValueString($_POST['city_ID'], "int"),
	                       GetSQLValueString($_POST['type_ID'], "int"),
	                       GetSQLValueString($_POST['property_listing_price'], "double"),
	                       GetSQLValueString($_POST['long_description'], "text"),
	                       GetSQLValueString($_POST['short_description'], "text"));
	if(mysql_query($insertSQL,$dbcon)){
		$newpropid = mysql_insert_id($dbcon);
		$uploads = array();
		if(ISSET($_FILES['pro_image_1']['name'])){
			$uploads[] = uploadImage($dbcon,'pro_image_1',$newpropid);
		}
		if(ISSET($_FILES['pro_image_2']['name'])){
			$uploads[] = uploadImage($dbcon,'pro_image_2',$newpropid);
		}
		if(ISSET($_FILES['pro_image_3']['name'])){
			$uploads[] = uploadImage($dbcon,'pro_image_3',$newpropid);
		}
		if(ISSET($_FILES['pro_image_4']['name'])){
			$uploads[] = uploadImage($dbcon,'pro_image_4',$newpropid);
		}
		if(ISSET($_FILES['pro_image_5']['name'])){
			$uploads[] = uploadImage($dbcon,'pro_image_5',$newpropid);
		}
		if(ISSET($_FILES['pro_image_6']['name'])){
			$uploads[] = uploadImage($dbcon,'pro_image_6',$newpropid);
		}
		if(ISSET($_FILES['pro_image_7']['name'])){
			$uploads[] = uploadImage($dbcon,'pro_image_7',$newpropid);
		}
		if(ISSET($_FILES['pro_image_8']['name'])){
			$uploads[] = uploadImage($dbcon,'pro_image_8',$newpropid);
		}
	}else{
		echo 'Error prop failed to insert: '.mysql_error($dbcon);
	}
}
if(ISSET($uploads)){
	foreach($uploads as $v){
		echo $v['name'].' '.$v['success']."<br/>\r\n";
	}
}

the advantage with functions is you can include your whole app into a function file so you always have an easy way to upload images from here on out

SORRY ALL FOR THE LATE RESPONSE, thank you StephNicolaou I tried opening that link but it wont work, even tried gettin rid of the br tags on it but no luck.

Thank you Biiim, didnt even know what includes where but thanks to you I now have an Idea. I will try the method you suggested and see what comes out, this is my first project and I am self teaching as I go.

Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.