selecting and updating fields
| From: | Claudiu Bandac | Date: | Mon, 13 Oct 2003 03:39:56 +0000 |
| Subject: | selecting and updating fields | ||
| Groups: | php.db | ||
| Request: | Send a blank email to php-db+get-30983@lists.php.net to get a copy of this message | ||
Hi !
I've got a form that passes a variable to a script.
The script connects to a database and I need to check if a field containing that variable already
exists in the table, and if it does, to select the "nr_of_views" field, increment it, and
update the table and if it doesn't, to create a new field with that variable and set
nr_of_views to 1
the table in my database that looks like this :
url nrviews
www.google.com 1
www.yahoo.com 3
And I've tried something like this:
$query = mysql_query("select * from url") or die (mysql_error());
while ($row = mysql_fetch_row($query))
{
if ($url === $row["url"])
{
$nrviews = $row["nrviews"];
$nrviews++
mysql_query("update url set nrviews='$nrviews' where
url='$url'") or
die (mysql_error());
}
else
{
$nrviews=1;
mysql_query("insert into url (url, nrviews) values
('$url','$nrviews')") or
die (mysql_error());
}
}
AND STILL DOESN'T WORK !!!
When I get a variable that is not in the database, the whole thing goes crazy ! (it inserts lots of
fields with that variable and nrviews=1
Please HELP !
Thank You !