...

View Full Version : Incrementation not incrementing correctly



devinmaking
10-16-2012, 09:21 AM
Hi guys.

Ill post you the code and then ill talk about it :)


$viewcount_update = mysql_query("UPDATE dynamic_pages SET view_count=`view_count` +1 WHERE page_name='" . $securename . "'") OR die();

Ok as you can see its a sql query which is basically updating the dynamic_pages by updating the view count by 1.

The problem is its increasing by 2.

How is this possible? Its even stating +1

Can someone point me the the correct path to a solution.

Thanks

Redcoder
10-16-2012, 10:20 AM
This should work:


$viewcount_update = mysql_query("UPDATE dynamic_pages SET view_count=(view_count +1) WHERE page_name='" . $securename . "'") OR die();

Why'd you escape view_count in the second and not in the first instance? You should probably drop the back-tick as it's usually for MySQL reserved words (http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html), so it's usually used when escaping variables when used for table names as they may contain any name.

devinmaking
10-16-2012, 10:46 AM
This should work:


$viewcount_update = mysql_query("UPDATE dynamic_pages SET view_count=(view_count +1) WHERE page_name='" . $securename . "'") OR die();

Why'd you escape view_count in the second and not in the first instance? You should probably drop the back-tick as it's usually for MySQL reserved words (http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html), so it's usually used when escaping variables when used for table names as they may contain any name.

Thanks for the input, put its exactly the same, its still incrementing +2 instead of +1

Redcoder
10-16-2012, 10:56 AM
Then my guess is that you are running that query twice by mistake. Try looking into the code that triggers that query to run, i'm sure that it triggers it twice.

Try having a variable $i=1 just outside the loop that runs the query. Set it to increase by one. Then set it to echo at the very bottom of the script. If $i has a value of 3, then the problem is that the query is run twice by mistake.

Or better, post the whole code here.

devinmaking
10-16-2012, 11:12 AM
Then my guess is that you are running that query twice by mistake. Try looking into the code that triggers that query to run, i'm sure that it triggers it twice.

Try having a variable $i=1 just outside the loop that runs the query. Set it to increase by one. Then set it to echo at the very bottom of the script. If $i has a value of 3, then the problem is that the query is run twice by mistake.

Or better, post the whole code here.

I have double checked the code, its not duplicating anywhere.

Here is the whole page including included pages:

FRONTSIDE:


<?php

require_once("applications/configuration/connection.php");
require_once("applications/configuration/settings.php");
require_once("applications/controllers/Index/homepage.php");

?>
<!DOCTYPE HTML>

<html>

<head>

<meta charset="utf-8">
<meta name="viewport" content="width=device-width">

<title><?php echo $meta_title ;?></title>

<meta name="description" content="<?php echo $meta_description ; ?>" />

<meta name="keywords" content="<?php echo $meta_keyword ; ?>" />
<meta http-equiv="Content-language" content="en" />
<meta name="language" content="english, EN" />
<meta name="rating" content="General" />
<meta name="distribution" content="Global" />
<meta name="robots" content="index, follow" />
<meta name="revisit-after" content="1 Day" />
<meta name="email" content="info(at)tinaadams.co.uk" />
<meta name="copyright" content="UBL Designs" />
<link rel="shortcut icon" href="" />
<link rel="bookmark icon" href="" />
<link href="/css/main.css" rel="stylesheet" type="text/css">

<script src="/js/jquery.min.js"></script>
<script>

$(document).ready(function() {

$("<select />").appendTo(".navigation");
$("<option />", {"selected": "selected","value" : "","text" : "Navigation Menu"}).appendTo(".navigation select");
$(".navigation ul li a").each(function() {var el = $(this);
$("<option />", {"value" : el.attr("href"),"text" : el.text()}).appendTo(".navigation select");});
$(".navigation select").change(function() {
window.location = $(this).find("option:selected").val();
});
});
</script>

<?php require_once("bodies/googleanalytics.php"); ?>

</head>

<body>

<?php require_once("bodies/left-column.php"); ?>

<div class="clear"></div>

<div class="right_column">

<div class="webwrap">

<div class="middlewrap1">

<div class="middlewrap2">

<div class="innerwebwrap">

<?php require_once("bodies/slider.php"); ?>

<div class="clear"></div>

<h1><?php echo $page_title ; ?><span></span></h1>

<div class="clear"></div>

<p><?php echo nl2br($page_content) ; ?></p>

<h2>Events &amp; Services We Offer<span></span></h2>

<?php echo images_nav() ; ?>

</div>

</div>

</div>

</div>

</div>

</body>

</html>


settings.php:



<?php

$getsettings = mysql_query("SELECT email,maintainance_mode,facebook,twitter,linkedin FROM settings WHERE id=1") OR die();
$fetchsettings = mysql_fetch_assoc($getsettings);

$websites_email = stripslashes($fetchsettings["email"]);
$websites_maintainance_mode = stripslashes($fetchsettings["maintainance_mode"]);
$websites_facebook = stripslashes($fetchsettings["facebook"]);
$websites_twitter = stripslashes($fetchsettings["twitter"]);
$websites_linkedin = stripslashes($fetchsettings["linkedin"]);

if($websites_maintainance_mode == 1){
header("Location: /maintainance-mode/");
die();
}


Homepage.php:



<?php
require_once("./Libs/sanitise.php");
$viewcount_update = mysql_query("UPDATE static_pages SET view_count=(view_count +1) WHERE id=1") OR die();
//page details
$apage = "home";
$query = mysql_query("SELECT * FROM static_pages WHERE id=1") OR die();
$fetch_page_info = mysql_fetch_assoc($query);

$page_title = stripslashes($fetch_page_info["page_title"]);
$meta_title = stripslashes($fetch_page_info["meta_title"]);
$meta_description = stripslashes($fetch_page_info["meta_description"]);
$meta_keyword = stripslashes($fetch_page_info["meta_keyword"]);
$page_content = stripslashes($fetch_page_info["content"]);

//imaged navigation details
function images_nav(){

$slug = new sanitiseClass();

$image_query = mysql_query("SELECT page_name,mini_image,page_title FROM dynamic_pages WHERE islive=1 ORDER BY page_name ASC") OR die();
$query_num = mysql_num_rows($image_query);
if($query_num > 0){

$list = '';

$list .= '<div class="elements">';
$list .= '<ul>';

$list .= '<li>';
$list .= '<img src="/Resource/Navigation/weddingflowers.jpg" alt="Wedding Flowers In And Around The Burton &amp; Derby Areas" height="185" width="185">';
$list .= '<div class="elopac">';
$list .= '<p><a href="/wedding-flowers-in-burton-and-derby/" class="elementtitle">Wedding Flowers</a></p>';
$list .= '<a href="/wedding-flowers-in-burton-and-derby/"><img src="/images/view.png" alt="View Info"></a>';
$list .= '</div>';
$list .= '</li>';

$list .= '<li>';
$list .= '<img src="/Resource/Navigation/736492floristry-classes-small.jpg" alt="Flower Classes, Flower Courses, Flower Arranging In And Around The Burton &amp; Derby Areas" height="185" width="185">';
$list .= '<div class="elopac">';
$list .= '<p><a href="/flower-arranging-classes-in-burton-and-derby/" class="elementtitle">Flower Classes</a></p>';
$list .= '<a href="/flower-arranging-classes-in-burton-and-derby/"><img src="/images/view.png" alt="View Info"></a>';
$list .= '</div>';
$list .= '</li>';

while($fetch_image = mysql_fetch_assoc($image_query)){
$imagenav_name = stripslashes($fetch_image["page_name"]);
$mini_image = stripslashes($fetch_image["mini_image"]);
$imagenav_title = stripslashes($fetch_image["page_title"]);
$list .= '<li>';
$list .= '<img src="/Resource/Navigation/' . $mini_image . '" alt="' . $imagenav_title . '" height="185" width="185">';
$list .= '<div class="elopac">';
$list .= '<p><a href="/' . $slug->sluggify($imagenav_name) . '/" class="elementtitle">' . $imagenav_name . '</a></p>';
$list .= '<a href="/' . $slug->sluggify($imagenav_name) . '/"><img src="/images/view.png" alt="View Info"></a>';
$list .= '</div>';
$list .= '</li>';
}

$list .= '</div>';
$list .= '</ul>';

return $list;

} else {
return "No Navigation Has Been Created Yet";
}

}


I have not given connection file because that is database connection file and also the sanitise file, its just stripping slashes, mysql_real_escape etc etc.

As you can see this is not duplicating the code



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum