Jump to content

Recommended Posts

Hi Guys,

 

I've got an Image Upload Script setup, But it just wont work :(

 

The code is as follows

<?php 
// create flag(s) for validation errors -
$errors = array(); // an array is generally used for this, where the index name would relate each error element to the field it corresponds to (should you want to individually output errors next to the field)

// check if the form has been submitted
if(isset($_POST['submit']))
{
//Collect Form Vars
extract($_POST);
$img = $_FILES['image1']['name'];

//Check Forms' Post Vars
  
if (!empty($_POST['carname'])) {
	$carname = $_POST['carname'];
}else{
	$carname = NULL;
	$errors['carname'] = '<p><font color="red">You need to enter the cars name</font></p>';
}

if (!empty($_POST['carsprice'])) {
	$price = $_POST['carsprice'];
}else{
	$price = NULL;
	$errors['carsprice'] = '<p><font color="red">You need to enter the cars price</font></p>';
}

//Handle the Image
//Set Images Upload Directory
$uploaddir = "../carpictures"; 
// Upload Part
if(is_uploaded_file($_FILES['image1']['tmp_name']))
{
move_uploaded_file($_FILES['image1']['tmp_name'],$uploaddir.'/'.$_FILES['image1']['name']);
}

// If everything is filled out print the message.
if(empty($errors))
{
	// If all is ok, Insert into DB
	$sql = "INSERT INTO $db_table(carname, description, price, feature1, feature2, feature3, feature4, image1) values ('$carname','$description','$price','$feature1','$feature2','$feature3','$feature4','$img')"; 
	($result = mysql_query($sql ,$db) or die(mysql_error()));
	echo "Thank you! The Car <b>$carname</b> has been added to the site";
	exit;
}
}

// if the form was not submitted or there were validation errors, display the form -
if(!isset($_POST['submit']) || !empty($errors))
{
?>

 

But the key errors are on lines 32

Line 32s' Error is : Undefined index: image1 in bla bla

$img = $_FILES['image1']['name'];

and the other error is line 54

Error being : Undefined index: image1 in bla bla

if(is_uploaded_file($_FILES['image1']['tmp_name']))

 

If anyone has any suggestions why this is happening, Please fire away, Or if they have a way to maybe to improve the code etc, Then fire away :)

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/131182-image-upload/
Share on other sites

Here is my HTML Form

<form action="addcar.php" method="post" enctype="multipart/form-data">
<h2>Add a new car</h2>               


<ul>
<li><label>Car Name : </label><input type="text" name="carname" tabindex="1" /></li>
<li><label>Cars' Description : </label><textarea name="description" cols="40" rows="5" tabindex="2"></textarea></li>
<li><label>Price : </label><input name="carsprice" type="text" tabindex="3" id="carsprice" />
<li>
<li><label>Feature 1 : </label><input name="feature1" type="text" tabindex="4" /><em>Car feature i.e ABS</em></li>
<li><label>Feature 2 : </label><input name="feature2" type="text" tabindex="5" /><em>Car feature i.e ABS</em></li>
<li><label>Feature 3 : </label><input name="feature3" type="text" tabindex="6" /><em>Car feature i.e ABS</em></li>
<li><label>Feature 4 : </label><input name="feature4" type="text" tabindex="7" /><em>Car feature i.e ABS</em></li>
<li><label>Feature 5 : </label><input name="feature5" type="text" tabindex="8" /><em>Car feature i.e ABS</em></li>
<li><label>Image 1 : </label><input type="file" name="image1" /><em>* This is the main image that will appear on the site</em></li>
<li><label>Image 2 : </label><input type="file" name="image2"  /></li>
<li><label>Image 3 : </label><input type="file" name="image3" /></li>
<li><label>Image 4 : </label><input type="file" name="image4" /></li>
<br /><br />
<li>
<input type="submit" name="submit" id="submit" value="Submit" />
<input type="reset" name="reset" id="reset" value="Reset" />
</li>
</ul>
</form>

Link to comment
https://forums.phpfreaks.com/topic/131182-image-upload/#findComment-681100
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.