Log in

View Full Version : Click Counter Question



Titan85
01-25-2007, 12:40 AM
I am trying to make a click counter, but ran into an issue that I can't seem to fix. I get this error when I try to go to clicks.php: "
Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/bntqann/public_html/testing/click_counter/clicks.php on line 23"
Here is the code:
<?php

// Get referreing link
$id = $_GET['id'];

// SQL Query
$sql = "SELECT * FROM `clicks` WHERE `id` = '$id'";
$result = mysql_query($sql) or die ('Error Getting Link! <br />' .mysql_error());
$row_count = mysql_num_rows($result);

while($row = mysql_fetch_array($result)) {

$count = "$row[count]";
$count++;
$sql = "UPDATE `clicks` SET `count` = '$count', `date` = '$date', `id` = '$row['id']' LIMIT 1";
$result = mysql_query($sql) or die ('Error Getting Link! <br />' .mysql_error());
}

if($row_count =< 1) {
header("Location: $url")
}

else {
echo 'Error sending you to the link location!';
}
?>I am not sure why I got that error, hopefully someone will know :). Thanks in advance

apollo
01-25-2007, 03:11 PM
Possibly you need to add "exit" after the "header" below?


if($row_count =< 1) {
header("Location: $url")
exit;

Li0rE
01-25-2007, 05:50 PM
Possibly you need to add "exit" after the "header" below?


if($row_count =< 1) {
header("Location: $url")
exit;


No, that's not it either. It usually helps to put a semicolon after a line of code :)



if($row_count =< 1) {
header("Location: $url");
}

Strangeplant
01-25-2007, 08:41 PM
There are no statements after your 'else' and the program doesn't have anything to parse, leaving the code unexecutable.

Titan85
01-25-2007, 08:58 PM
Thanks, the semicolon was the error. Missed that due to tiredness :rolleyes: . Now that the script is working, I found another error, when I click on a link, the click amount does not increase. Here is my revised code:
<?php
require('config.php');

// Get referreing link
$id = $_GET['id'];

// SQL Query
$sql = "SELECT * FROM `clicks` WHERE `id` = '$id'";
$result = mysql_query($sql) or die ('Error Getting Link! <br />' .mysql_error());
$row_count = mysql_num_rows($result);
$array = mysql_fetch_array($result);

while($row = mysql_fetch_array($result)) {
extract($row);

$id = $_GET['id'];
$count = $_GET['count'];
$count++;
$sql = "UPDATE `clicks` SET `date` = '$date', `count` = '$count' WHERE `id` = '$id'";
$result = mysql_query($sql) or die ('Error Updating Link! <br />' .mysql_error());
}

$url = $array['url'];

if($row_count >= 1) {
header("Location: $url");
}

else {
echo 'Error sending you to the link location!';
}

?>I appreciate any help.

There are no statements after your 'else' and the program doesn't have anything to parse, leaving the code unexecutable.After the else statement, there is an echo statement.

djr33
01-25-2007, 10:42 PM
Else could stand alone. It would be pointless.
It's implied in the first place--
if (something) {
dothis();
}
[else, nothing]

If you wanted, you could add else, like this--
if (something) {
dothis();
}
else {
}

Or even:
if (something) {
dothis();
}
else
//continue code here

If you have, for example:
else echo "a";
it would work just fine.
So, using else blank shouldn't be much of an issue. I don't think it would be anyway. At least having the brackets, like else {} would make it perfectly valid. Stupid and do nothing. But valid.

The only time else will through an error at you is when you don't have an if preceeding it. Also, it'll have trouble like any other statement if you forgot the semicolon or bracket before it.

Strangeplant
01-26-2007, 01:35 PM
You have this:
$sql = "SELECT * FROM `clicks` WHERE `id` = '$id'";
$result = mysql_query($sql) or die ('Error Getting Link! <br />' .mysql_error());
$row_count = mysql_num_rows($result);
$array = mysql_fetch_array($result);

while($row = mysql_fetch_array($result)) {
extract($row);

$id = $_GET['id'];
$count = $_GET['count'];
$count++;
$sql = "UPDATE `clicks` SET `date` = '$date', `count` = '$count' WHERE `id` = '$id'";
$result = mysql_query($sql) or die ('Error Updating Link! <br />' .mysql_error());
} First, take out the backticks, the variables don't use them. See the man pages, for example at http://dev.mysql.com/doc/refman/5.0/en/select.html So, for example, the SELECT line would read:
$sql = "SELECT * FROM clicks WHERE id = '$id'";

Second, you are using the extract function to take the variables in the $row array and use them in the program, so the next two lines using the $_GET[] will overwrite the contents. Just use the variables themselves, i.e. $id and $count.

But I have a more basic question: Where do you INSERT a $id into the database in the first place? If the $id is not found, don't you want to add it? Because that's the only way that you will get a new row.....

Titan85
01-26-2007, 08:53 PM
I just learned to use backticks when I started, so I just kinda stuck with it. It still works normally. I have a separate page that adds the links into the database, but didn't post it because there is no error inserting the data. I took out the $_GET[] parts, but the click count still doesn't go up. Any ideas?