Urgent help with errors :(

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rsmags
Forum Newbie
Posts: 1
Joined: Mon Apr 21, 2003 11:47 am

Urgent help with errors :(

Post by rsmags »

I am a bit confused as to what is wrong with my script... I have a form (http://www.logicalreef.com/zoos.php), and when I enter a $zoonum that allready exists, I get an error but I put an if statement into my script and I dont think its working, I really am no php guru so if someone can tell me what is wrong that would be great!
Here is my error:

Warning: mysql_result(): supplied argument is not a valid MySQL result resource in /home/thelogic/public_html/admin/upload.php on line 9
Duplicate entry 'Z0001' for key 2

Here's complete script:

Code: Select all

<?php
$price = $HTTP_POST_VARS['price'];
$zoonum = $HTTP_POST_VARS['zoonum'];
$submit = $HTTP_POST_VARS['submit'];
$file = $HTTP_POST_FILES['srcimage'];
$uploaddir = '../sale/';
mysql_connect("localhost","username","password"); //then connect as user
mysql_select_db("thelogic_sale"); //select which database you want to edit
if (mysql_result(mysql_query('SELECT COUNT(*) FROM zoos WHERE zooID='.$zoonum),0)==0) {
    mysql_query("INSERT INTO zoos (ID,zooID,avail,price,code)"."VALUES ('NULL', '$zoonum', 'avail', '$price', 'Yummy Zoos')") or die(mysql_error());
	echo "$zoonum added!";
} 
else
{
    mysql_query('UPDATE zoos SET avail="avail", price="'.$price.'", code="Yummy Zoos" WHERE zooID='.$zoonum) or die (mysql_error());
	echo "$zoonum updated!";
}
print "Creating thumbnail from source image...";

echo $file['name']."<br>\n";
echo $file['tmp_name']."<br>\n";
copy($file['tmp_name'], $uploaddir.$zoonum.'.jpg');
$src = ImageCreateFromJPEG($uploaddir.$zoonum.'.jpg');
$org_h = imagesy($src); 
$org_w = imagesx($src); 
$img = ImageCreateTrueColor(150,100); 
ImageCopyResampled($img, $src, 0, 0, 0, 0, 150, 100, $org_w, $org_h ); 
ImageJPEG($img, $uploaddir.$zoonum.'thumb.jpg', 100); 
ImageDestroy ($img); 
ImageDestroy ($src);
echo "Finished, click <a href='index.php'>here</a> to add additional entries.<br>";
echo "Below is the thumbnail that was generated:<br>";
echo "<img src='http://www.logicalreef.com/sale/"."$zoonum"."thumb.jpg'>";
?>
Any help is greatly appreciated! :cry:

Ryan
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The error is occuring because of a problem with the first mysql_query() call - ie. there is an error in the first SQL statement, you could try something like:

Code: Select all

<?php
$price  = $HTTP_POST_VARS['price']; 
$zoonum = $HTTP_POST_VARS['zoonum']; 
$submit = $HTTP_POST_VARS['submit']; 
$file   = $HTTP_POST_FILES['srcimage']; 
$uploaddir = '../sale/'; 
@mysql_connect('localhost', 'username', 'password') or die(mysql_error()); //then connect as user 
@mysql_select_db('thelogic_sale') or die(mysql_error()); //select which database you want to edit 

// in SQL statements strings like Z0001 need to be enclosed in single quotes
$sql = "SELECT ID FROM zoos WHERE zooID='$zoonum'";
@$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');

// IMHO, using mysql_num_rows instead of mysql_query() and mysql_result()
// together is a bit clearer to read and allows you to handle errors better
if (mysql_num_rows($result) == 0) {
	$sql = "INSERT INTO zoos (ID,zooID,avail,price,code)"."VALUES (NULL, '$zoonum', 'avail', '$price', 'Yummy Zoos')";
	$action = 'added';
} else {
	$sql = "UPDATE zoos SET avail='avail', price='$price', code='Yummy Zoos' WHERE zooID='$zoonum'";
	$action = 'updated';
}

@mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
echo (mysql_affected_rows() == 1) ? $zoonum.' '.$action.'!' : $zoonum.' has not been '.$action.'!';

// image processing stuff...
?>
Post Reply