Jump to content


uploading image and inserting path in db?


5 replies to this topic

#1 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 07 January 2006 - 05:02 PM

Ok I made a shopping cart and this function adds the product to the store, but the image wont go into the database. All I want is the url of the image but it just inserts /images/items/ , heres my function.

function productAdd() {
// Add the product to the store
	$cat		 = $_POST["cat"];
	$name		= $_POST["name"];
	$description = $_POST["description"];
	$price	   = str_replace(',', '', (double)$_POST["price"]);
	$quantity	= (int)$_POST["quantity"];
	
	// Set where you want to store files
	$image = "images/items/".$_FILES["file"]["name"][0];
	$thumb = "images/items/".$_FILES["file"]["name"][1];
	copy($_FILES["file"]["tmp_name"][0], $image);
	copy($_FILES["file"]["tmp_name"][1], $thumb);
	
	$query  = "INSERT INTO store_product (id_cat, name, description, price, quantity, image_large, image_thumb)
			  VALUES ('$cat', '$name', '$description', '$price', '$quantity', '$image', '$thumb')";
	$result = db_query($query);	
}


#2 Jaymz

    Retired P2L Staff

  • Members
  • PipPipPipPip
  • 4,104 posts

Posted 07 January 2006 - 06:29 PM

If it's any help, I use this on my site and it works perfectly:

$file = $_FILES['file']['name'];


#3 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 07 January 2006 - 07:39 PM

Jaymz, i believe he's trying to upload multiple files, so what he's using should work (in theory).

Could you possible show us the html form you're using? To make sure you have the name correct.
Or you could check and see if it is right your self:
die(highlight_string(print_r($_FILES,1),1));
Simply at that to the top of your function, it will show you want is (or is not) in $_FILES.

Edited by rc69, 07 January 2006 - 07:40 PM.


#4 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 07 January 2006 - 08:51 PM

Heres my form

/////////////////
		// Add Product //
		/////////////////
		case "add":
		
		// Get the category id
		$id = (isset($_GET["parent"]) && $_GET["parent"] > 0) ? $_GET["parent"] : 0;
		$list = buildCategoryOptions(); ?>
		
		<form action="admin.php?page=products&amp;do=process&amp;go=add" method="post" enctype="multipart/form-data" onsubmit="return checkProduct(this);">
		<table cellpadding="5" cellspacing="2" border="0">
			<tr>
				<td colspan="2"><h1>Store Products</h1></td>
			</tr>
			<tr>
				<td><a href="javascript:history.go(-1)">Go back to previous page?</a></td>
				<td>You are adding a new product into the store, please input a name and description for it.</td>
			</tr>
			<tr>
				<td>Name:</td>
				<td><input type="text" name="name" id="name" size="35" /></td>
			</tr>
			<tr>
				<td>Category:</td>
				<td><select name="cat" id="cat">
					<? echo $list; ?>
					</select>
				</td>
			</tr>
			<tr>
				<td>Price:</td>
				<td><input type="text" name="price" id="price" size="35" maxlength="7" /></td>
			</tr>
			<tr>
				<td>Quantity:</td>
				<td><input type="text" name="quantity" id="quantity" size="35" maxlength="5" /></td>
			</tr>
			<tr valign="top">
				<td>Description:</td>
				<td><textarea name="description" id="description" cols="50" rows="5"></textarea></td>
			</tr>
			<tr>
				<td>Image:</td>
				<td><input type="file" name="image[]" id="image[]" size="35" /></td>
			</tr>
			<tr>
				<td>Thumbnail:</td>
				<td><input type="file" name="image[]" id="image[]" size="35" /></td>
			</tr>
			<tr>
				<td><input type="hidden" name="MAX_FILE_SIZE" value="100000" /></td>
				<td><input type="submit" name="submit" id="submit" value="Add Product" /> 
					<input type="button" name="cancel" id="cancel" value="Cancel" onclick="javascript:history.go(-1)" />  
 				</td>
			</table>
			</form>

Ill try your suggestion shortly.

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 08 January 2006 - 01:50 AM

Well, if you've tried my suggestion, and you have found the answer yet... here it is:
			<tr>
				<td>Image:</td>
				<td><input type="file" name="image[]" id="image[]" size="35" /></td>
			</tr>
			<tr>
				<td>Thumbnail:</td>
				<td><input type="file" name="image[]" id="image[]" size="35" /></td>
			</tr>
So you need to change your php to this:
	$image = 'images/items/'.$_FILES['image']['name'][0];
	$thumb = 'images/items/'.$_FILES['image']['name'][1];
Also note, you're using invalid html. The "id" attribute specifies a unique id 1 element on a page. Giving 2 or more elements the same id is not "proper." If you're not using JS or CSS to do something to your <input>'s, then you don't even need to give them an id, you can remove them all and the only thing it will change is how much bandwidth you use :ph34r: (unless you're shooting for valid xhtml, in which case i still don't think you need to specify an id for them)

Edited by rc69, 08 January 2006 - 01:55 AM.


#6 tiki

    Young Padawan

  • Members
  • Pip
  • 259 posts
  • Gender:Male
  • Location:California

Posted 08 January 2006 - 04:04 AM

Oh wow im retarded, I forgot to change the php file to image after I changed the html :) meh but ya I was going for valid xhtml, ill go try this.

Edit: Tried it and it worked, thanks alot, stupid mistake :ph34r:





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users