...

View Full Version : upload form with dropdown menu



saunders1989
01-14-2010, 12:48 PM
i have a database with 3 tables that look like this:

members:
member_id
firstname
lastname
login
password

images:
id (auto incrementing also the primary key)
name
size
created
image_path
gallery_type_id

gallery_type:
gallery_type_id
gallery_type_desc

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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<?php
$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;
?>

</head>

<body>

<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>

<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">
<?php
while($i = 0; $i < $num_results; $i++) {
echo "<option value=\"" . $galleryTypes[$i]['gallery_type_id'] . "\">";
echo $galleryTypes[$i]['gallery_type_desc'];
echo "</option>\n";
}
?>
</select>
</form>

</body>
</html>

add_file.php code



<?php

// 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->real_escape_string($_FILES['uploaded_file']['name']);

$mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);

$size = intval($_FILES['uploaded_file']['size']);

$image_path = $dbLink->real_escape_string($target_path);

$gallery_type = $dbLink->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}')";



$dbLink->query($query);



}





else {

echo 'Error! A file was not sent!';

}



}



}



// Echo a link back to the main page

echo '<p>Click <a href="member-index.php">here</a> to go back</p>';

?>

Rowsdower!
01-14-2010, 12:55 PM
Change "while" to "for" and that highlighted code should run (or at least not throw a parse error there).

Other than that, can you explain what isn't working about your code? Is there a test page that you can link us to so that we can see the results of your code? Or is the parse error your only issue?

saunders1989
01-14-2010, 05:37 PM
i changed the while to a for. but now 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

something to do with this line:

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

erm i wouldnt know how to link you to a test page. i could zip the two files that work together and email them to you if that is possible?

Rowsdower!
01-14-2010, 05:51 PM
Nah, I don't want to set this up on my host. Is this upload form running on your local computer or is it hosted online? If it's hosted online then you would just give us the URL that leads to your upload form.

Anyway, your problem now is this:
PHP has determined that your syntax is all OK so it is starting to execute code. When it reaches $result = $dbLink->query($query); it finds that $dbLink has not been assigned a value (in this case it has not been set as a valid database connection).

You must assign a working database connection to the variable $dbLink to get this script on track.

The exact script to do that will depend on your database. Are you using MySQL?

I just realized that your connection script is right there in add_file.php.

Just copy that part and paste it into your form page right above $result = $dbLink->query($query); and you should be all set.

saunders1989
01-14-2010, 06:01 PM
wow such simple mistakes im making. it is now displaying some information in the dropdown menu which says:

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

this corrosponds to this line:

echo $galleryTypes[$i]['gallery_type_desc'];

im not 100% sure why it isnt echoing the gallery_type_desc

im thinking because in my database i have this:

gallery_type_id
1
2
3

is it because i started at 1 instead i should start at 0?

EDIT:

it also says:

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.
etc

Rowsdower!
01-14-2010, 06:05 PM
Either that or change your loop to start at 1:


for($i = 1; $i < $num_results+1; $i++) {


Note that you will have to increase the limit by 1 also ($num_results+1) in order to avoid skipping the last entry.

But yes, honestly, I'd just start the database ID's at 0.

I should read more carefully... :(

saunders1989
01-14-2010, 06:09 PM
i tried both ideas. but i still get that error. its really odd. i really thought changing it to a zero to start wtih would make it work.

dont suppose you have any other ideas?

Rowsdower!
01-14-2010, 06:26 PM
Part of the problem is that I'm not at all familiar with OOP.

You might need to change your code here:

$galleryTypes = $result->fetch_assoc();

to something like this:

$i=0;
while($row=$result->fetch_assoc()){
$galleryTypes[$i] = $row;
$i++;
}

But again, I've actually never created anything using object-oriented PHP so the syntax might be crappy.

Basically, I think the last problem you have is that your $result is only returning one set of table values rather than all of them.

Maybe someone proficient in OOP can hop in and seal the deal?

saunders1989
01-14-2010, 06:40 PM
that bit of code worked a treat thanks :) it now shows all my gallery names. but im having another issue. (sorry for buggin you soo much. this is very much appreciated!)

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

that line is producing this error:
Notice: Undefined index: gallery_type_id in C:\wamp\www\Blean_Photos\add_file.php on line 48

i thought it was to do with the [gallery_type'] i dont have in my database so i tried both ['gallery_type_id'] and ['gallery_type_desc'] but they both came back with the same error.

Rowsdower!
01-14-2010, 06:51 PM
that bit of code worked a treat thanks :) it now shows all my gallery names. but im having another issue. (sorry for buggin you soo much. this is very much appreciated!)

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

that line is producing this error:
Notice: Undefined index: gallery_type_id in C:\wamp\www\Blean_Photos\add_file.php on line 48

i thought it was to do with the [gallery_type'] i dont have in my database so i tried both ['gallery_type_id'] and ['gallery_type_desc'] but they both came back with the same error.

I think the problem is that you have two separate forms. The submit button exists in a form that does not contain the dropdown, so the variable you are trying to use has not been passed through $_POST and your script is failing.

Try combining your two forms into one and see if that sorts it out.

saunders1989
01-14-2010, 06:54 PM
that was it!. but do you know why it has stopped writing to my database? beacuase before i put this in i would be able to upload my image. and display like image path and other details. now it doesnt write at all

EDIT:

you've been soo helpful. thank you soo much!!!!

Rowsdower!
01-14-2010, 06:59 PM
Not off-hand. This is where you need to start debugging your variable values inside of the add_file.php file. I'd start by echoing $query right after this line:


$query = "INSERT INTO `images` (`name`, `mime`, `size`, `data`, `created`, `image_path`, `gallery_type_id`)

VALUES ('{$name}', '{$mime}', {$size}, '', NOW(), '{$image_path}', '{$gallery_type}')";

And make sure that the output is a valid database command. I would suspect that, for whatever reason, it is not a valid command. It could be missing values or field names or might have quotation marks screwing it up. Really, it could be a lot of things.

Just start here and work your way back. At this stage it's a pretty safe bet that the upload page is OK the way it is so the error is most likely in the add_file.php file.

saunders1989
01-14-2010, 07:06 PM
i wrote this to
echo $query; is that correct because that displays this:

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 correct what i want to be written into my database. but it doesnt appear

EDIT:

i have now debugged it and was able to do an if statement around my execution of the query and picked up a few errors and i fixed them and now it is writing to my database!

thanks for all your help. been very helpful!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum