Link to home
Start Free TrialLog in
Avatar of error77
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_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

Avatar of maeltar
maeltar
Flag of United Kingdom of Great Britain and Northern Ireland image

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
Avatar of error77
error77

ASKER

I've been on that url already before.

I was hoping to properly merge upload functionality into my current code.
Avatar of error77

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
What is the type of file you are trying to upload ?
Avatar of error77

ASKER

An Image ... a .jpg ...but I want to be able to use ...jpg, gif and png.

ASKER CERTIFIED SOLUTION
Avatar of maeltar
maeltar
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of error77

ASKER

Its not too helpful the message :o/ Any ideas of how to debug this?