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>';
?>
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>';
?>