TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   (HELP!!!!!!!!) Creating a 1 - 5 Star rating script (http://www.talkphp.com/general/1785-help-creating-1-5-star-rating-script.html)

Orc 12-21-2007 02:31 PM

(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 12-21-2007 02:57 PM

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

Village Idiot 12-21-2007 03:01 PM

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?

Orc 12-21-2007 03:02 PM

Quote:

Originally Posted by Village Idiot (Post 6998)
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? -_-

Village Idiot 12-21-2007 03:07 PM

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

Orc 12-21-2007 03:18 PM

THANKS! But could you help me fix this anyway? Please :D?

sketchMedia 12-21-2007 03:29 PM

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'

Village Idiot 12-21-2007 03:29 PM

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.

Orc 12-21-2007 03:31 PM

Quote:

Originally Posted by Village Idiot (Post 7004)
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.

Village Idiot 12-21-2007 03:35 PM

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

Orc 12-21-2007 03:41 PM

Quote:

Originally Posted by Village Idiot (Post 7006)
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.

Village Idiot 12-21-2007 03:49 PM

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();">


Orc 12-21-2007 03:52 PM

Quote:

Originally Posted by Village Idiot (Post 7009)
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

ReSpawN 12-24-2007 04:55 PM

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.


All times are GMT. The time now is 01:58 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0