AW: selecting and updating fields

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

 



You could do:

mysql_query("update url set nrviews=nrviews+1 where url='$url'");
if(mysql_affected_rows()==0)
  mysql_query("insert into url (urn, nrviews) values ('$url',1)");

Lars

> -----Ursprüngliche Nachricht-----
> Von: Claudiu Bandac [mailto:k@kgb.ro] 
> Gesendet: Montag, 13. Oktober 2003 05:40
> An: php-db@lists.php.net
> Betreff:  selecting and updating fields
> 
> 
> 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
> 

-- 
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