error77
asked on
PHP Add Image Upload to Existing Form
Hi all,
I need to add image upload functionality to my existing form.
Can anyone help?
Here is the current code:
//index.php
//form part
<form method="post" name="formBarang" action="process.php" id="formBarang">
<table width="400">
<tr>
<td>ID</td><td><input type="text" name="id" size="10" value="<?php echo $id;?>" readonly="Yes" /></td>
</tr>
<tr>
<td>Name</td><td><input type="text" name="name" size="50" value="<?php echo $name;?>" /></td>
</tr>
<tr>
<td>Short Description</td><td><input type="text" name="short_desc" size="50" value="<?php echo $short_desc;?>" /></td>
</tr>
<tr>
<td>Price</td><td><input type="text" name="price" size="10" value="<?php echo $price;?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="<?php echo $status;?>"</td>
</tr>
</table>
<input type="hidden" name="action" value="<?php echo $action;?>" />
</form>
//Process.php
<?php
include("conn.php");
if(isset($_POST))
{
$action=$_POST['action'];
$id=$_POST['id'];
$name=$_POST['name'];
$price=$_POST['price'];
$short_desc=$_POST['short_ desc'];
}
if($action=="new")
{
$check=mysql_query("select * from tbl_barang where id='$id'");
if(mysql_num_rows($check)= =0)
{
mysql_query("insert into tbl_barang(id,name,short_d esc,price) values('$id','$name',$shor t_desc','$ price')") or die("data cannot be inserted");
echo 1;
}
else
{
echo "code cannot be nothing";
}
exit;
}
elseif($action=="update")
{
mysql_query("update tbl_barang set name='$name',short_desc='$ short_desc ',price='$ price' where id='$id'") or die ("data cannot be updated");
echo 1 ;
exit;
}
elseif($_GET['action']=="d elete")
{
$id=intval($_GET['id']);
mysql_query("delete from tbl_barang where id='$id'")or die("Cannot Delete record");
echo 1;
exit;
}
?>
Hope someone can help.
Thanks
I need to add image upload functionality to my existing form.
Can anyone help?
Here is the current code:
//index.php
//form part
<form method="post" name="formBarang" action="process.php" id="formBarang">
<table width="400">
<tr>
<td>ID</td><td><input type="text" name="id" size="10" value="<?php echo $id;?>" readonly="Yes" /></td>
</tr>
<tr>
<td>Name</td><td><input type="text" name="name" size="50" value="<?php echo $name;?>" /></td>
</tr>
<tr>
<td>Short Description</td><td><input
</tr>
<tr>
<td>Price</td><td><input type="text" name="price" size="10" value="<?php echo $price;?>" /></td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="<?php echo $status;?>"</td>
</tr>
</table>
<input type="hidden" name="action" value="<?php echo $action;?>" />
</form>
//Process.php
<?php
include("conn.php");
if(isset($_POST))
{
$action=$_POST['action'];
$id=$_POST['id'];
$name=$_POST['name'];
$price=$_POST['price'];
$short_desc=$_POST['short_
}
if($action=="new")
{
$check=mysql_query("select
if(mysql_num_rows($check)=
{
mysql_query("insert into tbl_barang(id,name,short_d
echo 1;
}
else
{
echo "code cannot be nothing";
}
exit;
}
elseif($action=="update")
{
mysql_query("update tbl_barang set name='$name',short_desc='$
echo 1 ;
exit;
}
elseif($_GET['action']=="d
{
$id=intval($_GET['id']);
mysql_query("delete from tbl_barang where id='$id'")or die("Cannot Delete record");
echo 1;
exit;
}
?>
Hope someone can help.
Thanks
ASKER
I've been on that url already before.
I was hoping to properly merge upload functionality into my current code.
I was hoping to properly merge upload functionality into my current code.
ASKER
I've tried adding that code from your suggested link:
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name" ] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES ["file"][" tmp_name"] ,
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
and I get this error:
<br />
<b>Notice</b>: Undefined index: file in <b>C:\wamp\www\proc.php</b > on line <b>2</b><br />
<br />
<b>Notice</b>: Undefined index: file in <b>C:\wamp\www\proc.php</b > on line <b>3</b><br />
<br />
<b>Notice</b>: Undefined index: file in <b>C:\wamp\www\proc.php</b > on line <b>4</b><br />
Invalid file
<?php
if ((($_FILES["file"]["type"]
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
and I get this error:
<br />
<b>Notice</b>: Undefined index: file in <b>C:\wamp\www\proc.php</b
<br />
<b>Notice</b>: Undefined index: file in <b>C:\wamp\www\proc.php</b
<br />
<b>Notice</b>: Undefined index: file in <b>C:\wamp\www\proc.php</b
Invalid file
What is the type of file you are trying to upload ?
ASKER
An Image ... a .jpg ...but I want to be able to use ...jpg, gif and png.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Its not too helpful the message :o/ Any ideas of how to debug this?
As a side note, please use a little security on your variables to prevent injection :
Open in new window
http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php