For Programmers: Free Programming Magazines  


Home > Archive > PHP SQL > November 2004 > Re: Record Management Question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: Record Management Question
Trevor Llewellyn

2004-11-25, 3:56 am

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
##-----------------------------------------------##
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com