Ok so I guess there are many threads about undefined indexes.
I am trying to create a form in which a user can upload both textual information and an image. In my site its adding a watch included information and a picture into a database.
I have two questions. First one why am i getting undefined indexes in this code.
I am connecting to the database using an include include("connect.php");

<form enctype="multipart/form-data" action="" method="POST">
				<p>Watch Name: <input type="text" name="item_name"></p>
				<p>Watch Model: <input type="text" name="item_model"></p>
				<p>Item Description: <textarea id="wysiwyg" rows="11" cols="50" name="item_desc"></textarea></p>
				<p>Watch Cost (£): <input type="text" name="item_cost"></p>
				<p>Watch Quantity: <input type="text" name="item_qty"></p>
				<p>Photo: <input type="file" name="image"></p>
				<p><input name="submit" type="submit" class="box" id="upload" value=" Upload ">
				<input type="reset" name="reset" value="reset" /></p>
				</form>
				
				<?php
				$target = 'images/';
				$target = $target . basename($_FILES['image']['name']);
				
				$item_name=$_POST['item_name'];
				$item_model=$_POST['item_model'];
				$item_desc=$_POST['item_desc'];
				$item_cost=$_POST['item_cost'];
				$item_qty=$_POST['item_qty'];
				$image=($_FILES['image']['name']);
				
				
				mysql_query("INSERT INTO 'stock' VALUES ('$item_name', '$item_model', '$item_desc', '$item_cost', '$item_qty', '$image')");

				if(move_uploaded_file($_FILES['image']['tmp_name'], $target))
					{
					echo "The file ". basename($_FILES['image']['name']). "has been uploaded, and you watch has been added to the catalogue";
					}
					else {
						 echo "Sorry, you watch has not been uploaded.";
						 }
						 ?>

Second question is: Is there any way to make sure you dont get any undefined indexes.

I am fairly new to PHP. Thanks Reece

Recommended Answers

All 9 Replies

You should check

if(isset($_POST['submit']))
{

}

before php code.

Thanks arunss. Works fine. Now just to work out how to view the image on the site.

I dont suppose you know whether this code would show any results in the database. For example when I go to localhost and veiw the records I can see any information/entry/text etc in the image field. The image has been uploaded to the images folder but I cant see any record of it being linked to a certain record. Any ideas?

Thanks, Reece

Ask the person who design your database...he's the only one who can answer you why he saves record about info of your images on your database and uploaded images but doesn't link them. I think the images field should contain the source/path of the images for you to to be able to display them.

That'll be me then, this is a project im working on. First website I've made using a database and I don't really know much. Thanks for your help anyways. Ill leave this thread unsolved in case I have any other questions related to this. Im sure I will.

Hey it might seem like I'm trying to get you guys to do the work for me, but I cant for the life of me work out how to do it.

I have a field in my database called image that I want the file path of the linked image to be stored so that when I call that record the image will be displayed alongside the other information.

Is there anyone that already knows how to do this. Do I need to add another variable into the form that will add the filepath of the image into field (if that makes any sense).

Any help is appreciated. Reece

Hey problem with the original code. When I added the 'isset' yesterday everything worked fine, the picture would be added into the images folder and the data from the other fields entered into the database.

I came back to it this morning and now it doesnt add anything to the database. I have no idea why as I havent made any changes since yesterday. Please help this is pissing me off so much.

<form enctype="multipart/form-data" action="" method="POST">
				<p>Watch Name: <input type="text" name="item_name"></p>
				<p>Watch Model: <input type="text" name="item_model"></p>
				<p>Item Description: <textarea id="wysiwyg" rows="11" cols="50" name="item_desc"></textarea></p>
				<p>Watch Cost (£): <input type="text" name="item_cost"></p>
				<p>Watch Quantity: <input type="text" name="item_qty"></p>
				<p>Photo: <input type="file" name="image"></p>
				<p><input name="submit" type="submit" class="box" id="upload" value=" Upload ">
				<input type="reset" name="reset" value="reset" /></p>
				</form>
				
								
				<?php
				if(isset($_POST['submit']))
				{
				$target = 'images/';
				$target = $target . basename($_FILES['image']['name']);
				
				$item_name=$_POST['item_name'];
				$item_model=$_POST['item_model'];
				$item_desc=$_POST['item_desc'];
				$item_cost=$_POST['item_cost'];
				$item_qty=$_POST['item_qty'];
				$image=($_FILES['image']['name']);
				
				
				mysql_query("INSERT INTO 'stock' VALUES ('$item_name', '$item_model', '$item_desc', '$item_cost', '$item_qty')"); #taken off ,'$image'

				if(move_uploaded_file($_FILES['image']['tmp_name'], $target))
					{
					echo "The file ". basename($_FILES['image']['name']). " has been uploaded, and your watch has been added to the catalogue";
					}
					else {
						 echo "Sorry, your watch has not been uploaded.";
						 }}
				?>

This above code works fine for me [apart from the insert query].
Few things to remember when dealing with databases.

1. Always sanitize user's input. Never trust your users. This can be achieved using mysql_real_escape_string .
2. If you are sure that the posted value is going to be an integer, use, is_numeric .
3. Mention all the column names in your insert query. For example,

$query = "insert into table (col1, col2) values ('val1','val2')";

Doing so will save you from the headaches you might have in future. Example, You are asked to add another field to log the time of insert. Then you have to change all the scripts in which you have an insert query.

Lastly, your query is wrong. There are no ' ' around table name.

INSERT INTO stock (mention_column_names_here) VALUES ('$item_name', '$item_model', '$item_desc', '$item_cost', '$item_qty')

P.S. It's also better to store your query in a variable. You can print out the variable incase your query isn't working and test it in phpmyadmin or mysql console.

Oh, Btw, you can also make use of die(mysql_error()); to know why your query failed.
ie.,

$result = mysql_query($query) or die(mysql_error());

Cheers,
Nav

Cheers nav33n. I thought my problem would be syntax related. I guess the more I code the more i'll pick up on such mistakes. I've added all the changes you mentioned all works fine now. Thread shall nw be solved.

Great! Good luck man :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.