From an image upload form I want to be able to either select a Category from a drop down list to place my image under, or create a new Category. If I select a Category from the list I don't want it to be added again - in any fashion to the $cat (category) table, because it's already in there.
I tried to write a script that checks to see if a Category by that name already exists in the $cat table and if so skip it and go straight to the part where it puts the image information into the $table (picture table). I want the Category name (cat_name) to be entered into the cat_name field in the $table (picture table), but it's nbot doing that either.
The problem however, is that it's not working. The $cat table is being advanced 1. No name or desc is being entered but the id is advancing by 1. So I know that my script is not working because if should be skipping anything to do with the $cat table if there is already a cat_name in there by the name submitted by the form from the drop down menu. I wrote a script to populate the drop down menu with the category names (cat_name) from the $cat table. So yes, the name already exist in the table.
Can you see why this is not working?
If I decide to create a new Category rather than selecting one from the list it should skip down to the second part of this script and 1- add the new Category to the $cat table and add the picture info into the $table (picture table). I haven't tested this part yet, as Iwould like to fix the first problem first. But if you see a problem in this section to please tell me.
Thanks.
include("includes/dbconnect.php");
$cat_name = $_GET['cat_name'];
// Check to see if Category exists with this cat_name
$query = "SELECT cat_name FROM $cat WHERE cat_name = '$cat_name'";
$result = mysql_query($query) or die('Error, query failed: ' . mysql_error());
if ($cat_name == '')
{
$query = "INSERT INTO $cat VALUES ('', '$cat_name', '$cat_desc')";
mysql_query($query);
$cat_name = mysql_real_escape_string($_POST['cat_name']);
$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_name', '$title', '$desc', '$image_name', '$thumb_w', '$thumb_h')";
$result = mysql_query($query);
echo "<h2 style='color: #090'>Success! Picture Added</h2>
<br>
<a href='index_c.php'>View Pics</a>
<br><br>
<a href='image_upload_d.php'>Add Another Pic</a>
";
}
else
{
// Make the query
$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_name', '$title', '$desc', '$image_name', '$thumb_w', '$thumb_h')";
$result = mysql_query($query);
echo "<h2 style='color: #090'>Success! New Category and Picture Added</h2>
<br>
<a href='index_c.php'>View Pics</a>
<br><br>
<a href='image_upload_d.php'>Add Another Pic</a>
";
mysql_close();
}