I thought maybe that's where it should go, but I wasn't sure.
Look, the page that is calling this page has some code that I would like for you to take a peek at and see if I did it right. Basically what it does is check the table $cat to see if there's anything in it. If there is, it displays a drop-down form field with the Categories listed in that table. It also displays a regular form text field and the user can either select a Category from the drop down to file their image under or create a new Category using the regular form text field.
Also, I added some code to the bottom of this page we've been working on to handle the information on the sending page to go into the database. The code above I'll call Code-1 and the one you've been helping me with I'll call Code-2.
Code-2 I'm getting errors. I'll show both pages and if you don't mind please take a peek at them and see if 1- I'm on point and 2- gross mistakes.
Code-1
<form action="image_process_c.php" enctype="multipart/form-data" method="post">
<p>
<?php
include("includes/dbconnect.php");
$query = "SELECT * FROM $cat";
$result = mysql_query($query) or die('Error, query failed');
if (mysql_num_rows($result) == 0) {
echo "
Add a new Category:<br>
<input type='text' size='55' name='cat_name'><br>
Category Description:<br>
<textarea name='cat_desc' rows='2' cols='55' wrap=virtual></textarea><br><br>
";
} else {
echo "
Select a Category for your Picture:<br>
<select name='file'>
<option value'' selected='selected'>Select a Category</option>
";
$query = "SELECT * FROM $cat";
$result = mysql_query($query) or die('Error, query failed');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$id = $row['id'];
$cat_name = $row['cat_name'];
echo "<option value='$id'>$cat_name</option>";
}
}
echo "
</select><br><br>
Or Add a new Category:<br>
<input type='text' size='55' name='cat_name'><br>
Category Description:<br>
<textarea name='cat_desc' rows='2' cols='55' wrap=virtual></textarea><br><br><br>
";
?>
-- the form fields goes here --
</form>
Code-2
/*
* Here is where you insert the information and all:
*/
include("includes/dbconnect.php");
$file = $_GET['file'];
// Check to see if Category exists with this id
// if so bypass adding it to the $cat table and put straight into $table
$query = "SELECT id FROM $cat WHERE id='$file'";
$result = mysql_query($query) or die('Error, query failed: ' . mysql_error());
if (mysql_num_rows($result) != 0) {
// Make the query
$cat_id = $file;
$title = mysql_real_escape_string($_POST['title']);
$desc = mysql_real_escape_string($_POST['desc']);
$thumb_w = $t_width;
$thumb_h = $t_height;
$query = "INSERT INTO $table VALUES ('', '$cat_id', '$title', '$desc', '$image_name', '$thumb_w', '$thumb_h')";
$result = mysql_query($query);
if(mysql_affected_rows() > 0) {
exit('<h2 style="color: #090">Success!</h2><br><a href="index_c.php">View Pics</a> • <a href="image_upload_d.php">Add Another Pic</a>');
} else {
exit('<h2 style="color:#f00">Insertion into database failed!!</h2>');
} else {
exit('<h2 style="color:#f00">No file uploaded!!</h2>');
}
} else {
// If Category doesn't exist add it to the $cat table
$query = "INSERT INTO $cat VALUES ('', '$cat_name', '$cat_desc')";
mysql_query($query);
$cat_id = mysql_insert_id();
$title = mysql_real_escape_string($_POST['title']);
$desc = mysql_real_escape_string($_POST['desc']);
$thumb_w = $t_width;
$thumb_h = $t_height;
$query = "INSERT INTO $table VALUES ('', '$cat_id', '$title', '$desc', '$image_name', '$thumb_w', '$thumb_h')";
$result = mysql_query($query);
if(mysql_affected_rows() > 0) {
exit('<h2 style="color: #090;">Success!</h2><br><a href="index_b.php">View Pics</a> • <a href="image_process_b.php">Add Another Pic</a>');
} else {
exit('<h2 style="color:#f00;">Insertion into database failed!!</h2>');
}
} else {
exit('<h2 style="color:#f00;">No file uploaded!!</h2>');
}
I have not successfully run this script, so I don't know if it'll work or not. The current error I get when I run this is this: parse error, unexpected T_ELSE in /local/home/user/www/gallery/image_process_c.php on line 119 and the line in question I've placed in bold below.
Here is the entire page that you and I have been woking on. I haven't included the "check to see if image_name exist" in yet, but I did change a couple of things, that maybe I shouldn't have - I don't know.
<?php
// Max allowed size of the image (in bytes):
$maxUploadSize = 200000;
// The paths to save the image(s):
$thumb_dir = './thumb_uploads/';
$thumb_url = 'thumb_uploads/';
$photo_dir = './image_uploads/';
$photo_url = 'image_uploads/';
// The max width, or height of the thumbnail:
$thumb['width'] = 120;
$thumb['height'] = 100;
$thumb['resolution'] = 80;
// Uploaded file's Temp Name:
$tempname = $_FILES['image']['tmp_name'];
// Define the image name:
$image_name = $_FILES['image']['name'];
// Is the file uploaded:
if(is_uploaded_file($tempname))
{
// Get the orginal dimensions:
$o_dims = getimagesize($tempname);
if($o_dims === false || $o_dims[2] != 2)
{
exit('<h2 style="color:#f00">Image Upload Failed!!</h2>');
}
// Put the original dimensions into something easier to work with:
$width = $o_dims[0];
$height = $o_dims[1];
// Create a new image based on the temp:
$orig_image = imagecreatefromjpeg($tempname);
// Get the proportionate thumbnail dimensions:
if($height >= $width)
{
$t_width = ceil(($thumb['height']/$height)*$width);
$t_height = $thumb['height'];
}
elseif($width > $height)
{
$t_width = $thumb['width'];
$t_height = ceil(($thumb['width']/$width)*$height);
}
// Create a new image with our thumb dimensions:
$new_image = imagecreatetruecolor($t_width, $t_height);
// Now, copy the old image to the new image magically resizing it:
if(imagecopyresampled($new_image, $orig_image, 0, 0, 0, 0, $t_width, $t_height, $width, $height) === false)
{
exit('<h2 style="color: #f00;">Image Resizing Failed!!</h2>');
}
if(@imagejpeg($new_image, $thumb_dir.'thumb_'.$image_name, $thumb['resolution']) === false)
{
exit('<h2 style="color:#f00;">Creating Thumb Failed!!</h2>');
}
if(move_uploaded_file($tempname, $photo_dir.$image_name) === false)
{
exit('<h2 style="color:#f00;">Unable to move uploaded file!!</h2>');
}
/*
* Here is where you insert the information and all:
*/
include("includes/dbconnect.php");
$file = $_GET['file'];
// Check to see if Category exists with this id
$query = "SELECT id FROM $cat WHERE id='$file'";
$result = mysql_query($query) or die('Error, query failed: ' . mysql_error());
if (mysql_num_rows($result) != 0) {
// Make the query
$cat_id = $file;
$title = mysql_real_escape_string($_POST['title']);
$desc = mysql_real_escape_string($_POST['desc']);
$thumb_w = $t_width;
$thumb_h = $t_height;
$query = "INSERT INTO $table VALUES ('', '$cat_id', '$title', '$desc', '$image_name', '$thumb_w', '$thumb_h')";
$result = mysql_query($query);
if(mysql_affected_rows() > 0) {
exit('<h2 style="color: #090">Success!</h2><br><a href="index_c.php">View Pics</a> • <a href="image_upload_d.php">Add Another Pic</a>');
} else {
exit('<h2 style="color:#f00">Insertion into database failed!!</h2>');
[B]} else {[/B]
exit('<h2 style="color:#f00">No file uploaded!!</h2>');
}
} else {
$query = "INSERT INTO $cat VALUES ('', '$cat_name', '$cat_desc')";
mysql_query($query);
$cat_id = mysql_insert_id();
$title = mysql_real_escape_string($_POST['title']);
$desc = mysql_real_escape_string($_POST['desc']);
$thumb_w = $t_width;
$thumb_h = $t_height;
$query = "INSERT INTO $table VALUES ('', '$cat_id', '$title', '$desc', '$image_name', '$thumb_w', '$thumb_h')";
$result = mysql_query($query);
if(mysql_affected_rows() > 0) {
exit('<h2 style="color: #090;">Success!</h2><br><a href="index_b.php">View Pics</a> • <a href="image_process_b.php">Add Another Pic</a>');
} else {
exit('<h2 style="color:#f00;">Insertion into database failed!!</h2>');
}
} else {
exit('<h2 style="color:#f00;">No file uploaded!!</h2>');
}
?>