Insert image path problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Insert image path problem

Post by nitediver »

What's wrong with the code for upload image, this is the result in database.
[text]//Hasil : 12 Blue Six 499000 DC 42 img/Array Lorem[/text]
The value of image field "img/Array " but it supposed to be "img/name.jpg".

I cant find where the incorrect code is, anyone can help me?

Code: Select all

<?
include "function.php";
connect();

$view = mysql_query("SELECT * FROM product WHERE id='$id'");
$fetch = mysql_fetch_array($view);

if(isset($_POST['submit'])){
$prod_name = $_POST['prod_name'];
$prod_price = $_POST['prod_price'];
$prod_vendor = $_POST['prod_vendor'];
$prod_size = $_POST['prod_size'];
$prod_img = $_POST['prod_img'];
$prod_desc = $_POST['prod_desc'];

$prod_img = "img/".$_FILES['prod_img'];
//$name = $_FILES['img']['name'];
if($prod_img != none)
{
if(copy($_FILES['prod_img']['tmp_name'],$prod_img))
{
echo "ok";
}else{
echo "bad | ";
}
}


$add = mysql_query("INSERT INTO a_prod(prod_name,prod_price,prod_vendor,prod_size,prod_img,prod_desc) VALUES('$prod_name','$prod_price','$prod_vendor','$prod_size','$prod_img','$prod_desc')");

if($add){
	echo "ok";
}else{
	echo "bad";
	}
}



echo (" 
<form name=\"update\" enctype=\"multipart/form-data\" method=\"post\" action=\"$PHP_SELF\">
<pre>
name			<input type=\"text\" name=\"prod_name\"><br />
price			<input type=\"text\" name=\"prod_price\"><br />
vendor		  <input type=\"text\" name=\"prod_vendor\"><br />
description		<input type=\"text\" name=\"prod_desc\"><br />
size			<select name=\"prod_size\"><option>40</option><option>41</option><option>42</option></select>
image			<input name=\"prod_img\" type=\"file\" size=\"45\">
</pre>
<input type=\"submit\" value=\"Update\" name=\"submit\">

</form>
");
?>
roders
Forum Commoner
Posts: 68
Joined: Tue Oct 20, 2009 9:29 am

Re: Insert image path problem

Post by roders »

where it says

Code: Select all

$prod_img = "img/".$_FILES['prod_img'];
it should be

Code: Select all

$prod_img = "img/".$_FILES['prod_img']['name'];
$_FILES in itself is described as an associative array of item. Just using

Code: Select all

echo $_FILES['prod_img'];
will echo out Array
you need to specify which item of this array you want to use

Code: Select all

echo $_FILES['prod_img']['name'];
will echo the file name of the uploaded file.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Insert image path problem

Post by nitediver »

Sleepy can't see that, but I already solved that, thanks.
Post Reply