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?
 
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
 
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
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

 
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
 
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
 
error77Author Commented:
Its not too helpful the message :o/ Any ideas of how to debug this?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.