...

View Full Version : Upload and insert to database



imrie
08-09-2007, 12:04 AM
Hello,

I got this code from various sources.

This doesent work:


<?php

if($_SERVER['REQUEST_METHOD'] == "POST"){

$Title=$_POST['Title'];
$Author=$_POST['Author'];




//places files into same dir as form resides
foreach ($_FILES["pictures"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
echo"error";
move_uploaded_file(
$_FILES["pictures"]["tmp_name"][$key],
$_FILES["pictures"]["name"][$key]
) or die("Problems with upload");
}
}

mysql_connect("", "imrieimag", "") or die(mysql_error()) ;
mysql_select_db("imrieimag") or die(mysql_error()) ;

$someSize = count($_FILES['pictures']['name']);
// thats the count of image/file names

for ($i = 2; $i <= $someSize; $i++)
{
$query = "INSERT INTO `" . $_POST['options'] . "` (title, author, image_file ) VALUES ('".$_POST['Title']."', '".$_POST['Author']."', '".$_FILES['userfile']['name'][$i]."' ) ";
mysql_query($query);
}

mysql_query($query);


}
?>

error:


Warning: Invalid argument supplied for foreach() in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form2.php on line 12

Notice: Undefined variable: query in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form2.php on line 34

This is the form:


<html>
<head>
<title>HTML Form for uploading image to server</title>
</head>
<body>
<form action="form2.php" method="post" enctype="multipart/form-data">
<p>Skin File:
<input type="file" name="pictures" /> <br>
Image:<input type="file" name="pictures" /> <br>
Title:<input type="text" name="Title" /><br>
Author:<input type="text" name="Author" /><br>
Category: <select name="options">
<option value="CLAAS_Cougar ">CLAAS_Cougar </option>
<option value="CLAAS_Disco ">CLAAS_Disco </option>
<option value="CLAAS_Round Baler ">CLAAS_Round Baler </option>
<option value="CLAAS_Xerion">CLAAS_Xerion</option>
<option value="Delvano_Sprayer">Delvano_Sprayer</option>
<option value="Disc_Harrow">Disc_Harrow</option>
<option value="Forage_Harvester ">Forage_Harvester </option>
<option value="Grain_Cart">Grain_Cart</option
<option value="Grain_Trailer">Grain_Trailer</option>
<option value="Header_Trailer ">Header_Trailer </option>
<option value="John_Deere_7810">John_Deere_7810</option>
<option value="John_Deere_7920 ">John_Deere_7920 </option>
<option value="John_Deere_8310">John_Deere_8310</option>
<option value="John_Deere_8310t">John_Deere_8310 T </option>
<option value="John_Deere_Corn_Header">John_Deere_Corn_Header </option>
<option value="John_Deere_Corn_Seeder">John_Deere_Corn_Seeder</option>
<option value="John_Deere_Front_Loader">John_Deere_Front_Loader </option>
<option value="John_Deere_Machine_40">John_Deere_Machine_40 </option>
<option value="John_Deere_Seeder">John_Deere_Seeder </option>
<option value="John_Deere_Square_Baler">John_Deere_Square_Baler </option>
<option value="John_Deere_STS">John_Deere_STS </option>
<option value="John_Deere_Telehandler">John_Deere_Telehandler </option>
<option value="John_Deere_Tiller">John_Deere_Tiller </option>
<option value="Lexion">Lexion</option>
<option value="Livestock_Trialer">Livestock_Trialer</option>
<option value="Manure_spreader">Manure_spreader </option>
<option value="Manure_Tanker">Manure_Tanker</option>
<option value="Miscellaneous">Miscellaneous</option>
<option value="New_Holland_NCR">New_Holland_NCR</option>
<option value="New_Holland_TG">New_Holland_TG </option>
<option value="Plough">Plough</option>
<option value="Truck">Truck</option>

</select>



Could someone please correct the error and also tell me how to make one file go into one colum of a table and the other to another colum.


Thanks in advance,

imrie
08-09-2007, 11:18 AM
I have made an improvment, this code was made from various sources and the problem is it wont put the 2nd file to the directory.


Form:


html>
<head>
<title>HTML Form for uploading image to server</title>
</head>
<body>
<form action="form3.php" method="post" enctype="multipart/form-data">
<p>Skin File:
<input type="file" name="file" /> <br>
Image:<input type="file" name="file" /> <br>
Title:<input type="text" name="Title" /><br>
Author:<input type="text" name="Author" /><br>
Category: <select name="options">
<option value="CLAAS_Cougar ">CLAAS_Cougar </option>
<option value="CLAAS_Disco ">CLAAS_Disco </option>
<option value="CLAAS_Round Baler ">CLAAS_Round Baler </option>
<option value="CLAAS_Xerion">CLAAS_Xerion</option>
<option value="Delvano_Sprayer">Delvano_Sprayer</option>
<option value="Disc_Harrow">Disc_Harrow</option>
<option value="Forage_Harvester ">Forage_Harvester </option>
<option value="Grain_Cart">Grain_Cart</option
<option value="Grain_Trailer">Grain_Trailer</option>
<option value="Header_Trailer ">Header_Trailer </option>
<option value="John_Deere_7810">John_Deere_7810</option>
<option value="John_Deere_7920 ">John_Deere_7920 </option>
<option value="John_Deere_8310">John_Deere_8310</option>
<option value="John_Deere_8310t">John_Deere_8310 T </option>
<option value="John_Deere_Corn_Header">John_Deere_Corn_Header </option>
<option value="John_Deere_Corn_Seeder">John_Deere_Corn_Seeder</option>
<option value="John_Deere_Front_Loader">John_Deere_Front_Loader </option>
<option value="John_Deere_Machine_40">John_Deere_Machine_40 </option>
<option value="John_Deere_Seeder">John_Deere_Seeder </option>
<option value="John_Deere_Square_Baler">John_Deere_Square_Baler </option>
<option value="John_Deere_STS">John_Deere_STS </option>
<option value="John_Deere_Telehandler">John_Deere_Telehandler </option>
<option value="John_Deere_Tiller">John_Deere_Tiller </option>
<option value="Lexion">Lexion</option>
<option value="Livestock_Trialer">Livestock_Trialer</option>
<option value="Manure_spreader">Manure_spreader </option>
<option value="Manure_Tanker">Manure_Tanker</option>
<option value="Miscellaneous">Miscellaneous</option>
<option value="New_Holland_NCR">New_Holland_NCR</option>
<option value="New_Holland_TG">New_Holland_TG </option>
<option value="Plough">Plough</option>
<option value="Truck">Truck</option>

</select>

<input type="submit" value="Send" />
</p>
</form>

</body>
</html>

Script:


<?php

if($_SERVER['REQUEST_METHOD'] == "POST"){
$success = true;
$type = $_FILES['file']['type'];


$Title=$_POST['Title'];
$Author=$_POST['Author'];



if ($type == "image/gif" || $type == "image/jpeg" || $type == "image/bmp") {
//upload image
if ($_FILES['file']['size'] > 500000) {
echo "Error: The image is too large!";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],"/image_uploads/" . $_FILES["file"]["name"]);
if (file_exists("PATH" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
}
} else if ($type == "application/rar" || $type == "application/zip" || $type = "application/x-zip-compressed") {
//Upload file
if ($_FILES['file']['size'] > 1500000) {
echo "Error: The file is too large!";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],"/uploads/" . $_FILES["file"]["name"]);
if (file_exists("PATH" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
}
} else {
//Invalid file type
$success = false;
echo "Error: File type $type not valid";
}

if ($success) {

mysql_connect("","imrieimag", "");
mysql_select_db("imrieimag");

$query = "INSERT INTO `" . $_POST['options'] . "` (title, author, file_name,image) VALUES ('$_POST[Title]', '$_POST[Author]', '$_FILES[file][name]','$_FILES[file][name]')";
mysql_query($query);
}
}
?>

The directory uploads is chmodded to 777.

error:

Warning: move_uploaded_file(/uploads/aw14.zip) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form3.php on line 29

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpygQIHZ' to '/uploads/aw14.zip' in /home/fhlinux164/m/mysimtractor.com/user/htdocs/form3.php on line 29


Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum