TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-21-2007, 02:31 PM   #1 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Unhappy (HELP!!!!!!!!) Creating a 1 - 5 Star rating script

For now I just made it say Rating Successful and Unsuccessful, and query.

PHP Code:
<style type="text/css">
 input.ratingsnstuff {
   background:url('rate/default.png') no-repeat;
   border:none;
   width:16px;
   height:16px;
   margin:1px;
   cursor: pointer;
   }
   
    input:hover.ratingsnstuff {
   background:url('rate/default_hover.png') no-repeat;
   border:none;
   width:16px;
   height:16px;
   margin:1px;
   cursor: pointer;
   }
   </style>
   
<form class="ratingsnstuff" action="rating.php" method="post">
<input class="ratingsnstuff" name="star1"  type="button" value="">
<input class="ratingsnstuff" name="star2"  type="button" value="">
<input class="ratingsnstuff" name="star3"  type="button" value="">
<input class="ratingsnstuff" name="star4"  type="button" value="">
<input class="ratingsnstuff" name="star5"  type="button" value="">
</form>
<?php

$dbhost 
"localhost";
$dbuser "root";
$dbpass "";
$database "rates";

 
mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error());
 
mysql_select_db($database) or die(mysql_error());


 function 
ratingInsert($ratings) {
$rateid =  intval($_REQUEST['rateid']);
   
$rating = array($_POST['star1'], $_POST['star2'],$_POST['star3'], $_POST['star4'], $_POST['star5']);
 foreach(
$rating as $ratings) {
   
$query mysql_query("Insert into db_ratings WHERE rateid=".$rateid." and rate=".$ratings."");

    if(
$query!=null) {
       echo 
"Rated successfully!";
   } 
   elseif(!
$query) {
     echo 
'Rated Unsuccessfully!';
     }
    }
  }
           



$ratingInsert ratingInsert($ratings);
return 
$ratingInsert;


?>
This outputted:
Code:
Rated Unsuccessfully!Rated Unsuccessfully!Rated Unsuccessfully!Rated Unsuccessfully!Rated Unsuccessfully!
Please help!

UPDATE!!!!
Code:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE rateid=0 and rate=' at line 1
No idea what this means :[
Orc is offline  
Reply With Quote
Old 12-21-2007, 02:57 PM   #2 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Is there a better way to group the ratings? And make this work! :[

I've updated more and more of the sql and experimented more but I cannot find the right solution. -.-
Orc is offline  
Reply With Quote
Old 12-21-2007, 03:01 PM   #3 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

You arent inserting any values on your foreach loop, thats the SQL error. You have no separation between the PHP and HTML, it will execute when you load the page. You will want to put the code outside of a function in a different file. Lastly, why the return statement outside of a function?
__________________

Village Idiot is offline  
Reply With Quote
Old 12-21-2007, 03:02 PM   #4 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
You arent inserting any values on your foreach loop. You have no separation between the PHP and HTML, it will execute when you load the page. Lastly, why the return statement outside of a function?
I need to be better guided in PHP is what I need but umm, in foreach I tried that and well it still seems to output the same thing, I dunno. :/ Care to give me any examples or anything? -_-
Orc is offline  
Reply With Quote
Old 12-21-2007, 03:07 PM   #5 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

If thats the case, you need to learn mysql a bit better. Go here for that.

For the SQL, here is the proper syntax
Code:
INSERT INTO `table` VALUES ('each','row','here','in','order')
edit: Just woke up, there is no WHERE in INSERT's syntax
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Orc (12-21-2007)
Old 12-21-2007, 03:18 PM   #6 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

THANKS! But could you help me fix this anyway? Please :D?
Orc is offline  
Reply With Quote
Old 12-21-2007, 03:29 PM   #7 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

read the sql error, its quite informative (unlike some errors from languages i've used):

Code:
'WHERE rateid=0 and rate='
and as villageidiot said b4, there isnt a 'where' in 'insert'
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 12-21-2007, 03:29 PM   #8 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Something like this should work
form.html
HTML Code:
 <style type="text/css">
 input.ratingsnstuff {
   background:url('rate/default.png') no-repeat;
   border:none;
   width:16px;
   height:16px;
   margin:1px;
   cursor: pointer;
   }
   
    input:hover.ratingsnstuff {
   background:url('rate/default_hover.png') no-repeat;
   border:none;
   width:16px;
   height:16px;
   margin:1px;
   cursor: pointer;
   }
   </style>
   
<form class="ratingsnstuff" action="action.php" method="post">
<input class="ratingsnstuff" name="star"  type="button" value="1">
<input class="ratingsnstuff" name="star"  type="button" value="2">
<input class="ratingsnstuff" name="star"  type="button" value="3">
<input class="ratingsnstuff" name="star"  type="button" value="4">
<input class="ratingsnstuff" name="star"  type="button" value="5">
</form>
Note the values I put in there, without that, how does the database know whats been rated? Also, give then the same name, only one can be clicked after all.

action.php
PHP Code:
 <?php

$dbhost 
"localhost";
$dbuser "root";
$dbpass "";
$database "rates";

mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());


$rateid intval($_REQUEST['rateid']);
$rating $_POST["star"];
//lets say the ratings table has two fields, ID (primary key, auto_incriment) and rating.
$query mysql_query("Insert into db_ratings VALUES (NULL,'$rating')") OR die(mysql_error());

echo 
"rating sucessful";
?>
This doesnt need to be in a function, and the rating is all one name so no need for the foreach loop. This may not be valid HTML however.

Insetad of two if statements, just tell the query do die the mysql error if unsuccessful and echo if it passes (Die ends the page, should be taken out after debugging)

Putting a return statement outside of a function will generate a syntax error.
__________________

Village Idiot is offline  
Reply With Quote
Old 12-21-2007, 03:31 PM   #9 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
Something like this should work
form.html
HTML Code:
 <style type="text/css">
 input.ratingsnstuff {
   background:url('rate/default.png') no-repeat;
   border:none;
   width:16px;
   height:16px;
   margin:1px;
   cursor: pointer;
   }
   
    input:hover.ratingsnstuff {
   background:url('rate/default_hover.png') no-repeat;
   border:none;
   width:16px;
   height:16px;
   margin:1px;
   cursor: pointer;
   }
   </style>
   
<form class="ratingsnstuff" action="action.php" method="post">
<input class="ratingsnstuff" name="star"  type="button" value="1">
<input class="ratingsnstuff" name="star"  type="button" value="2">
<input class="ratingsnstuff" name="star"  type="button" value="3">
<input class="ratingsnstuff" name="star"  type="button" value="4">
<input class="ratingsnstuff" name="star"  type="button" value="5">
</form>
Note the values I put in there, without that, how does the database know whats been rated? Also, give then the same name, only one can be clicked after all.

action.php
PHP Code:
 <?php

$dbhost 
"localhost";
$dbuser "root";
$dbpass "";
$database "rates";

mysql_connect($dbhost$dbuser$dbpass) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());


$rateid intval($_REQUEST['rateid']);
$rating $_POST["star"];
//lets say the ratings table has two fields, ID (primary key, auto_incriment) and rating.
$query mysql_query("Insert into db_ratings VALUES (NULL,'$rating')") OR die(mysql_error());

echo 
"rating sucessful";
?>
This doesnt need to be in a function, and the rating is all one name so no need for the foreach loop. This may not be valid HTML however.

Insetad of two if statements, just tell the query do die the mysql error if unsuccessful and echo if it passes (Die ends the page, should be taken out after debugging)

Putting a return statement outside of a function will generate a syntax error.

Wow, you took my script and simplified it! Great; thanks man!

UPDATE: It still says Rated Successful even though I have not submitted one of the stars yet.
Orc is offline  
Reply With Quote
Old 12-21-2007, 03:35 PM   #10 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

It takes you to action.php without you clicking anything?
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Orc (12-21-2007)
Old 12-21-2007, 03:41 PM   #11 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
It takes you to action.php without you clicking anything?
Well no, but it doesn't redirect to action.php when trying to submit the input.
Orc is offline  
Reply With Quote
Old 12-21-2007, 03:49 PM   #12 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

Try changing yout html to
HTML Code:
<form class="ratingsnstuff" name="rating" action="action.php" method="post">
<input class="ratingsnstuff" name="star" type="button" value="1" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="2" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="3" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="4" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="5" OnClick="document.rating.submit();">
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
Orc (12-21-2007)
Old 12-21-2007, 03:52 PM   #13 (permalink)
Orc
The Prestige
 
Orc's Avatar
 
Join Date: Dec 2007
Posts: 1,044
Thanks: 193
Orc is on a distinguished road
Default

Quote:
Originally Posted by Village Idiot View Post
Try changing yout html to
HTML Code:
<form class="ratingsnstuff" name="rating" action="action.php" method="post">
<input class="ratingsnstuff" name="star" type="button" value="1" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="2" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="3" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="4" OnClick="document.rating.submit();">
<input class="ratingsnstuff" name="star" type="button" value="5" OnClick="document.rating.submit();">
Thanks! That worked! But it's a bit slow but it was successful! :D
Orc is offline  
Reply With Quote
Old 12-24-2007, 04:55 PM   #14 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

If you want, you can modify the script using real javascript, instead of the document.name.action format. Should get rid of 'slow' performance which you are talking about.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 11:27 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design