Results 1 to 8 of 8

Thread: Click Counter Question

  1. #1
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Click Counter Question

    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 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
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Jan 2007
    Location
    Boulder City, Nevada
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Maybe

    Possibly you need to add "exit" after the "header" below?
    Code:
    if($row_count =< 1) { 
        header("Location: $url") 
        exit;

  3. #3
    Join Date
    Jun 2006
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by apollo View Post
    Possibly you need to add "exit" after the "header" below?
    Code:
    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

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

  4. #4
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    There are no statements after your 'else' and the program doesn't have anything to parse, leaving the code unexecutable.

  5. #5
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks, the semicolon was the error. Missed that due to tiredness . 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 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.
    Quote Originally Posted by Strangeplant View Post
    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.
    Thanks DD, you saved me countless times

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  7. #7
    Join Date
    Feb 2006
    Posts
    236
    Thanks
    8
    Thanked 3 Times in 3 Posts

    Default

    You have this:
    Code:
    $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:
    Code:
    $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.....

  8. #8
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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?
    Thanks DD, you saved me countless times

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •