Code Comments
Programming Forum and web based access to our favorite programming groups.Hi Dan,
The only way out there that I know of is to check to see if the entry
exists. If it does, extract the entry, increment it, then update it.
Like this:
<?
$result = mysql_query("SELECT * FROM `tools` WHERE toolname = '#10
Phillips'", $conn) or die(mysql_error());
if(mysql_num_rows($result) == 0)
{
mysql_query("INSERT INTO `tools` (toolID, toolname, numtools) VALUES ('',
'#10 Phillips', 0)", $conn) or die(mysql_error());
}
else
{
$row = mysql_fetch_assoc($result);
$oldNumTools = $row['numtools'];
$newNumTools = $oldNumTools + 1;
mysql_query("UPDATE `tools` SET numtools = '$newNumTools' WHERE toolname =
'#10 Phillips'", $conn) or die(mysql_error());
}
?>
I know it's a bit of a lengthy bit of code to make such a simple thing
happen, but it's all that is available.
Hope this helps...
Trev
D & T wrote:
> Hi,
> very new to PHP / MySql, had a quick question.
> I am creating a database to track tools. I only want there to be
> one
> entry for each type of tool (ie. #10 Phillips). If there is already a
> record for a #10 Phillips, I do not want there to be a second one. I
> just
> want to increase the quantity associated with the first record.
> Is there a clean / simple way to verify whether or not an entry
> already
> exists in my database?
> Thanks,
> Dan.
##-----------------------------------------------##
Article posted from PHP Freaks NewsGroups
http://www.phpfreaks.com/newsgroups
Get Addicted: alt.php.sql
##-----------------------------------------------##
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.