selecting and updating fields

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 !

-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux