...

View Full Version : uploading an image and inserting values at the same time



ciaracous
08-22-2006, 03:00 PM
I am 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



<?
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();

?>

What I want to do is to combine the two of these so that when I click on the "Add record" button on my admin_insert.htm page, the image will upload to the server and the values entered will be stored to the database.

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>


I am also 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.

Can anyone help me with this?
Thanks

boeing747fp
08-22-2006, 04:19 PM
your "please fill all the fields" error is because $image is being told to find $_POST['image']..... the "image" field should be called $_FILES['image']

boeing747fp
08-22-2006, 04:31 PM
try keeping the form html in a separate file and combine the rest like below

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>


admin_insert.php


<?php
if(empty($_POST['MAX_FILE_SIZE'])){
include("admin_insert.htm");
}elseif(isset($_POST['MAX_FILE_SIZE'])){
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_FILES["image"];


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


/* Performing SQL query */

$check_existence = mysql_query("select * from product where prodName = '$prodName'") or die(mysql_error());

//$result = mysql_query($query,$connect);
if(mysql_num_rows($check_existence) >0){
echo "This product has already been entered !!!";
}else{
mysql_query("INSERT INTO product Values ('','$shopName','$prodName', '$dept', '$brand', '$type', '$image', '$price', '$NoInStock')") or die(mysql_error());
echo "data inserted";
}
/* Free resultset */


/* Closing connection */
mysql_close();

$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!';
}
}
}
}
?>

ciaracous
08-22-2006, 04:41 PM
Thanks! Its now inserting all the values, only when I look at what is stored for image is just says 'array' rather than the actual path for the image.

What can I do to change that?

Also the image isnt actually uploaded on the server when I check my clothes folder where I have everything stored. It was working ok when I had it in its own page doupload.php so is there some more changes need to be made? It displays the message the file was uploaded but there is nothing being stored

boeing747fp
08-22-2006, 04:56 PM
mysql_query("INSERT INTO product Values ('','$shopName','$prodName', '$dept', '$brand', '$type', '".$file_dir.$image['name']."', '$price', '$NoInStock')") or die(mysql_error());

ciaracous
08-22-2006, 05:00 PM
yay i got it working! Thank you!! :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum