Jump to content

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/106103-solved-1-view/
Share on other sites

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.

 

 

Link to comment
https://forums.phpfreaks.com/topic/106103-solved-1-view/#findComment-544345
Share on other sites

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);
}

?>

Link to comment
https://forums.phpfreaks.com/topic/106103-solved-1-view/#findComment-544355
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.