Log in

View Full Version : Rating Script Error



Titan85
05-28-2007, 11:57 PM
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

$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.

thetestingsite
05-29-2007, 12:05 AM
Not 100&#37; sure on this one, but you could try the following (changes are in red):



<?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.

Titan85
05-29-2007, 01:44 AM
Ok, I changed it to the code you gave and now it shows an error. Here it is:

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?

Titan85
05-31-2007, 12:42 AM
Anyone have any idea of what could be wrong?

mbrodin
05-31-2007, 05:27 AM
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

$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', $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);


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 &#180;ratings&#180; 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

mbrodin
05-31-2007, 05:33 AM
Oh, I think i saw the thing that can be the prob, add this:



<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

Titan85
06-01-2007, 12:48 AM
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

mbrodin
06-01-2007, 10:18 AM
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

thetestingsite
06-02-2007, 11:08 PM
<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:


<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.

mburt
06-02-2007, 11:14 PM
It's good practise to use the full syntax anyways, if you move servers or something for some reason.