469,616 Members | 1,652 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,616 developers. It's quick & easy.

Blank Row added to database when existing row found

155 100+
I'm creating (trying to create) a picture gallery for my website.

The script is not working. I've been working on it now for about 80 hours with no success. My php skills aren't very good.

This picture gallery uses 2 mysql tables: acwrtcats and acwrtpics

The table acwrtcats has 2 fields: cat_name and cat_desc. The cat_name is the name of a category that pictures will be filed under, and cat_desc is a brief description of the category.

My picture information is stored in the table acwrtpics. This contains the fields: id, cat_name, title, desc, image_name, thumb_w, thumb_h.

When the image upload page is called an IF statement is used to determine if the table is empty or has categories. If no entries are found a form with a standard text input field is displayed to allow me to type in a name for a new category.

If there are entries found, a form with a drop-down list is presented and populated with the catgories found in the acwrtcats table, and a standard text input field is displayed under the drop-down list. I can either select an existing category for my image to be filed using the drop-down list or create a new category to be entered into the acwrtcats table via the text input field.

Below all this is the fields pertaining to the acwrtpics table.

When submitted it goes to the image_processing.php page where a thumbnail is created and the original image and newly created thumbnail image is placed into separate folders on the server. This page also processes the information for table insertion.

This is where I'm running into trouble. Much of this script isn't working and I don't know how to fix it.

The script is suppose to check whether or not the category name I chose on the upload page already existed in the acwrtcats table. If the name already exist then don't add it again, skip over this part and go straight to adding the picture information into the acwrtpics table.

If the category name (cat_name) does not exist in the acwrtcats table - add it.

This part is not working. If the name exists it advances the table by one. No cat_name or cat_desc is being added - just a blank row is being created. I was hoping that it would just leave this table alone if the name existed and if not add the information and the proceed to entering the information into the acwrtpics table.

The images are being created and uploaded into their respective folders and the image information is being added to the acwrtpics table - all except the row =cat_name.

The name of the category comes to this page via [PHP]$cat_name = $_GET['cat_name'];[/PHP].

Any help with this would be very much appreciated.

Here are the codes from the image_upload.php and the image_process.php:
[PHP]// image_upload.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='cat_name'>
<option value'' selected='selected'></option>
";

while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

$id = $row['id'];
$cat_name = stripslashes($row['cat_name']);

echo "

<option value='$cat_name'>$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>
";

---- image upload form here ----[/PHP]

[PHP]// image_process.php

---- Image handling script here ----

/*
* Here is where information is inserted into databases:
*/


include("includes/dbconnect.php");

$cat_name = $_GET['cat_name'];

// Make sure Category doesn't already exist, if it does skip down to second section
$query = "SELECT cat_name FROM $cat WHERE cat_name = '$cat_name'";
$result = mysql_query($query) or die('Error, query failed: ' . mysql_error());

if (mysql_num_rows($result) == 1)

{

// Make the 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;
$width = $width;
$height = $height;

$query = "INSERT INTO $table VALUES ('', '$cat_name', '$title', '$desc', '$image_name', '$thumb_w', '$thumb_h', '$width', '$height')";
$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>
";

}

else

{

$cat_name = mysql_real_escape_string($_POST['cat_name']);
$cat_desc = mysql_real_escape_string($_POST['cat_desc']);


$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;
$width = $width;
$height = $height;

$query = "INSERT INTO $table VALUES ('', '$cat_name', '$title', '$desc', '$image_name', '$thumb_w', '$thumb_h', '$width', '$height')";
$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>
";


mysql_close();
}
[/PHP]
Jun 18 '07 #1
2 1910
pbmods
5,821 Expert 4TB
Changed thread title to actually describe the problem (did you know that thread titles that contain phrases such as 'please help' and 'quick problem' actually get FEWER responses?).

Heya, DavidPr.

Try using a REPLACE statement instead of INSERT.
Jun 18 '07 #2
bonski
53
ei davidpr,

just an idea.. i think db design is quite essential also... designing your db table on how they are related...

acwrtcats
cat_id
cat_name
cat_desc

acwrtpics
image_id
cat_id
title
desc
image_name
thumb_w
thumb_h
i think for the table acwrtcats you should have a unique id also.. which is also found in table acwrtpics, and that is cat_id, just like you had (cat_name).. but your using string.. sometimes string aren't good to use... better use id(int) so that you'll have unique ids...^__^
Jun 19 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.