Results 1 to 10 of 10

Thread: Rating Script Error

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

    Default Rating Script Error

    I am trying to make a script for rating tutorials. I have it mostly worked out, but for some reason the script will not insert the data nor throw an error. Here is the script code:
    PHP Code:
    <?php

    $id 
    $_GET['id'];

    // If vote sent
    if ($_POST['vote']) {

        
    $star $_POST['star'];
        
        
    // If voted before
        
    if (isset($_COOKIE['voted']) && $_COOKIE['voted'] == ''.$id.'') { echo '<meta http-equiv="refresh" content="2;URL=tutorials.php" /> <span style="color: #FF0000">You have already voted!</span>'; }
        
        
    // If have not voted
        
    else {
        
            @
    setcookie('voted'''.$id.''time() + ((60*60*60) * 72)); 
            
    $get mysql_query("SELECT star1, star2, star3, star4, star5, total FROM `ratings` WHERE id = '$id'") or die ("Error Getting Votes! \n<br />\n" .mysql_error());
            
    $v mysql_fetch_array($get);
            
            
    // Update votes
            
    if ($star == 1) { $update mysql_query("UPDATE `ratings` SET star1 = star1+1, total = total+1 WHERE id = '$id'") or die ("Error Updating Votes! \n<br />\n" .mysql_error()); 
                echo 
    '
                <meta http-equiv="refresh" content="2;URL=tutorials.php" />
                    Your vote has been submitted!'
    ;
            }
            
            elseif (
    $star == 2) { $update mysql_query("UPDATE `ratings` SET star2 = star2+1, total = total+1 WHERE id = '$id'") or die ("Error Updating Votes! \n<br />\n" .mysql_error());
                echo 
    '
                <meta http-equiv="refresh" content="2;URL=tutorials.php" />
                    Your vote has been submitted!'
    ;
            }
            
            elseif (
    $star == 3) { $update mysql_query("UPDATE `ratings` SET star3 = star3+1, total = total+1 WHERE id = '$id'") or die ("Error Updating Votes! \n<br />\n" .mysql_error());
                echo 
    '
                <meta http-equiv="refresh" content="2;URL=tutorials.php" />
                    Your vote has been submitted!'
    ;
            }
            
            elseif (
    $star == 4) { $update mysql_query("UPDATE `ratings` SET star4 = star4+1, total = total+1 WHERE id = '$id'") or die ("Error Updating Votes! \n<br />\n" .mysql_error());
                echo 
    '
                <meta http-equiv="refresh" content="2;URL=tutorials.php" />
                    Your vote has been submitted!'
    ;
            }
            
            elseif (
    $star == 5) { $update mysql_query("UPDATE `ratings` SET star5 = star5+1, total = total+1 WHERE id = '$id'") or die ("Error Updating Votes! \n<br />\n" .mysql_error());
                echo 
    '
                <meta http-equiv="refresh" content="2;URL=tutorials.php" />
                    Your vote has been submitted!'
    ;
            }
            
            else {
                echo 
    'Please select a rating.';
            }
            
        }
    }


    // If vote not sent
    if (!$_POST['vote']) { ?>

    <!-- Vote Form -->
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'?>">
            <table width="200">
                <tr>
                    <td><input type="radio" name="star" value="1" /></td>
                    <td>1 Star</td>
                </tr>
                <tr>
                    <td><input type="radio" name="star" value="2" /></td>
                    <td>2 Stars</td>
                </tr>
                <tr>
                    <td><input type="radio" name="star" value="3" /></td>
                    <td>3 Stars</td>
                </tr>
                <tr>
                    <td><input type="radio" name="star" value="4" /></td>
                    <td>4 Stars</td>
                </tr>
                <tr>
                    <td><input type="radio" name="star" value="5" /></td>
                    <td>5 Stars</td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="vote" value="Vote" /></td>
                </tr>
            </table>
        </form>
    <!-- /Vote Form -->

    <?
    }

    ?>
    If shows the success message, but doesn't submit the vote. I am lost as to why it is doing this. I appreciate any help.
    Thanks DD, you saved me countless times

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Not 100&#37; sure on this one, but you could try the following (changes are in red):

    Code:
    <?php
    
    $id = $_GET['id'];
    
    // If vote sent
    if ($_POST['vote']) {
    
        $star = $_POST['star'];
        
        // If voted before
        if (isset($_COOKIE['voted']) && $_COOKIE['voted'] == ''.$id.'') { echo '<meta http-equiv="refresh" content="2;URL=tutorials.php" /> <span style="color: #FF0000">You have already voted!</span>'; }
        
        // If have not voted
        else {
        
            @setcookie('voted', ''.$id.'', time() + ((60*60*60) * 72)); 
            $get = mysql_query("SELECT star1, star2, star3, star4, star5, total FROM `ratings` WHERE id = '$id'") or die ("Error Getting Votes! \n<br />\n" .mysql_error());
            $v = mysql_fetch_array($get);
            extract($v);
    
            // Update votes
            if ($star == 1) { $update = mysql_query("UPDATE `ratings` SET `star1` = $star1+1, `total` = $total+1 WHERE id = '$id'") or die ("Error Updating Votes! \n<br />\n" .mysql_error()); 
                echo '
                <meta http-equiv="refresh" content="2;URL=tutorials.php" />
                    Your vote has been submitted!';
            }
            
            elseif ($star == 2) { $update = mysql_query("UPDATE `ratings` SET `star2` = $star2+1, `total` = $total+1 WHERE id = '$id'") or die ("Error Updating Votes! \n<br />\n" .mysql_error());
                echo '
                <meta http-equiv="refresh" content="2;URL=tutorials.php" />
                    Your vote has been submitted!';
            }
            
            elseif ($star == 3) { $update = mysql_query("UPDATE `ratings` SET `star3` = $star3+1, `total` = $total+1 WHERE id = '$id'") or die ("Error Updating Votes! \n<br />\n" .mysql_error());
                echo '
                <meta http-equiv="refresh" content="2;URL=tutorials.php" />
                    Your vote has been submitted!';
            }
            
            elseif ($star == 4) { $update = mysql_query("UPDATE `ratings` SET `star4` = $star4+1, `total` = $total+1 WHERE id = '$id'") or die ("Error Updating Votes! \n<br />\n" .mysql_error());
                echo '
                <meta http-equiv="refresh" content="2;URL=tutorials.php" />
                    Your vote has been submitted!';
            }
            
            elseif ($star == 5) { $update = mysql_query("UPDATE `ratings` SET `star5` = $star5+1, `total` = $total+1 WHERE id = '$id'") or die ("Error Updating Votes! \n<br />\n" .mysql_error());
                echo '
                <meta http-equiv="refresh" content="2;URL=tutorials.php" />
                    Your vote has been submitted!';
            }
            
            else {
                echo 'Please select a rating.';
            }
            
        }
    }
    
    
    // If vote not sent
    if (!$_POST['vote']) { ?>
    
    <!-- Vote Form -->
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
            <table width="200">
                <tr>
                    <td><input type="radio" name="star" value="1" /></td>
                    <td>1 Star</td>
                </tr>
                <tr>
                    <td><input type="radio" name="star" value="2" /></td>
                    <td>2 Stars</td>
                </tr>
                <tr>
                    <td><input type="radio" name="star" value="3" /></td>
                    <td>3 Stars</td>
                </tr>
                <tr>
                    <td><input type="radio" name="star" value="4" /></td>
                    <td>4 Stars</td>
                </tr>
                <tr>
                    <td><input type="radio" name="star" value="5" /></td>
                    <td>5 Stars</td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="vote" value="Vote" /></td>
                </tr>
            </table>
        </form>
    <!-- /Vote Form -->
    
    <?
    }
    
    ?>
    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

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

    Default

    Ok, I changed it to the code you gave and now it shows an error. Here it is:
    Code:
    Warning: extract(): First argument should be an array in /home/bntqann/public_html/projects/tut_system/includes/vote.inc.php on line 19
    I am guessing there is some kind of issue with the sql query. Any idea whats wrong?
    Thanks DD, you saved me countless times

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

    Default

    Anyone have any idea of what could be wrong?
    Thanks DD, you saved me countless times

  5. #5
    Join Date
    May 2007
    Location
    Sweden
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Hi there!

    Do you have any data in the database? You must add at least one "INSERT INTO"-sql question to make sure there are data to update.

    The error about "extract" means it's no array, therefore I don't believe you have add any data in the database, check it and try again.

    Then, it can be better to have the updates as this:
    PHP Code:
    <?php

    $id 
    $_REQUEST['id'];

        if( 
    $_POST['vote'] )
        {

        
    $star $_POST['star'];

            if( isset( 
    $_COOKIE['voted'] ) && ( $_COOKIE['voted'] == $id ) )
            {
            echo 
    "<p>You have already voted!</p>";
            }
            else
            {

            
    setcookie('voted'$idtime() + ( ( 60 60 60 ) * 72 ) );
            
    $get    mysql_query("SELECT star1, star2, star3, star4, star5, total FROM `ratings` WHERE id = '$id'") or die ("Error Getting Votes! \n<br />\n" .mysql_error());
            
    $v    mysql_fetch_array($get);


                switch( 
    $star )
                {

                    case 
    5:
                    
    $update_star "star5 = star5+1";
                    break;

                    case 
    4:
                    
    $update_star "star4 = star4+1";
                    break;

                    case 
    3:
                    
    $update_star "star3 = star3+1";
                    break;

                    case 
    2:
                    
    $update_star "star2 = star2+1";
                    break;

                    case 
    1:
                    
    $update_star "star1 = star1+1";
                    break;

                }

                
    $query mysql_query("UPDATE ´ratings´ SET " $update_star ", total = total + 1 WHERE id = '" $id "' LIMIT 1");

            echo 
    '<meta http-equiv="refresh" content="2;URL=tutorials.php" />Your vote has been submitted!'

            }

        }
        else
        {
        echo 
    "<p>Please select a rate!</p>";
        }


        
    // If vote not sent 
        
    if (!$_POST['vote'])
        {

    ?> 

    <!-- Vote Form --> 
        <form method="post" action="<?php echo $_SERVER['PHP_SELF'?>"> 
            <table width="200"> 
                <tr> 
                    <td><input type="radio" name="star" value="1" /></td> 
                    <td>1 Star</td> 
                </tr> 
                <tr> 
                    <td><input type="radio" name="star" value="2" /></td> 
                    <td>2 Stars</td> 
                </tr> 
                <tr> 
                    <td><input type="radio" name="star" value="3" /></td> 
                    <td>3 Stars</td> 
                </tr> 
                <tr> 
                    <td><input type="radio" name="star" value="4" /></td> 
                    <td>4 Stars</td> 
                </tr> 
                <tr> 
                    <td><input type="radio" name="star" value="5" /></td> 
                    <td>5 Stars</td> 
                </tr> 
                <tr> 
                    <td></td> 
                    <td><input type="submit" name="vote" value="Vote" /></td> 
                </tr> 
            </table> 
        </form> 
    <!-- /Vote Form --> 

    <? 

        


    ?>
    Best regards,
    mbrodin
    Last edited by mbrodin; 05-31-2007 at 05:34 AM.

  6. #6
    Join Date
    May 2007
    Location
    Sweden
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Oh, I think i saw the thing that can be the prob, add this:

    Code:
    <input type="hidden" name="id" value="<?=$id?>" />
    ... somewhere in <form></form>.

    Make sure $id has been added as $_REQUEST['id'] so you can get it both as _GET and _POST!

    Best regards,
    mbrodin
    Last edited by mbrodin; 05-31-2007 at 05:40 AM.

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

    Default

    Thanks a ton mbrodin. Its working now after adding the hidden line to the form. I had that at first and then took it out because for some reason I thought it was not needed. I also really appreciate that condensed version of the script to get which star. Makes it look a LOT neater. Thanks again
    Thanks DD, you saved me countless times

  8. #8
    Join Date
    May 2007
    Location
    Sweden
    Posts
    27
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Quote Originally Posted by Titan85 View Post
    Thanks a ton mbrodin. Its working now after adding the hidden line to the form. I had that at first and then took it out because for some reason I thought it was not needed. I also really appreciate that condensed version of the script to get which star. Makes it look a LOT neater. Thanks again
    I'm glad it's now working.
    And thanks self!

    Best regards,
    mbrodin

  9. #9
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Quote Originally Posted by mbrodin View Post
    Code:
    <input type="hidden" name="id" value="<?=$id?>" />
    Note that <?=$id?> only works with "short_tags" enabled in your php.ini file. A better (and more versatile) method would be to do this instead:
    Code:
    <input type="hidden" name="id" value="<?php echo $id;?>">
    Just figured I'd add that for anyone that has problems with this code.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  10. #10
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    It's good practise to use the full syntax anyways, if you move servers or something for some reason.
    - Mike

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
  •