Upload form with dropdown menu

i have a database with 3 tables that look like this:


id (auto incrementing also the primary key)


i am trying to create an upload form with a drop down menu (using the select function) what i want to do is in the dropdown menu display the gallery_type_desc in order by gallery_type_id. i have got this code so far but it dont work. i dont suppose anyone could have a look through and explain what i need to add or change that would be really helpfull. im new to php so any help would be really gratefull!

upload form code: (i get a parse error on the coloured line)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
$query = "SELECT gallery_type_id, gallery_type_desc FROM gallery_type ORDER BY gallery_type_id";

/* run the query */
$result = $dbLink->query($query);

/* load the query's results into an array */
$galleryTypes = $result->fetch_assoc();
$num_results = $result->num_rows;



 <form action="add_file.php" method="post" enctype="multipart/form-data">
         <input type="file" name="uploaded_file"><br>
         <input type="submit" value="Upload file">
     <form action="add_file.php" method="post" enctype="multipart/form-data">
<label for="gallery_type">Gallery Type</label>
<select name="gallery_type" id="gallery_type">
	[COLOR="Red"]while($i = 0; $i < $num_results; $i++) {[/COLOR]
	echo "<option value=\\"" . $galleryTypes[$i]['gallery_type_id'] . "\\">";
	echo $galleryTypes[$i]['gallery_type_desc'];
	echo "</option>\


add_file.php code

 // Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
     // Make sure the file was sent without errors
     if($_FILES['uploaded_file']['error'] == 0) {
		 $target_path = "images/";
		$target_path = $target_path . basename( $_FILES['uploaded_file']['name']); 

	if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploaded_file']['name']). 
    " has been uploaded";
	$dbLink = new mysqli('localhost', 'root', '', 'gallery');
         if(mysqli_connect_errno()) {
             die("MySQL connection failed: ". mysqli_connect_error());
         }  // You missed this closing curly brace here

         // Gather all required data
         $name = $dbLink-&gt;real_escape_string($_FILES['uploaded_file']['name']);
         $mime = $dbLink-&gt;real_escape_string($_FILES['uploaded_file']['type']);
         $size = intval($_FILES['uploaded_file']['size']);
         $image_path = $dbLink-&gt;real_escape_string($target_path);
		 $gallery_type = $dbLink-&gt;real_escape_string($_POST['gallery_type']); 
	$query = "INSERT INTO `images` (`name`, `mime`, `size`, `data`, `created`, `image_path`, `gallery_type_id`)
             VALUES ('{$name}', '{$mime}', {$size}, '', NOW(), '{$image_path}', '{$gallery_type}')";
 else {
     echo 'Error! A file was not sent!';
 // Echo a link back to the main page
 echo '&lt;p&gt;Click &lt;a href="member-index.php"&gt;here&lt;/a&gt; to go back&lt;/p&gt;';

Echo out something that proves that you actually got something out of the database.

Tell us what the error message was.

I’d prefer to do this;

	foreach( $galleryTypes as $gallery ) {

	echo '<option value="' . $gallery['gallery_type_id'] . '\\">'  ;
        echo $gallery['gallery_type_desc'] . '</option>' . PHP_EOL ;


Less error-prone, saves having to use a counter, easier to read. If you are creating double quoted html attributes then try surrounding them in single quotes and the constant PHP_EOL does
but correctly picks the right one for the operating sytem it is hosted on.

If $gallery is only destined to be used for the next few lines, then shortening it to something like $gall or even $g that makes no big difference and is arguably easier to read too.

how would i echo something from the database? im not very sure how to do that.

but at the moment i changed the while to a for loop and i get these errors:

Notice: Undefined variable: dbLink in C:\wamp\www\Blean_Photos\upload.php on line 10

Fatal error: Call to a member function query() on a non-object in C:\wamp\www\Blean_Photos\upload.php on line 10

they refer to the line:

$result = $dbLink->query($query);

You havent defined $dbLink anywhere in the upload.php script, you need to add the following line somewhere before line 10:

$dbLink = new mysqli('localhost', 'root', '', 'gallery');

ahhh wow thank you for that. i completly forgot i needed to define my db again.

it now picks up some information in the drop down menu but not the info i want

it gives me a error saying:

NOTICE: undefined offset: 1 in c:/wamp/www/Blean_Photos/upload.php on line 39.
NOTICE: undefined offset: 2 in c:/wamp/www/Blean_Photos/upload.php on line 39.
NOTICE: undefined offset: 3 in c:/wamp/www/Blean_Photos/upload.php on line 39.

which corrosponds to this:
echo $galleryTypes[$i][‘gallery_type_desc’];

any ideas what to change?

ive tried changing my for loop to this:
for($i = 1; $i < $num_results+1; $i++)

and starting my gallery_type_id at 0 but nothing has worked


i am now able to view my gallery names due to this code:

$galleryTypes[$i] = $row;

but get this error:

Notice: Undefined index: gallery_type_id in C:\wamp\www\Blean_Photos\add_file.php on line 48

from this line:

$gallery_type = $dbLink->real_escape_string($_POST[‘gallery_type’]);

it uploads to the folder okay but it doesnt write to the database due to that. i thought changing the [‘gallery_type’] to either [‘gallery_type_id’] or [‘gallery_type_desc’] but that didnt make any difference.

anyideas guys?

ive also got rid of the error. and now my problem is that it doesnt write to my db any more.

ive echoed my query and got this back:

INSERT INTO images (name, mime, size, data, created, image_path, gallery_type_id) VALUES (‘church interior.jpg’, ‘image/jpeg’, 607836, ‘’, NOW(), ‘images/church interior.jpg’, ‘3’)

which is the write stuff that needs to be written into my db but it doesnt appear in my db.

[ot]you cannot use
or \r
ie PHP_EOL while you are passing the dropdown data to javascript[/ot]