dachshund Posted May 17, 2008 Share Posted May 17, 2008 I'm trying to add 1 to 'views' in my table each time the page is viewed. My code at the moment is: <?php $id=$_GET['id']; $sql="SELECT * FROM features WHERE id='$id'"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ $view=$rows['views']; if(empty($view)){ $view=1; $sql2="INSERT INTO features(views) VALUES('$view') WHERE id='$id'"; } $addview=$view+1; $sql3="update features set views='$addview' WHERE id='$id'"; ?> But the views still stays at 0. The 'views' row is an INT and NOT_NULL. Any help? Quote Link to comment Share on other sites More sharing options...
dachshund Posted May 17, 2008 Author Share Posted May 17, 2008 Basically I need a code that increases the the INT number in the 'views' column by one automatically, when the page is viewed. Basically a view counter. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted May 17, 2008 Share Posted May 17, 2008 auto_increment in the mysql table Quote Link to comment Share on other sites More sharing options...
Barand Posted May 17, 2008 Share Posted May 17, 2008 you need to execute the queries. All you have done is define strings. $sql3="update features set views=views+1 WHERE id='$id'"; mysql_query($sql3); And Insert queries don't have a WHERE clause. Quote Link to comment Share on other sites More sharing options...
947740 Posted May 18, 2008 Share Posted May 18, 2008 To add on, you need to make a connection to the database, which is not apparent in your script. Quote Link to comment Share on other sites More sharing options...
dachshund Posted May 18, 2008 Author Share Posted May 18, 2008 Here's the full code. <?php include "../template/header.php"; ?> <link href="../template/style.css" rel="stylesheet" type="text/css" /> <tr> <td width="760" valign="top"> <table align="center" bgcolor="#FFFFFF" width="100%" cellpadding="5" cellspacing="0"> <tr> <td align="left" valign="top" class="secondtitle" height="15"> Features </td> </tr> <tr> <td valign="top" align="left"> <table width="100%" border="0" align="left" cellpadding="5"> <?php $id=$_GET['id']; $sql="SELECT * FROM features WHERE id='$id'"; $result=mysql_query($sql); while($rows=mysql_fetch_array($result)){ $view=$rows['views']; if(empty($view)){ $view=1; $sql2="INSERT INTO features(views) VALUES('$view') WHERE id='$id'"; } $addview=$view+1; $sql3="update features set views='$addview' WHERE id='$id'"; ?> <tr> <td width="100%" align="left" valign="top" class="contentmaintitle"> <? echo $rows['title']; ?> </td> </tr> <tr> <td width="100%" align="left" valign="top" class="smallertitle"> <? echo $rows['subtitle']; ?> </td> </tr> <tr> <td width="100%" align="justify" valign="top" class="maincontent"> <? echo $rows['content']; ?> </td> </tr> <tr> <td width="100%" align="justify" valign="top" class="maincontent"> <a href="<? echo $rows['link']; ?>"><? echo $rows['link']; ?></a> </td> </tr> <tr> <td width="100%" align="left" valign="top" class="wordsby"> <? echo $rows['wordsby']; ?> </td> </tr> <?php } mysql_close(); ?> </table> </td> </tr> </table> <?php include "../template/footer.php"; ?> The connection to the database is all fine, that's in the header. Where should I execute the queries? Still can't get it to work. Thanks for your help. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 18, 2008 Share Posted May 18, 2008 try <?php $id=$_GET['id']; $sql="SELECT * FROM features WHERE id='$id'"; $result=mysql_query($sql); if ($rows=mysql_fetch_array($result)){ $sql = "update features set views=views+1 WHERE id='$id'"; mysql_query($sql); } else { $sql="INSERT INTO features(id, views) VALUES('$id', 1)"; mysql_query($sql); } ?> Quote Link to comment Share on other sites More sharing options...
dachshund Posted May 18, 2008 Author Share Posted May 18, 2008 Hmm. Comes up with Parse error: syntax error, unexpected '}' in /homepages/46/d193566068/htdocs/features/view_feature.php on line 58 Can't work out what's wrong though Quote Link to comment Share on other sites More sharing options...
dachshund Posted May 18, 2008 Author Share Posted May 18, 2008 Basically I just need a code which will increase the INT number by 1 each time the page is viewed. Quote Link to comment Share on other sites More sharing options...
DeanWhitehouse Posted May 18, 2008 Share Posted May 18, 2008 as i said use a auto_increment field in a table, then just insert a blank entry into the database when the script is run Quote Link to comment Share on other sites More sharing options...
micmania1 Posted May 18, 2008 Share Posted May 18, 2008 You may have forgotten to remove the braket from the old while loop. If you havn't, repost your code. Quote Link to comment Share on other sites More sharing options...
Barand Posted May 18, 2008 Share Posted May 18, 2008 Hmm. Comes up with Parse error: syntax error, unexpected '}' in /homepages/46/d193566068/htdocs/features/view_feature.php on line 58 Can't work out what's wrong though Perhaps you have an unexpected "}" on line 58 Quote Link to comment Share on other sites More sharing options...
947740 Posted May 19, 2008 Share Posted May 19, 2008 Just as a side note, whenever I get complicated scripts with a lot of brackets, I comment like: } # closes: if(something) {, so I know which bracket it closes. Quote Link to comment Share on other sites More sharing options...
dachshund Posted May 19, 2008 Author Share Posted May 19, 2008 thanks, got it sorted, sorry for being stupid. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.