Jump to content


Photo

uploading an image and inserting values at the same time


  • Please log in to reply
4 replies to this topic

#1 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 22 August 2006 - 01:30 PM

I'm trying to insert values into a datbase on products and upload an image of it at the same time. I am able to insert all of the values in no problem when i do not try to upload and I can upload the image if I just click on browse and upload and not insert other values. The reason I am running into problems is because I used a seperate script for my upload to that of the insert. My upload code is in a file called doupload.php

<?php

$file_dir = '/home/c/ciaracousins/public_html/clothes/';
// Get the image..
$image = $HTTP_POST_FILES['image'];
// Verify for erros
if ( $image['error'] == 0 )
{
    // Ok, next: is it uploaded?
    if ( is_uploaded_file ( $image['tmp_name'] ))
    {
        // Copy it to folder and check
        if ( move_uploaded_file ( $image['tmp_name'] , $file_dir . $image['name'] ))
        {
            // Ok, done
            # do something here, use header.. java redirection.. don't know, use your imagination..
            print 'Done! The file was uploaded!';
        }
    }
}
?>

And my insert code code is in a file called admin_insert.php

<? 

//connect to server and select database


include "db.php";



$submit=$HTTP_POST_VARS["submit"];
$reset=$HTTP_POST_VARS["reset"];
 
$shopName=$HTTP_POST_VARS["shopName"];
$prodName=$HTTP_POST_VARS["prodName"];
$dept=$HTTP_POST_VARS["dept"];
$brand=$HTTP_POST_VARS["brand"];
$type=$HTTP_POST_VARS["type"];
$price=$HTTP_POST_VARS["price"];
$NoInStock=$HTTP_POST_VARS["NoInStock"];
$image=$HTTP_POST_VARS["image"];
 

if (!$shopName || !$prodName || !$dept || !$brand || !$type 
	 || !$price || !$NoInStock || !$image){
	echo "Please fill all the fields";
	exit;


}


/* Performing SQL query */

$query = "select * from product where prodName = '$prodName'";

//$result = mysql_query($query,$connect);

require_once('db.php');


	$query = "INSERT INTO product Values ('','$shopName','$prodName', '$dept', '$brand', '$type', '$image', '$price', '$NoInStock')";
	$result = mysql_query($query);

	if ($result){
		echo "data inserted";
		
	}
	else{
		echo "This product has already been entered !!!";
	}
	/* Free resultset */
	

	/* Closing connection */
	mysql_close();

?>

The values are entered in through a page called admin_insert.htm

<form action="admin_insert.php" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="51200" />

 <tr>
    <td align="center"><p style="color: #FF6600; font size=24"><strong>Add New Records</strong></p></td></tr>
  <tr><td></td></tr>
<table align="left" width="100%" border="0">
 
  <tr>
    <td align="right" style="color: #006699;">
      <strong>Shop Name:</strong>
    </td>
    <td>
      <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="shopName" />
    </td>
  </tr>
  <tr>
    <td align="right" style="color: #006699;">
      <strong>Product Name:</strong>
    </td>
    <td>
      <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="prodName" />
    </td>
  </tr>
  <tr>
    <td align="right" style="color: #006699;">
      <strong>Department:</strong>
    </td>
    <td>
      <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="dept" />
    </td>
  </tr>
  <tr>
    <td align="right" style="color: #006699;">
      <strong>brand:</strong>
    </td>
    <td>
      <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="brand" />
    </td>
  </tr>
  <tr>
    <td align="right" style="color: #006699;">
      <strong>Type:</strong>
    </td>
    <td>
      <input maxLength="45" size="45" name="type" />
    </td>
  </tr>
<tr>
    <td align="right" style="color: #006699;">
      <strong>Price:</strong>
    </td>
    <td>
      <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="price" />
    </td>
  </tr>
<tr>
    <td align="right" style="color: #006699;">
      <strong>Quantity:</strong>
    </td>
    <td>
      <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="NoInStock" />
    </td>
  </tr>

<!--<tr>
    <td align="right" style="color: #006699;">
      <strong>File to Upload:</strong>
    </td>
    <td>
      <input onfocus="ShowPwWarning()" maxLength="45" size="45" name="image" />
    </td>
  </tr>-->

 <tr>
    <td align="right" style="color: #006699;">
      <strong>File to Upload:</strong>
    </td>
    <td>
      <input type="text" name="image" />
      <!--<input type="submit" value="Upload!" />-->
    </td>
  </tr>
  <tr>
    <td></td>
    <td align="left" colspan="2">
      <input type="submit" value="Add Record" name=submit />
      <input type=reset value=Reset name=reset />
    </td>
  </tr>
</table>
</form>
			

                      
                    </TBODY>
                  </TABLE>
                </FORM>

Firstly I am having  problems with the upload part. If I have <input type="file" name="image" /> it keeps saying to enter all fields. It is not recognising what is being entered because image is set to be a text value in the database, because I am not storing the image in the db, I am trying to upload the image to the server and then just store its path in the db.

Also I think I need to combine my doupload.php and my admin_insert.php but am not sure how.

Any ideas?? (please try and dumb your answers down for me as only a beginner!)


#2 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 22 August 2006 - 01:38 PM

Just looking over my post there I think I might actually need to just have a seperate field for image name, because in my db i only store the name of the image, not the full path. When I want to display an image I just append the name of the image to the path of where it is stored, eg:

$imageDir = "http://snet.wit.ie/~ciaracousins/clothes/";
$imagePath = $imageDir."".$image;

So I think what I need to do is just enter the image name in myself when entering the details, and just upload the image to the server without storing the path anywhere.

#3 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 22 August 2006 - 02:10 PM

if($_FILES["ufile"]["name"](
$dir = "images/".$_FILES["ufile"]["name"];
if(move_uploaded_file($_FILES["ufile"]["name"], $dir){
echo "File has been uploaded

$query = "INSERT INTO table SET image = '".$dir."'";
$result = mysql_query($query);

}else{
echo "The file failed to uploaded";
}
}else{
echo "No File";
}

NOTE: this is uing
<input type='file' name='ufile' />
Tell me the problem, I will try tell you the solution

#4 kikilahooch

kikilahooch
  • Members
  • PipPipPip
  • Advanced Member
  • 72 posts

Posted 22 August 2006 - 02:51 PM

Thanks!

I put that code in under $image=$HTTP_POST_VARS["image"];

and changed the dir to my own and changed the name of the file to ufile in the admin_insert.htm
Is that what I should have done??

Now when I run that code I get the msg

Parse error: parse error, unexpected ';' in /home/c/ciaracousins/public_html/admin_insert.php on line 23

line 23 is $dir = "clothes/".$_FILES["ufile"]["name"];

Are the brackets all there or the right way round??

As you prob realise from yesterday I dont know what I'm doing so sorry if these are silly Q's

#5 onlyican

onlyican
  • Members
  • PipPipPip
  • Advanced Member
  • 921 posts
  • LocationHants - UK

Posted 22 August 2006 - 04:04 PM

ufile is the input name
and that script is the complete script (not secure) for uploading
Tell me the problem, I will try tell you the solution




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users