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_desc,price) values('$id','$name',$short_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']=="delete")
{
$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

error77Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

maeltarCommented:
Rather than copy and pasting, please follow this page : http://www.w3schools.com/php/php_file_upload.asp

As a side note, please use a little security on your variables to prevent injection  :

$action=mysql_real_escape_string($_POST['action']);
$id=mysql_real_escape_string($_POST['id']);
$name=mysql_real_escape_string($_POST['name']);
$price=mysql_real_escape_string($_POST['price']);
$short_desc=mysql_real_escape_string($_POST['short_desc']);

Open in new window


http://www.tizag.com/mysqlTutorial/mysql-php-sql-injection.php
0
error77Author Commented:
I've been on that url already before.

I was hoping to properly merge upload functionality into my current code.
0
error77Author Commented:
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
0
OWASP: Forgery and Phishing

Learn the techniques to avoid forgery and phishing attacks and the types of attacks an application or network may face.

maeltarCommented:
What is the type of file you are trying to upload ?
0
error77Author Commented:
An Image ... a .jpg ...but I want to be able to use ...jpg, gif and png.

0
maeltarCommented:
it woudl appear to get the message you are getting the file has failed one or more of the checks in the following if statement :

if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
error77Author Commented:
Its not too helpful the message :o/ Any ideas of how to debug this?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.